Skip to content
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

BUG: pivot dropping wrong column level with numeric columns and ea dtype #56528

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ Reshaping
- Bug in :meth:`DataFrame.melt` where an exception was raised if ``var_name`` was not a string (:issue:`55948`)
- Bug in :meth:`DataFrame.melt` where it would not preserve the datetime (:issue:`55254`)
- Bug in :meth:`DataFrame.pivot_table` where the row margin is incorrect when the columns have numeric names (:issue:`26568`)
- Bug in :meth:`DataFrame.pivot` with numeric columns and extension dtype for data (:issue:`56528`)

Sparse
^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ def _unstack_extension_series(

# equiv: result.droplevel(level=0, axis=1)
# but this avoids an extra copy
result.columns = result.columns.droplevel(0)
result.columns = result.columns._drop_level_numbers([0])
return result


Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2520,11 +2520,12 @@ def test_pivot_empty(self):
expected = DataFrame(index=[], columns=[])
tm.assert_frame_equal(result, expected, check_names=False)

def test_pivot_integer_bug(self):
df = DataFrame(data=[("A", "1", "A1"), ("B", "2", "B2")])
@pytest.mark.parametrize("dtype", [object, "string"])
def test_pivot_integer_bug(self, dtype):
df = DataFrame(data=[("A", "1", "A1"), ("B", "2", "B2")], dtype=dtype)

result = df.pivot(index=1, columns=0, values=2)
tm.assert_index_equal(result.columns, Index(["A", "B"], name=0))
tm.assert_index_equal(result.columns, Index(["A", "B"], name=0, dtype=dtype))

def test_pivot_index_none(self):
# GH#3962
Expand Down
Loading