Skip to content

BUG: unstack accessing wrong index level when midx has mixed names #49092

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

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Groupby/resample/rolling
Reshaping
^^^^^^^^^
- Bug in :meth:`DataFrame.pivot_table` raising ``TypeError`` for nullable dtype and ``margins=True`` (:issue:`48681`)
- Bug in :meth:`DataFrame.unstack` and :meth:`Series.unstack` unstacking wrong level of :class:`MultiIndex` when :class:`MultiIndex` has mixed names (:issue:`48763`)
- Bug in :meth:`DataFrame.pivot` not respecting ``None`` as column name (:issue:`48293`)
- Bug in :func:`join` when ``left_on`` or ``right_on`` is or includes a :class:`CategoricalIndex` incorrectly raising ``AttributeError`` (:issue:`48464`)
-
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,8 @@ def unstack(obj: Series | DataFrame, level, fill_value=None):
else:
level = level[0]

# Prioritize integer interpretation (GH #21677):
if not is_integer(level) and not level == "__placeholder__":
level = obj.index._get_level_number(level)
obj.index._get_level_number(level)

if isinstance(obj, DataFrame):
if isinstance(obj.index, MultiIndex):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/test_stack_unstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2183,3 +2183,16 @@ def test_stack_nullable_dtype(self):
# be an EA
expected = df.astype(object).stack("station")
tm.assert_frame_equal(result, expected)

def test_unstack_mixed_level_names(self):
# GH#48763
arrays = [["a", "a"], [1, 2], ["red", "blue"]]
idx = MultiIndex.from_arrays(arrays, names=("x", 0, "y"))
df = DataFrame({"m": [1, 2]}, index=idx)
result = df.unstack("x")
expected = DataFrame(
[[1], [2]],
columns=MultiIndex.from_tuples([("m", "a")], names=[None, "x"]),
index=MultiIndex.from_tuples([(1, "red"), (2, "blue")], names=[0, "y"]),
)
tm.assert_frame_equal(result, expected)
14 changes: 14 additions & 0 deletions pandas/tests/series/methods/test_unstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ def test_unstack_multi_index_categorical_values():
index=dti.rename("major"),
)
tm.assert_frame_equal(result, expected)


def test_unstack_mixed_level_names():
# GH#48763
arrays = [["a", "a"], [1, 2], ["red", "blue"]]
idx = MultiIndex.from_arrays(arrays, names=("x", 0, "y"))
ser = Series([1, 2], index=idx)
result = ser.unstack("x")
expected = DataFrame(
[[1], [2]],
columns=pd.Index(["a"], name="x"),
index=MultiIndex.from_tuples([(1, "red"), (2, "blue")], names=[0, "y"]),
)
tm.assert_frame_equal(result, expected)