Skip to content

Commit

Permalink
ENH: Implement feedback (#13473)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVanDeursen committed Mar 5, 2019
1 parent 3095c5f commit c544613
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Other Enhancements
- ``Series.str`` has gained :meth:`Series.str.casefold` method to removes all case distinctions present in a string (:issue:`25405`)
- :meth:`DataFrame.set_index` now works for instances of ``abc.Iterator``, provided their output is of the same length as the calling frame (:issue:`22484`, :issue:`24984`)
- :meth:`DatetimeIndex.union` now supports the ``sort`` argument. The behaviour of the sort parameter matches that of :meth:`Index.union` (:issue:`24994`)
-
- :meth:`DataFrame.rename` now supports the ``errors`` argument to raise errors when attempting to rename nonexistent keys (:issue:`13473`)

.. _whatsnew_0250.api_breaking:

Expand Down
9 changes: 7 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3958,11 +3958,12 @@ def rename(self, *args, **kwargs):
Raises
------
KeyError
If any of the labels is not found in the selected axis.
If any of the labels is not found in the selected axis and
"errors='raise'".
See Also
--------
DataFrame.rename_axis: Set the name of the axis for the index or
DataFrame.rename_axis : Set the name of the axis for the index or
columns.
Examples
Expand All @@ -3989,6 +3990,10 @@ def rename(self, *args, **kwargs):
1 2 5
2 3 6
>>> df.rename(index=str, columns={"A": "a", "C": "c"}, errors="raise")
Traceback (most recent call last):
KeyError: ['C'] not found in axis
Using axis-style parameters
>>> df.rename(str.lower, axis='columns')
Expand Down
30 changes: 12 additions & 18 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,27 +871,21 @@ def test_rename_bug2(self):
columns=["a"])
tm.assert_frame_equal(df, expected)

def test_rename_errors(self):
# GH 13473
# rename now works with errors parameter

# Error has to be thrown and is thrown
def test_rename_errors_raises(self):
df = DataFrame(columns=['A', 'B', 'C', 'D'])
with pytest.raises(KeyError):
with pytest.raises(KeyError, match='\'E\'] not found in axis'):
df.rename(columns={'A': 'a', 'E': 'e'}, errors='raise')

# Error should be ignored
renamed = df.rename(columns={'A': 'a', 'E': 'e'})
expected = DataFrame(columns=['a', 'B', 'C', 'D'])
tm.assert_frame_equal(renamed, expected)

# Correct behaviour with raising errors.
renamed = df.rename(columns={'A': 'a'}, errors='raise')
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'])
@pytest.mark.parametrize('mapper, errors, expected_columns', [
({'A': 'a', 'E': 'e'}, 'ignore', ['a', 'B', 'C', 'D']),
({'A': 'a'}, 'raise', ['a', 'B', 'C', 'D']),
(str.lower, 'raise', ['a', 'b', 'c', 'd'])])
def test_rename_errors(self, mapper, errors, expected_columns):
# GH 13473
# rename now works with errors parameter
df = DataFrame(columns=['A', 'B', 'C', 'D'])
renamed = df.rename(columns=mapper, errors=errors)
expected = DataFrame(columns=expected_columns)
tm.assert_frame_equal(renamed, expected)

def test_reorder_levels(self):
Expand Down

0 comments on commit c544613

Please sign in to comment.