-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix for passing multiple ints as levels in DataFrame.stack() (#7660) #7770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
their is a method index._get_level_number() which handles translation and validation (not ordering though) also shouldn't this be core/reshape/Stack ? |
The initial bug report was using columns with no level names- if you have names I can see how The existing behaviour handles the multiple levels case within |
get_level_number validates - that's the main use (eg an out of range level number) also handles ambiguity - eg if a level is named 1 u can make a function in core/reshape to handle this kind of thing - maybe multi_stack (and move pretty much the entire stack routine their) |
@jreback: I went back over this and I think what I've come up with now is better overall. Moved the bulk of the logic to the reshape module. At the moment I think I've got a pretty good handle on the cases where
Not sure what to to do with more ambiguous cases though, since I guess someone could |
DataFrame.stack()
(#7660)for lev in level: | ||
result = stack(result, lev, dropna=dropna) | ||
# Otherwise, level numbers may change as each successive level is stacked | ||
elif all(isinstance(lev, int) for lev in level): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these also must be valid levels when you start (e.g. [3,1] is not ok for a len(levels=2))
looks reasonable. just add some out-of-bounds checking on the levels (and tests for those). pls add a note in v0.15.0 as well (API section is ok) |
@jreback OK, this ended up requiring a few more changes. Otherwise, if things are looking good, I can squash back to a single commit. |
@@ -32,6 +32,10 @@ API changes | |||
|
|||
.. _whatsnew_0150.cat: | |||
|
|||
- Passing multiple levels to `DataFrame.stack()` will now work when multiple level |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this above the _whatnew_0150.cat
, you are in a different section here
minor comments, then pls squish |
@jreback Fixed those two things then squashed, should be good now. |
@@ -30,6 +30,10 @@ users upgrade to this version. | |||
API changes | |||
~~~~~~~~~~~ | |||
|
|||
- Passing multiple levels to `DataFrame.stack()` will now work when multiple level | |||
numbers are passed (:issue:`7660`), and will raise a ``ValueError`` when the | |||
levels aren't all level names or all level numbers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a short example to the doc section (reshape.rst), and a reference from here.
@jreback: Added some spacing around sections and some examples to the docs |
You may also stack or unstack more than one level at a time by passing a list | ||
of levels, in which case the end result is as if each level in the list were | ||
processed individually. | ||
|
||
.. ipython:: python | ||
|
||
import itertools |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
u can take out this import
Fix for multiple level numbers If passed levels match level names, use them directly Add test cases for multiple stacking Add check for out of range negative level num in _get_level_number() Use _get_level_number() to do validation and conversion Explain why level list isn't iterated over directly Raise exception on fall-through case Add test cases for raising exceptions Use _get_level_number() to convert and validate negative levels Add changes to release notes Fix Python 2.6 build issue: use assertRaisesRegexp from testing module Add tests for out of bounds level numbers Move API change note to correct section Add blank line after if/elifs Add blank line between sections Too many blank lines between functions Add multiple stacking examples Reference to new examples in What's new docs More blank lines between sections Remove unused itertools import
@jreback Sorry, went through a few different options while trying to come up with succinct example data, didn't realize that import was still in there. |
BUG: Fix for passing multiple ints as levels in DataFrame.stack() (#7660)
@onesandzeroes thanks for this! pls check the docs after built and confirm that they look good |
closes #7660
The specific case that came up in #7660 (and originally in #7653) seems easy enough to fix, so I've covered that in the PR. I feel like this raises a few other potential problems though, e.g.:
ValueError
if the level numbers aren't sorted?