Skip to content

Commit eeaf828

Browse files
Fix pivot index bug (#37771)
1 parent b1a6421 commit eeaf828

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ Reshaping
563563
- Bug in :meth:`DataFrame.agg` with ``func={'name':<FUNC>}`` incorrectly raising ``TypeError`` when ``DataFrame.columns==['Name']`` (:issue:`36212`)
564564
- Bug in :meth:`Series.transform` would give incorrect results or raise when the argument ``func`` was dictionary (:issue:`35811`)
565565
- Bug in :meth:`DataFrame.pivot` did not preserve :class:`MultiIndex` level names for columns when rows and columns both multiindexed (:issue:`36360`)
566+
- Bug in :meth:`DataFrame.pivot` modified ``index`` argument when ``columns`` was passed but ``values`` was not (:issue:`37635`)
566567
- Bug in :func:`join` returned a non deterministic level-order for the resulting :class:`MultiIndex` (:issue:`36910`)
567568
- Bug in :meth:`DataFrame.combine_first()` caused wrong alignment with dtype ``string`` and one level of ``MultiIndex`` containing only ``NA`` (:issue:`37591`)
568569
- Fixed regression in :func:`merge` on merging DatetimeIndex with empty DataFrame (:issue:`36895`)

pandas/core/reshape/pivot.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,9 @@ def pivot(
450450
cols = com.convert_to_list_like(index)
451451
else:
452452
cols = []
453-
cols.extend(columns)
454453

455454
append = index is None
456-
indexed = data.set_index(cols, append=append)
455+
indexed = data.set_index(cols + columns, append=append)
457456
else:
458457
if index is None:
459458
index = [Series(data.index, name=data.index.name)]

pandas/tests/reshape/test_pivot.py

+38
Original file line numberDiff line numberDiff line change
@@ -2153,3 +2153,41 @@ def test_pivot_index_none(self):
21532153

21542154
expected.columns.name = "columns"
21552155
tm.assert_frame_equal(result, expected)
2156+
2157+
def test_pivot_index_list_values_none_immutable_args(self):
2158+
# GH37635
2159+
df = DataFrame(
2160+
{
2161+
"lev1": [1, 1, 1, 2, 2, 2],
2162+
"lev2": [1, 1, 2, 1, 1, 2],
2163+
"lev3": [1, 2, 1, 2, 1, 2],
2164+
"lev4": [1, 2, 3, 4, 5, 6],
2165+
"values": [0, 1, 2, 3, 4, 5],
2166+
}
2167+
)
2168+
index = ["lev1", "lev2"]
2169+
columns = ["lev3"]
2170+
result = df.pivot(index=index, columns=columns, values=None)
2171+
2172+
expected = DataFrame(
2173+
np.array(
2174+
[
2175+
[1.0, 2.0, 0.0, 1.0],
2176+
[3.0, np.nan, 2.0, np.nan],
2177+
[5.0, 4.0, 4.0, 3.0],
2178+
[np.nan, 6.0, np.nan, 5.0],
2179+
]
2180+
),
2181+
index=MultiIndex.from_arrays(
2182+
[(1, 1, 2, 2), (1, 2, 1, 2)], names=["lev1", "lev2"]
2183+
),
2184+
columns=MultiIndex.from_arrays(
2185+
[("lev4", "lev4", "values", "values"), (1, 2, 1, 2)],
2186+
names=[None, "lev3"],
2187+
),
2188+
)
2189+
2190+
tm.assert_frame_equal(result, expected)
2191+
2192+
assert index == ["lev1", "lev2"]
2193+
assert columns == ["lev3"]

0 commit comments

Comments
 (0)