From c544613c8f27c35503aaf9fc4c72f94861e6e811 Mon Sep 17 00:00:00 2001 From: Max van Deursen Date: Tue, 5 Mar 2019 18:05:13 +0100 Subject: [PATCH] ENH: Implement feedback (#13473) --- doc/source/whatsnew/v0.25.0.rst | 2 +- pandas/core/frame.py | 9 ++++++-- pandas/tests/frame/test_alter_axes.py | 30 +++++++++++---------------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 124ec8f4ab92cc..67474918159d52 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -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: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c6f75195df70bc..ea06116e7dd9c1 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 @@ -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') diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index ecceb8c9279462..571455515e2a97 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -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):