Skip to content

Commit 466e425

Browse files
hendrikmakaitjreback
authored andcommitted
Add inplace support for rename_axis (#16505)
1 parent 344cec7 commit 466e425

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Other Enhancements
3333
- 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 <merging.validation>` (:issue:`16270`)
3434
- ``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`)
3535
- ``RangeIndex.append`` now returns a ``RangeIndex`` object when possible (:issue:`16212`)
36+
- ``Series.rename_axis()`` and ``DataFrame.rename_axis()`` with ``inplace=True`` now return None while renaming the axis inplace. (:issue:`15704`)
3637
- :func:`to_pickle` has gained a protocol parameter (:issue:`16252`). By default, this parameter is set to `HIGHEST_PROTOCOL <https://docs.python.org/3/library/pickle.html#data-stream-format>`__
3738
- :func:`api.types.infer_dtype` now infers decimals. (:issue: `15690`)
3839
- :func:`read_feather` has gained the ``nthreads`` parameter for multi-threaded operations (:issue:`16359`)

pandas/core/generic.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False):
753753
754754
Returns
755755
-------
756-
renamed : type of caller
756+
renamed : type of caller or None if inplace=True
757757
758758
See Also
759759
--------
@@ -784,27 +784,31 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False):
784784
non_mapper = is_scalar(mapper) or (is_list_like(mapper) and not
785785
is_dict_like(mapper))
786786
if non_mapper:
787-
return self._set_axis_name(mapper, axis=axis)
787+
return self._set_axis_name(mapper, axis=axis, inplace=inplace)
788788
else:
789789
axis = self._get_axis_name(axis)
790790
d = {'copy': copy, 'inplace': inplace}
791791
d[axis] = mapper
792792
return self.rename(**d)
793793

794-
def _set_axis_name(self, name, axis=0):
794+
def _set_axis_name(self, name, axis=0, inplace=False):
795795
"""
796-
Alter the name or names of the axis, returning self.
796+
Alter the name or names of the axis.
797797
798798
Parameters
799799
----------
800800
name : str or list of str
801801
Name for the Index, or list of names for the MultiIndex
802802
axis : int or str
803803
0 or 'index' for the index; 1 or 'columns' for the columns
804+
inplace : bool
805+
whether to modify `self` directly or return a copy
806+
807+
.. versionadded: 0.21.0
804808
805809
Returns
806810
-------
807-
renamed : type of caller
811+
renamed : type of caller or None if inplace=True
808812
809813
See Also
810814
--------
@@ -831,9 +835,11 @@ def _set_axis_name(self, name, axis=0):
831835
axis = self._get_axis_number(axis)
832836
idx = self._get_axis(axis).set_names(name)
833837

834-
renamed = self.copy(deep=True)
838+
inplace = validate_bool_kwarg(inplace, 'inplace')
839+
renamed = self if inplace else self.copy()
835840
renamed.set_axis(axis, idx)
836-
return renamed
841+
if not inplace:
842+
return renamed
837843

838844
# ----------------------------------------------------------------------
839845
# Comparisons

pandas/tests/frame/test_alter_axes.py

+17
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,23 @@ def test_rename(self):
418418
pd.Index(['bar', 'foo'], name='name'))
419419
assert renamed.index.name == renamer.index.name
420420

421+
def test_rename_axis_inplace(self):
422+
# GH 15704
423+
frame = self.frame.copy()
424+
expected = frame.rename_axis('foo')
425+
result = frame.copy()
426+
no_return = result.rename_axis('foo', inplace=True)
427+
428+
assert no_return is None
429+
assert_frame_equal(result, expected)
430+
431+
expected = frame.rename_axis('bar', axis=1)
432+
result = frame.copy()
433+
no_return = result.rename_axis('bar', axis=1, inplace=True)
434+
435+
assert no_return is None
436+
assert_frame_equal(result, expected)
437+
421438
def test_rename_multiindex(self):
422439

423440
tuples_index = [('foo1', 'bar1'), ('foo2', 'bar2')]

pandas/tests/series/test_alter_axes.py

+10
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,13 @@ def test_reorder_levels(self):
224224

225225
result = s.reorder_levels(['L0', 'L0', 'L0'])
226226
assert_series_equal(result, expected)
227+
228+
def test_rename_axis_inplace(self):
229+
# GH 15704
230+
series = self.ts.copy()
231+
expected = series.rename_axis('foo')
232+
result = series.copy()
233+
no_return = result.rename_axis('foo', inplace=True)
234+
235+
assert no_return is None
236+
assert_series_equal(result, expected)

0 commit comments

Comments
 (0)