From f06f94ec5b335748c7d2adacb9ccec48ba84d3e4 Mon Sep 17 00:00:00 2001 From: Hendrik Makait Date: Thu, 25 May 2017 14:11:52 -0700 Subject: [PATCH 1/2] Add inplace support for rename_axis and add tests for happy case --- pandas/core/generic.py | 20 +++++++++++++------- pandas/tests/frame/test_alter_axes.py | 8 ++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8b186bab29d5e..54ad86b07d4d4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -753,7 +753,7 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): Returns ------- - renamed : type of caller + renamed : type of caller or None if inplace=True See Also -------- @@ -784,16 +784,16 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): non_mapper = is_scalar(mapper) or (is_list_like(mapper) and not is_dict_like(mapper)) if non_mapper: - return self._set_axis_name(mapper, axis=axis) + return self._set_axis_name(mapper, axis=axis, inplace=inplace) else: axis = self._get_axis_name(axis) d = {'copy': copy, 'inplace': inplace} d[axis] = mapper return self.rename(**d) - def _set_axis_name(self, name, axis=0): + def _set_axis_name(self, name, axis=0, inplace=False): """ - Alter the name or names of the axis, returning self. + Alter the name or names of the axis. Parameters ---------- @@ -801,10 +801,14 @@ def _set_axis_name(self, name, axis=0): Name for the Index, or list of names for the MultiIndex axis : int or str 0 or 'index' for the index; 1 or 'columns' for the columns + inplace : bool + whether to modify `self` directly or return a copy + + .. versionadded: 0.21.0 Returns ------- - renamed : type of caller + renamed : type of caller or None if inplace=True See Also -------- @@ -831,9 +835,11 @@ def _set_axis_name(self, name, axis=0): axis = self._get_axis_number(axis) idx = self._get_axis(axis).set_names(name) - renamed = self.copy(deep=True) + inplace = validate_bool_kwarg(inplace, 'inplace') + renamed = self if inplace else self.copy() renamed.set_axis(axis, idx) - return renamed + if not inplace: + return renamed # ---------------------------------------------------------------------- # Comparisons diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index fbfbcc14e9150..b123108b04a8f 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -418,6 +418,14 @@ def test_rename(self): pd.Index(['bar', 'foo'], name='name')) assert renamed.index.name == renamer.index.name + def test_rename_axis_inplace(self): + # GH 15704 + expected = self.frame.rename_axis('foo') + no_return = self.frame.rename_axis('foo', inplace=True) + assert no_return is None + result = self.frame + assert_frame_equal(result, expected) + def test_rename_multiindex(self): tuples_index = [('foo1', 'bar1'), ('foo2', 'bar2')] From 01eaba1382c8862cca33fa13e23448f02fecca5c Mon Sep 17 00:00:00 2001 From: Hendrik Makait Date: Sat, 10 Jun 2017 19:14:28 +0200 Subject: [PATCH 2/2] Add whatsnew note and additional tests for rename_axis(inplace=True) --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/tests/frame/test_alter_axes.py | 15 ++++++++++++--- pandas/tests/series/test_alter_axes.py | 10 ++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 36ca79e8b8714..48d835272537f 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -33,6 +33,7 @@ Other Enhancements - The ``validate`` argument for :func:`merge` function now checks whether a merge is one-to-one, one-to-many, many-to-one, or many-to-many. If a merge is found to not be an example of specified merge type, an exception of type ``MergeError`` will be raised. For more, see :ref:`here ` (:issue:`16270`) - ``Series.to_dict()`` and ``DataFrame.to_dict()`` now support an ``into`` keyword which allows you to specify the ``collections.Mapping`` subclass that you would like returned. The default is ``dict``, which is backwards compatible. (:issue:`16122`) - ``RangeIndex.append`` now returns a ``RangeIndex`` object when possible (:issue:`16212`) +- ``Series.rename_axis()`` and ``DataFrame.rename_axis()`` with ``inplace=True`` now return None while renaming the axis inplace. (:issue:`15704`) - :func:`to_pickle` has gained a protocol parameter (:issue:`16252`). By default, this parameter is set to `HIGHEST_PROTOCOL `__ - :func:`api.types.infer_dtype` now infers decimals. (:issue: `15690`) - :func:`read_feather` has gained the ``nthreads`` parameter for multi-threaded operations (:issue:`16359`) diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index b123108b04a8f..434c02b8eba2f 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -420,10 +420,19 @@ def test_rename(self): def test_rename_axis_inplace(self): # GH 15704 - expected = self.frame.rename_axis('foo') - no_return = self.frame.rename_axis('foo', inplace=True) + frame = self.frame.copy() + expected = frame.rename_axis('foo') + result = frame.copy() + no_return = result.rename_axis('foo', inplace=True) + + assert no_return is None + assert_frame_equal(result, expected) + + expected = frame.rename_axis('bar', axis=1) + result = frame.copy() + no_return = result.rename_axis('bar', axis=1, inplace=True) + assert no_return is None - result = self.frame assert_frame_equal(result, expected) def test_rename_multiindex(self): diff --git a/pandas/tests/series/test_alter_axes.py b/pandas/tests/series/test_alter_axes.py index 98ae749aaa10e..d93f0326fd3b1 100644 --- a/pandas/tests/series/test_alter_axes.py +++ b/pandas/tests/series/test_alter_axes.py @@ -224,3 +224,13 @@ def test_reorder_levels(self): result = s.reorder_levels(['L0', 'L0', 'L0']) assert_series_equal(result, expected) + + def test_rename_axis_inplace(self): + # GH 15704 + series = self.ts.copy() + expected = series.rename_axis('foo') + result = series.copy() + no_return = result.rename_axis('foo', inplace=True) + + assert no_return is None + assert_series_equal(result, expected)