Skip to content

Commit 90f3c10

Browse files
authored
BUG: pivot dropping wrong column level with numeric columns and ea dtype (#56528)
1 parent 5f4b45f commit 90f3c10

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ Reshaping
676676
- Bug in :meth:`DataFrame.melt` where an exception was raised if ``var_name`` was not a string (:issue:`55948`)
677677
- Bug in :meth:`DataFrame.melt` where it would not preserve the datetime (:issue:`55254`)
678678
- Bug in :meth:`DataFrame.pivot_table` where the row margin is incorrect when the columns have numeric names (:issue:`26568`)
679+
- Bug in :meth:`DataFrame.pivot` with numeric columns and extension dtype for data (:issue:`56528`)
679680

680681
Sparse
681682
^^^^^^

pandas/core/reshape/reshape.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ def _unstack_extension_series(
572572

573573
# equiv: result.droplevel(level=0, axis=1)
574574
# but this avoids an extra copy
575-
result.columns = result.columns.droplevel(0)
575+
result.columns = result.columns._drop_level_numbers([0])
576576
return result
577577

578578

pandas/tests/reshape/test_pivot.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2520,11 +2520,12 @@ def test_pivot_empty(self):
25202520
expected = DataFrame(index=[], columns=[])
25212521
tm.assert_frame_equal(result, expected, check_names=False)
25222522

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

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

25292530
def test_pivot_index_none(self):
25302531
# GH#3962

0 commit comments

Comments
 (0)