Permute levels of nested mappings #19
thorwhalen
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The problem of needing to flip the levels of nested mappings popped up several times.
If we were working with tables we would have identified the problem earlier on: It's related the problem of groupby, also related to sort by, where we want to pick one or several keys to group (or sort) the rows by. Rather, our problem here would be one of first ungrouping some grouped data, then regrouping according to different grouping rules.
Nested mappings with consistent depth (i.e. the depth of the leafs are all at the same level) could be seen as a representation of a (sparse) tensor (i.e. a multi-dimensional matrix).
Let's get to the concrete stuff.
The
simple_flip_first_two_levels
function below flips the first two levels of a nested mapping.See it in action:
But the function is limited in many ways:
typing.Mapping
, but returns adict
Note what happens when we use the function on a dictionary of depth 3:
Example of permuting levels
But what if we wanted to restructure it as, say:
And then, there's the fact that no matter what kind of mapping we input, we'll always get a dict as our output, which may not be what we want -- sure, we could always cast it to the mapping we want from after getting the dict, but that could be undesirable if our mapping is an interface to significant amounts of data.
We propose to write a
flip_levels
(permute_levels
?renest_mapping
? any name suggestions?) that would look something likeProposal
See One line representation of a permutation, but of course we could handle other representations.
Examples
For example, our
flip_first_two_levels
would basically befunctools.partial(flip_levels, permutation=(1, 0), casts=[dict] * 2
.Code ideas we can use
In the following code, we use
FlatReader
to flatten the input mapping andcast_mapping
to carry out the mapping casting.Note though that
FlatReader
only flattens two levels.There's a discussion about that.
It seems like a good idea to implicitly or explicitly restructure through flattening.
In fact, we should think about wording and design. Might it make more sense to make
group
andungroup
functions (restructuring/permutation being carried out by ungrouping and grouping differently)?Beta Was this translation helpful? Give feedback.
All reactions