Skip to content

Commit

Permalink
BUG: pivot dropping wrong column level with numeric columns and ea dt…
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored and cbpygit committed Jan 2, 2024
1 parent 1f000ab commit 54ac7c0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
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 @@ -676,6 +676,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

0 comments on commit 54ac7c0

Please sign in to comment.