-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fix MutliIndexed unstack failures at tuple names #30943
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
Changes from 5 commits
7e461a1
1314059
8bcb313
4b369ae
6bff8af
e873707
0c39036
f85001b
bd38e5f
912026d
8518879
4363f51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,6 +317,8 @@ def _unstack_multiple(data, clocs, fill_value=None): | |
|
||
index = data.index | ||
|
||
if clocs in index.names: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a comment here on what is going on |
||
clocs = [clocs] | ||
clocs = [index._get_level_number(i) for i in clocs] | ||
|
||
rlocs = [i for i in range(index.nlevels) if i not in clocs] | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||
import pytest | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not find test for Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See pandas/pandas/tests/series/test_analytics.py Line 163 in 4e2546d
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh! many thanks! @jschendel not sure if it is the best place for tests for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah don't appear to be many, could create (and move the existing ones from test_analytics) to pandas/tests/series/test_reshaping.py to mirror frame |
||||
|
||||
import pandas as pd | ||||
import pandas._testing as tm | ||||
|
||||
|
||||
def test_unstack_tuplename_in_multiindex(): | ||||
# GH 19966 | ||||
idx = pd.MultiIndex.from_product( | ||||
[["a", "b", "c"], [1, 2, 3]], names=[("A", "a"), ("B", "b")] | ||||
) | ||||
ser = pd.Series(1, index=idx) | ||||
result = ser.unstack(("A", "a")) | ||||
|
||||
expected = pd.DataFrame( | ||||
[[1, 1, 1], [1, 1, 1], [1, 1, 1]], | ||||
columns=pd.MultiIndex.from_tuples( | ||||
[("a",), ("b",), ("c",)], names=[("A", "a")], | ||||
), | ||||
index=pd.Index([1, 2, 3], name=("B", "b")), | ||||
) | ||||
tm.assert_frame_equal(result, expected) | ||||
|
||||
|
||||
@pytest.mark.parametrize( | ||||
"unstack_idx, expected_values, expected_index, expected_columns", | ||||
[ | ||||
( | ||||
("A", "a"), | ||||
[[1, 1], [1, 1], [1, 1], [1, 1]], | ||||
pd.MultiIndex.from_tuples( | ||||
[(1, 3), (1, 4), (2, 3), (2, 4)], names=["B", "C"] | ||||
), | ||||
pd.MultiIndex.from_tuples([("a",), ("b",)], names=[("A", "a")]), | ||||
), | ||||
( | ||||
(("A", "a"), "B"), | ||||
[[1, 1, 1, 1], [1, 1, 1, 1]], | ||||
pd.Index([3, 4], name="C"), | ||||
pd.MultiIndex.from_tuples( | ||||
[("a", 1), ("a", 2), ("b", 1), ("b", 2)], names=[("A", "a"), "B"] | ||||
), | ||||
), | ||||
], | ||||
) | ||||
def test_unstack_mixed_type_name_in_multiindex( | ||||
unstack_idx, expected_values, expected_index, expected_columns | ||||
): | ||||
# GH 19966 | ||||
idx = pd.MultiIndex.from_product( | ||||
[["a", "b"], [1, 2], [3, 4]], names=[("A", "a"), "B", "C"] | ||||
) | ||||
ser = pd.Series(1, index=idx) | ||||
result = ser.unstack(unstack_idx) | ||||
|
||||
expected = pd.DataFrame( | ||||
expected_values, columns=expected_columns, index=expected_index, | ||||
) | ||||
tm.assert_frame_equal(result, expected) |
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.
:func:`unstack`
doesn't exist, so instead should probably be ":meth:`DataFrame.unstack`
and:meth:`Series.unstack`
"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! changed!