Skip to content

Commit d79e68a

Browse files
Update tests and solution.
1 parent 27cbd60 commit d79e68a

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

pandas/core/common.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,15 @@ def get_rename_function(mapper):
478478
if isinstance(mapper, (abc.Mapping, ABCSeries)):
479479

480480
def f(x):
481-
if x in mapper:
482-
return mapper[x]
483-
else:
481+
if isinstance(x, bool):
482+
for map_key, repl in mapper.items():
483+
if id(map_key) == id(x):
484+
return repl
484485
return x
486+
return mapper.get(x, x)
485487

486488
else:
487489
f = mapper
488-
489490
return f
490491

491492

pandas/tests/frame/methods/test_rename.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,13 @@ def test_rename_with_duplicate_columns(self):
403403
],
404404
).set_index(["STK_ID", "RPT_Date"], drop=False)
405405
tm.assert_frame_equal(result, expected)
406+
407+
408+
def test_rename_dict_wrong_matches():
409+
# GH4980
410+
df = DataFrame(np.arange(15).reshape(5, 3), columns=[False, True, 2])
411+
mapper = {0: "foo", 1: "bar", 2: "bah"}
412+
res = df.rename(columns=mapper)
413+
expected = df.copy()
414+
expected.columns = [False, True, "bah"]
415+
tm.assert_frame_equal(res, expected)

pandas/tests/series/methods/test_rename.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,13 @@ def test_rename_callable(self):
101101
tm.assert_series_equal(result, expected)
102102

103103
assert result.name == expected.name
104+
105+
106+
def test_rename_dict_wrong_matches():
107+
# GH4980
108+
ser = Series(range(3), index=Index([False, True, 2]))
109+
mapper = {0: "foo", 1: "bar", 2: "bah"}
110+
res = ser.rename(mapper)
111+
expected = ser.copy()
112+
expected.index = Index([False, True, "bah"])
113+
tm.assert_series_equal(res, expected)

0 commit comments

Comments
 (0)