Skip to content

Commit

Permalink
ENH: Check that v is not a callable (#13473)
Browse files Browse the repository at this point in the history
In this commit, the bug is removed from the code, which raised a
TypeError if a function was passed to rename instead of a dictionary.
  • Loading branch information
MaxVanDeursen committed Mar 4, 2019
1 parent 011e2c1 commit 383bc59
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,11 +1098,13 @@ def rename(self, *args, **kwargs):
level = self.axes[axis]._get_level_number(level)

# GH 13473
indexer = self.axes[axis].get_indexer_for(v)
missing_labels = [label for index, label in enumerate(v)
if indexer[index] == -1]
if errors == 'raise' and len(missing_labels) > 0:
raise KeyError('{} not found in axis'.format(missing_labels))
if not callable(v):
indexer = self.axes[axis].get_indexer_for(v)
missing_labels = [label for index, label in enumerate(v)
if indexer[index] == -1]
if errors == 'raise' and len(missing_labels) > 0:
raise KeyError('{} not found in axis'
.format(missing_labels))

result._data = result._data.rename_axis(f, axis=baxis, copy=copy,
level=level)
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,10 @@ def test_rename_errors(self):
expected = DataFrame(columns=['a', 'B', 'C', 'D'])
tm.assert_frame_equal(renamed, expected)

renamed = df.rename(columns=str.lower, errors='raise')
expected = DataFrame(columns=['a', 'b', 'c', 'd'])
tm.assert_frame_equal(renamed, expected)

def test_reorder_levels(self):
index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]],
codes=[[0, 0, 0, 0, 0, 0],
Expand Down

0 comments on commit 383bc59

Please sign in to comment.