From a51336d4893bfdbb0eb04fcaa6b8319a1c5e74aa Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Sat, 23 Dec 2017 18:57:51 +0000 Subject: [PATCH 1/4] COMPAT-18589: Supporting axis in Series.rename --- pandas/core/generic.py | 2 ++ pandas/tests/generic/test_series.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e9dd82eb64834..df62a40360a12 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -862,6 +862,8 @@ def rename(self, *args, **kwargs): copy = kwargs.pop('copy', True) inplace = kwargs.pop('inplace', False) level = kwargs.pop('level', None) + # Axis supported for compatibility, detailed in GH-18589 + kwargs.pop('axis', None) if kwargs: raise TypeError('rename() got an unexpected keyword ' diff --git a/pandas/tests/generic/test_series.py b/pandas/tests/generic/test_series.py index 3393d7704e411..f3b20f1e995ec 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -227,3 +227,8 @@ def test_valid_deprecated(self): # GH18800 with tm.assert_produces_warning(FutureWarning): pd.Series([]).valid() + + def test_axis_supported(self): + # Supporting axis for compatibility, detailed in GH-18589 + s = Series(range(5)) + s.rename({}, axis=0) From 24edb556ed07485ad9f56f7ec6a9baf42acfaada Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Sat, 23 Dec 2017 21:15:36 +0000 Subject: [PATCH 2/4] PR Comments --- pandas/core/generic.py | 1 - pandas/tests/generic/test_series.py | 5 ----- pandas/tests/series/test_alter_axes.py | 6 ++++++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index df62a40360a12..8c2f6986df940 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -862,7 +862,6 @@ def rename(self, *args, **kwargs): copy = kwargs.pop('copy', True) inplace = kwargs.pop('inplace', False) level = kwargs.pop('level', None) - # Axis supported for compatibility, detailed in GH-18589 kwargs.pop('axis', None) if kwargs: diff --git a/pandas/tests/generic/test_series.py b/pandas/tests/generic/test_series.py index f3b20f1e995ec..3393d7704e411 100644 --- a/pandas/tests/generic/test_series.py +++ b/pandas/tests/generic/test_series.py @@ -227,8 +227,3 @@ def test_valid_deprecated(self): # GH18800 with tm.assert_produces_warning(FutureWarning): pd.Series([]).valid() - - def test_axis_supported(self): - # Supporting axis for compatibility, detailed in GH-18589 - s = Series(range(5)) - s.rename({}, axis=0) diff --git a/pandas/tests/series/test_alter_axes.py b/pandas/tests/series/test_alter_axes.py index f3be7bb9905f4..cceee7fdb5c83 100644 --- a/pandas/tests/series/test_alter_axes.py +++ b/pandas/tests/series/test_alter_axes.py @@ -278,3 +278,9 @@ def test_set_axis_prior_to_deprecation_signature(self): with tm.assert_produces_warning(FutureWarning): result = s.set_axis(0, list('abcd'), inplace=False) tm.assert_series_equal(result, expected) + + def test_rename_axis_supported(self): + # Supporting axis for compatibility, detailed in GH-18589 + s = Series(range(5)) + s.rename({}, axis=0) + s.rename({}, axis=5) From da45cf7724be6cb6430ea8dbb79a870da632f671 Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Thu, 8 Feb 2018 23:12:15 +0000 Subject: [PATCH 3/4] Raising when axis is invalid --- pandas/core/generic.py | 4 +++- pandas/tests/series/test_alter_axes.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8c2f6986df940..886f99978e889 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -862,7 +862,9 @@ def rename(self, *args, **kwargs): copy = kwargs.pop('copy', True) inplace = kwargs.pop('inplace', False) level = kwargs.pop('level', None) - kwargs.pop('axis', None) + axis = kwargs.pop('axis', None) + if axis is not None: + self._get_axis_number(axis) if kwargs: raise TypeError('rename() got an unexpected keyword ' diff --git a/pandas/tests/series/test_alter_axes.py b/pandas/tests/series/test_alter_axes.py index cceee7fdb5c83..186cdbce66b7f 100644 --- a/pandas/tests/series/test_alter_axes.py +++ b/pandas/tests/series/test_alter_axes.py @@ -283,4 +283,5 @@ def test_rename_axis_supported(self): # Supporting axis for compatibility, detailed in GH-18589 s = Series(range(5)) s.rename({}, axis=0) - s.rename({}, axis=5) + with tm.assert_raises_regex(ValueError, 'No axis named 5'): + s.rename({}, axis=5) From 1794b985fdb1f78a55fa1a9c0819f58f540e7066 Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Mon, 12 Feb 2018 23:43:41 +0000 Subject: [PATCH 4/4] Whatsnew, moving test, assigning axis --- doc/source/whatsnew/v0.23.0.txt | 1 + pandas/core/generic.py | 2 +- pandas/tests/series/test_alter_axes.py | 15 ++++++++------- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 72f63a4da0f4d..a8d35602b9185 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -840,6 +840,7 @@ Reshaping - Bug in :func:`concat` when concatting sparse and dense series it returns only a ``SparseDataFrame``. Should be a ``DataFrame``. (:issue:`18914`, :issue:`18686`, and :issue:`16874`) - Improved error message for :func:`DataFrame.merge` when there is no common merge key (:issue:`19427`) - Bug in :func:`DataFrame.join` which does an *outer* instead of a *left* join when being called with multiple DataFrames and some have non-unique indices (:issue:`19624`) +- :func:`Series.rename` now accepts ``axis`` as a kwarg (:issue:`18589`) Other ^^^^^ diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 01b340c54c363..297450417e3cf 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -865,7 +865,7 @@ def rename(self, *args, **kwargs): level = kwargs.pop('level', None) axis = kwargs.pop('axis', None) if axis is not None: - self._get_axis_number(axis) + axis = self._get_axis_number(axis) if kwargs: raise TypeError('rename() got an unexpected keyword ' diff --git a/pandas/tests/series/test_alter_axes.py b/pandas/tests/series/test_alter_axes.py index 36563f40d995e..dce4e82cbdcf1 100644 --- a/pandas/tests/series/test_alter_axes.py +++ b/pandas/tests/series/test_alter_axes.py @@ -81,6 +81,14 @@ def test_rename_set_name_inplace(self): exp = np.array(['a', 'b', 'c'], dtype=np.object_) tm.assert_numpy_array_equal(s.index.values, exp) + def test_rename_axis_supported(self): + # Supporting axis for compatibility, detailed in GH-18589 + s = Series(range(5)) + s.rename({}, axis=0) + s.rename({}, axis='index') + with tm.assert_raises_regex(ValueError, 'No axis named 5'): + s.rename({}, axis=5) + def test_set_name_attribute(self): s = Series([1, 2, 3]) s2 = Series([1, 2, 3], name='bar') @@ -267,10 +275,3 @@ def test_set_axis_prior_to_deprecation_signature(self): with tm.assert_produces_warning(FutureWarning): result = s.set_axis(0, list('abcd'), inplace=False) tm.assert_series_equal(result, expected) - - def test_rename_axis_supported(self): - # Supporting axis for compatibility, detailed in GH-18589 - s = Series(range(5)) - s.rename({}, axis=0) - with tm.assert_raises_regex(ValueError, 'No axis named 5'): - s.rename({}, axis=5)