-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: unstack with missing levels results in incorrect index names #38029
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
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.
Thanks @GYHHAHA for the PR!
Some comments
doc/source/whatsnew/v1.2.0.rst
Outdated
@@ -619,6 +619,7 @@ Indexing | |||
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`CategoricalIndex` using listlike indexer that contains elements that are in the index's ``categories`` but not in the index itself failing to raise ``KeyError`` (:issue:`37901`) | |||
- Bug in :meth:`DataFrame.iloc` and :meth:`Series.iloc` aligning objects in ``__setitem__`` (:issue:`22046`) | |||
- Bug in :meth:`DataFrame.loc` did not raise ``KeyError`` when missing combination was given with ``slice(None)`` for remaining levels (:issue:`19556`) | |||
- Bug in :meth:`MultiIndex.remove_unused_levels` drops NaN when level contains NaN (:issue:`37510`) |
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.
... was dropping missing values when levels contain ``NaN``
but also do we want to mention something about set_levels
since that was what the OP was about
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 to 1.3 (both)
df1.index = df1.index.set_levels(levels=new_levels, level="id1") | ||
df1.index = df1.index.set_levels(levels=new_levels, level="id2") | ||
|
||
result = df1.unstack("id3")[("x", 1)].sort_index().index |
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.
I'd say construct the expected frame and compare using assert_frame_equal
alternatively use tm.assert_index_equal
# GH 37510 | ||
df1 = DataFrame( | ||
{ | ||
"id1": [1, 2, 3, 4], |
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.
total nit but I'd call these L1',
L2,
L3`
@@ -271,3 +271,24 @@ def test_argsort(idx): | |||
result = idx.argsort() | |||
expected = idx.values.argsort() | |||
tm.assert_numpy_array_equal(result, expected) | |||
|
|||
|
|||
def test_not_remove_nan(): |
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.
I'd do test_remove_unused_levels_with_missing
frame hardcoded, cc @jreback |
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.
can you merge master
doc/source/whatsnew/v1.2.0.rst
Outdated
@@ -755,6 +755,7 @@ Reshaping | |||
- Bug in :meth:`DataFrame.apply` not setting index of return value when ``func`` return type is ``dict`` (:issue:`37544`) | |||
- Bug in :func:`concat` resulting in a ``ValueError`` when at least one of both inputs had a non-unique index (:issue:`36263`) | |||
- Bug in :meth:`DataFrame.merge` and :meth:`pandas.merge` returning inconsistent ordering in result for ``how=right`` and ``how=left`` (:issue:`35382`) | |||
- Bug in :meth:`DataFrame.unstack` with missing levels led to incorrect index names (:issue:`37510`) |
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 to 1.3
doc/source/whatsnew/v1.2.0.rst
Outdated
@@ -619,6 +619,7 @@ Indexing | |||
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`CategoricalIndex` using listlike indexer that contains elements that are in the index's ``categories`` but not in the index itself failing to raise ``KeyError`` (:issue:`37901`) | |||
- Bug in :meth:`DataFrame.iloc` and :meth:`Series.iloc` aligning objects in ``__setitem__`` (:issue:`22046`) | |||
- Bug in :meth:`DataFrame.loc` did not raise ``KeyError`` when missing combination was given with ``slice(None)`` for remaining levels (:issue:`19556`) | |||
- Bug in :meth:`MultiIndex.remove_unused_levels` drops NaN when level contains NaN (:issue:`37510`) |
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 to 1.3 (both)
thanks @GYHHAHA |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff