Skip to content

Commit c544613

Browse files
committed
ENH: Implement feedback (#13473)
1 parent 3095c5f commit c544613

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Other Enhancements
2525
- ``Series.str`` has gained :meth:`Series.str.casefold` method to removes all case distinctions present in a string (:issue:`25405`)
2626
- :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`)
2727
- :meth:`DatetimeIndex.union` now supports the ``sort`` argument. The behaviour of the sort parameter matches that of :meth:`Index.union` (:issue:`24994`)
28-
-
28+
- :meth:`DataFrame.rename` now supports the ``errors`` argument to raise errors when attempting to rename nonexistent keys (:issue:`13473`)
2929

3030
.. _whatsnew_0250.api_breaking:
3131

pandas/core/frame.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -3958,11 +3958,12 @@ def rename(self, *args, **kwargs):
39583958
Raises
39593959
------
39603960
KeyError
3961-
If any of the labels is not found in the selected axis.
3961+
If any of the labels is not found in the selected axis and
3962+
"errors='raise'".
39623963
39633964
See Also
39643965
--------
3965-
DataFrame.rename_axis: Set the name of the axis for the index or
3966+
DataFrame.rename_axis : Set the name of the axis for the index or
39663967
columns.
39673968
39683969
Examples
@@ -3989,6 +3990,10 @@ def rename(self, *args, **kwargs):
39893990
1 2 5
39903991
2 3 6
39913992
3993+
>>> df.rename(index=str, columns={"A": "a", "C": "c"}, errors="raise")
3994+
Traceback (most recent call last):
3995+
KeyError: ['C'] not found in axis
3996+
39923997
Using axis-style parameters
39933998
39943999
>>> df.rename(str.lower, axis='columns')

pandas/tests/frame/test_alter_axes.py

+12-18
Original file line numberDiff line numberDiff line change
@@ -871,27 +871,21 @@ def test_rename_bug2(self):
871871
columns=["a"])
872872
tm.assert_frame_equal(df, expected)
873873

874-
def test_rename_errors(self):
875-
# GH 13473
876-
# rename now works with errors parameter
877-
878-
# Error has to be thrown and is thrown
874+
def test_rename_errors_raises(self):
879875
df = DataFrame(columns=['A', 'B', 'C', 'D'])
880-
with pytest.raises(KeyError):
876+
with pytest.raises(KeyError, match='\'E\'] not found in axis'):
881877
df.rename(columns={'A': 'a', 'E': 'e'}, errors='raise')
882878

883-
# Error should be ignored
884-
renamed = df.rename(columns={'A': 'a', 'E': 'e'})
885-
expected = DataFrame(columns=['a', 'B', 'C', 'D'])
886-
tm.assert_frame_equal(renamed, expected)
887-
888-
# Correct behaviour with raising errors.
889-
renamed = df.rename(columns={'A': 'a'}, errors='raise')
890-
expected = DataFrame(columns=['a', 'B', 'C', 'D'])
891-
tm.assert_frame_equal(renamed, expected)
892-
893-
renamed = df.rename(columns=str.lower, errors='raise')
894-
expected = DataFrame(columns=['a', 'b', 'c', 'd'])
879+
@pytest.mark.parametrize('mapper, errors, expected_columns', [
880+
({'A': 'a', 'E': 'e'}, 'ignore', ['a', 'B', 'C', 'D']),
881+
({'A': 'a'}, 'raise', ['a', 'B', 'C', 'D']),
882+
(str.lower, 'raise', ['a', 'b', 'c', 'd'])])
883+
def test_rename_errors(self, mapper, errors, expected_columns):
884+
# GH 13473
885+
# rename now works with errors parameter
886+
df = DataFrame(columns=['A', 'B', 'C', 'D'])
887+
renamed = df.rename(columns=mapper, errors=errors)
888+
expected = DataFrame(columns=expected_columns)
895889
tm.assert_frame_equal(renamed, expected)
896890

897891
def test_reorder_levels(self):

0 commit comments

Comments
 (0)