Skip to content

Commit 89846ee

Browse files
Backport PR #52630 on branch 2.0.x (REGR: pivot changing index name of input object) (#52636)
Backport PR #52630: REGR: pivot changing index name of input object Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 4567d6b commit 89846ee

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717
- Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`)
1818
- Fixed regression in :meth:`DataFrame.sort_values` not resetting index when :class:`DataFrame` is already sorted and ``ignore_index=True`` (:issue:`52553`)
1919
- Fixed regression in :meth:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`)
20+
- Fixed regression in :meth:`DataFrame.pivot` changing :class:`Index` name of input object (:issue:`52629`)
2021

2122
.. ---------------------------------------------------------------------------
2223
.. _whatsnew_201.bug_fixes:

pandas/core/reshape/pivot.py

+2
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ def pivot(
508508
# If columns is None we will create a MultiIndex level with None as name
509509
# which might cause duplicated names because None is the default for
510510
# level names
511+
data = data.copy(deep=False)
512+
data.index = data.index.copy()
511513
data.index.names = [
512514
name if name is not None else lib.NoDefault for name in data.index.names
513515
]

pandas/tests/reshape/test_pivot.py

+7
Original file line numberDiff line numberDiff line change
@@ -2554,3 +2554,10 @@ def test_pivot_values_is_none(self):
25542554
result = df.pivot(columns="b", values=None)
25552555
expected = DataFrame(1, index=[0], columns=Index([2], name="b"))
25562556
tm.assert_frame_equal(result, expected)
2557+
2558+
def test_pivot_not_changing_index_name(self):
2559+
# GH#52692
2560+
df = DataFrame({"one": ["a"], "two": 0, "three": 1})
2561+
expected = df.copy(deep=True)
2562+
df.pivot(index="one", columns="two", values="three")
2563+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)