8000 ENH: provide "inplace" argument to set_axis() by toobaz · Pull Request #16994 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: provide "inplace" argument to set_axis() #16994

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
REF: adapt NDFrame.set_axis() calls to new signature
  • Loading branch information
toobaz committed Jul 23, 2017
commit 8fb9d0f7e78127876316d3a077d78bca0a1cee8d
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ Reindexing / Selection / Label manipulation
Series.reset_index
Series.sample
Series.select
Series.set_axis
Series.take
Series.tail
Series.truncate
Expand Down
32 changes: 16 additions & 16 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def _expand_axes(self, key):
Returns
-------
renamed : %(klass)s or None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs updating

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean... anyway, I just made the following line more explicit

New object if inplace=False, None otherwise.
An object of same type as caller if inplace=False, None otherwise.

See Also
--------
Expand All @@ -502,23 +502,23 @@ def _expand_axes(self, key):
1 2
2 3
dtype: int64
>>> s.set_axis(0, ['a', 'b', 'c'], inplace=False)
>>> s.set_axis(['a', 'b', 'c'], axis=0, inplace=False)
a 1
b 2
c 3
dtype: int64
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.set_axis(0, ['a', 'b', 'c'], inplace=False)
>>> df.set_axis(['a', 'b', 'c'], axis=0, inplace=False)
A B
a 1 4
b 2 5
c 3 6
>>> df.set_axis(1, ['I', 'II'], inplace=False)
>>> df.set_axis(['I', 'II'], axis=1, inplace=False)
I II
0 1 4
1 2 5
2 3 6
>>> df.set_axis(1, ['i', 'ii'], inplace=True)
>>> df.set_axis(['i', 'ii'], axis=1, inplace=True)
>>> df
i ii
0 1 4
Expand All @@ -531,18 +531,18 @@ def _expand_axes(self, key):
def set_axis(self, labels, axis=0, inplace=None):
if is_scalar(labels):
warnings.warn(
"set_axis now takes \"labels\" as first argument, and "
"\"axis\" as named parameter. The old form, with \"axis\" as "
"first parameter and \"labels\" as second, is still supported "
"but will be deprecated in a future version of pandas.",
'set_axis now takes "labels" as first argument, and '
'"axis" as named parameter. The old form, with "axis" as '
'first parameter and \"labels\" as second, is still supported '
'but will be deprecated in a future version of pandas.',
FutureWarning, stacklevel=2)
labels, axis = axis, labels

if inplace is None:
warnings.warn(
"set_axis currently defaults to operating inplace.\nThis "
"will change in a future version of pandas, use "
"inplace=True to avoid this warning.",
'set_axis currently defaults to operating inplace.\nThis '
'will change in a future version of pandas, use '
'inplace=True to avoid this warning.',
FutureWarning, stacklevel=2)
inplace = True
if inplace:
Expand Down Expand Up @@ -957,7 +957,7 @@ def _set_axis_name(self, name, axis=0, inplace=False):

inplace = validate_bool_kwarg(inplace, 'inplace')
renamed = self if inplace else self.copy()
renamed.set_axis(axis, idx)
renamed.set_axis(idx, axis=axis, inplace=True)
if not inplace:
return renamed

Expand Down Expand Up @@ -5845,7 +5845,7 @@ def slice_shift(self, periods=1, axis=0):

new_obj = self._slice(vslicer, axis=axis)
shifted_axis = self._get_axis(axis)[islicer]
new_obj.set_axis(axis, shifted_axis)
new_obj.set_axis(shifted_axis, axis=axis, inplace=True)

return new_obj.__finalize__(self)

Expand Down Expand Up @@ -6005,7 +6005,7 @@ def _tz_convert(ax, tz):
ax = _tz_convert(ax, tz)

result = self._constructor(self._data, copy=copy)
result.set_axis(axis, ax)
result.set_axis(ax, axis=axis, inplace=True)
return result.__finalize__(self)

@deprecate_kwarg(old_arg_name='infer_dst', new_arg_name='ambiguous',
Expand Down Expand Up @@ -6073,7 +6073,7 @@ def _tz_localize(ax, tz, ambiguous):
ax = _tz_localize(ax, tz, ambiguous)

result = self._constructor(self._data, copy=copy)
result.set_axis(axis, ax)
result.set_axis(ax, axis=axis, inplace=True)
return result.__finalize__(self)

# ----------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,11 @@ def _set_result_index_ordered(self, result):
if not self.grouper.is_monotonic:
index = Index(np.concatenate(
self._get_indices(self.grouper.result_index)))
result.set_axis(self.axis, index)
result.set_axis(index, axis=self.axis, inplace=True)
result = result.sort_index(axis=self.axis)

result.set_axis(self.axis, self.obj._get_axis(self.axis))
result.set_axis(self.obj._get_axis(self.axis), axis=self.axis,
inplace=True)
return result

def _dir_additions(self):
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,9 @@ def _all_key(key):
except TypeError:

# we cannot reshape, so coerce the axis
piece.set_axis(cat_axis, piece._get_axis(
cat_axis)._to_safe_for_reshape())
piece.set_axis(piece._get_axis(
cat_axis)._to_safe_for_reshape(),
axis=cat_axis, inplace=True)
piece[all_key] = margin[key]

table_pieces.append(piece)
Expand Down
0