Skip to content

Commit 1fc35cb

Browse files
jbrockmendelzhangbowen-coder
authored andcommitted
BUG: rename with Series with non-unique index (pandas-dev#62906)
1 parent 0eba9c4 commit 1fc35cb

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ Other
12361236
- Bug in :meth:`DataFrame.query` where using duplicate column names led to a ``TypeError``. (:issue:`59950`)
12371237
- Bug in :meth:`DataFrame.query` which raised an exception or produced incorrect results when expressions contained backtick-quoted column names containing the hash character ``#``, backticks, or characters that fall outside the ASCII range (U+0001..U+007F). (:issue:`59285`) (:issue:`49633`)
12381238
- Bug in :meth:`DataFrame.query` which raised an exception when querying integer column names using backticks. (:issue:`60494`)
1239+
- Bug in :meth:`DataFrame.rename` and :meth:`Series.rename` when passed a ``mapper``, ``index``, or ``columns`` argument that is a :class:`Series` with non-unique ``ser.index`` producing a corrupted result instead of raising ``ValueError`` (:issue:`58621`)
12391240
- Bug in :meth:`DataFrame.sample` with ``replace=False`` and ``(n * max(weights) / sum(weights)) > 1``, the method would return biased results. Now raises ``ValueError``. (:issue:`61516`)
12401241
- Bug in :meth:`DataFrame.shift` where passing a ``freq`` on a DataFrame with no columns did not shift the index correctly. (:issue:`60102`)
12411242
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)

pandas/core/generic.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,10 @@ def _rename(
10541054
if level is not None:
10551055
level = ax._get_level_number(level)
10561056

1057+
if isinstance(replacements, ABCSeries) and not replacements.index.is_unique:
1058+
# GH#58621
1059+
raise ValueError("Cannot rename with a Series with non-unique index.")
1060+
10571061
# GH 13473
10581062
if not callable(replacements):
10591063
if ax._is_multi and level is not None:

pandas/tests/frame/methods/test_rename.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
DataFrame,
99
Index,
1010
MultiIndex,
11+
Series,
1112
merge,
1213
)
1314
import pandas._testing as tm
@@ -409,3 +410,33 @@ def test_rename_boolean_index(self):
409410
index=["foo", "bar", "bah"],
410411
)
411412
tm.assert_frame_equal(res, exp)
413+
414+
def test_rename_non_unique_index_series(self):
415+
# GH#58621
416+
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
417+
orig = df.copy(deep=True)
418+
419+
rename_series = Series(["X", "Y", "Z", "W"], index=["A", "B", "B", "C"])
420+
421+
msg = "Cannot rename with a Series with non-unique index"
422+
with pytest.raises(ValueError, match=msg):
423+
df.rename(rename_series)
424+
with pytest.raises(ValueError, match=msg):
425+
df.rename(columns=rename_series)
426+
with pytest.raises(ValueError, match=msg):
427+
df.rename(columns=rename_series, inplace=True)
428+
429+
# check we didn't corrupt the original
430+
tm.assert_frame_equal(df, orig)
431+
432+
# Check the Series method while we're here
433+
ser = df.iloc[0]
434+
with pytest.raises(ValueError, match=msg):
435+
ser.rename(rename_series)
436+
with pytest.raises(ValueError, match=msg):
437+
ser.rename(index=rename_series)
438+
with pytest.raises(ValueError, match=msg):
439+
ser.rename(index=rename_series, inplace=True)
440+
441+
# check we didn't corrupt the original
442+
tm.assert_series_equal(ser, orig.iloc[0])

0 commit comments

Comments
 (0)