-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add inplace support for rename_axis #16505
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,27 +784,31 @@ 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 | ||
---------- | ||
name : str or list of str | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. versionadded tag There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which version should I specify? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specified 0.20.2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to 0.21.0 |
||
|
||
.. 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason you removed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's the default and can thus be removed (as of @jreback's request that disappeared after my rebase). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, perfect! (I looked for a comment about it, but didn't find one :-)) |
||
renamed.set_axis(axis, idx) | ||
return renamed | ||
if not inplace: | ||
return renamed | ||
|
||
# ---------------------------------------------------------------------- | ||
# Comparisons | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -418,6 +418,23 @@ def test_rename(self): | |
pd.Index(['bar', 'foo'], name='name')) | ||
assert renamed.index.name == renamer.index.name | ||
|
||
def test_rename_axis_inplace(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also add a similar test in series/test_alter_axes.py and tests this with axis=1 as well. |
||
# GH 15704 | ||
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 | ||
assert_frame_equal(result, expected) | ||
|
||
def test_rename_multiindex(self): | ||
|
||
tuples_index = [('foo1', 'bar1'), ('foo2', 'bar2')] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback - added