Skip to content

Commit

Permalink
DEPR: NDFrame.set_axis inplace defaults to false #27525 (#27600)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilkibun authored and jreback committed Jul 26, 2019
1 parent 8c0b131 commit 619f52b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 49 deletions.
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Deprecations
Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Removed the previously deprecated :meth:`Series.get_value`, :meth:`Series.set_value`, :meth:`DataFrame.get_value`, :meth:`DataFrame.set_value` (:issue:`17739`)
-
- Changed the the default value of `inplace` in :meth:`DataFrame.set_index` and :meth:`Series.set_axis`. It now defaults to False (:issue:`27600`)
-

.. _whatsnew_1000.performance:
Expand Down Expand Up @@ -190,7 +190,6 @@ ExtensionArray
-
-


.. _whatsnew_1000.contributors:

Contributors
Expand Down
33 changes: 5 additions & 28 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def _obj_with_exclusions(self):
""" internal compat with SelectionMixin """
return self

def set_axis(self, labels, axis=0, inplace=None):
def set_axis(self, labels, axis=0, inplace=False):
"""
Assign desired index to given axis.
Expand All @@ -587,15 +587,9 @@ def set_axis(self, labels, axis=0, inplace=None):
The axis to update. The value 0 identifies the rows, and 1
identifies the columns.
inplace : bool, default None
inplace : bool, default False
Whether to return a new %(klass)s instance.
.. warning::
``inplace=None`` currently falls back to to True, but in a
future version, will default to False. Use inplace=True
explicitly rather than relying on the default.
Returns
-------
renamed : %(klass)s or None
Expand All @@ -616,35 +610,27 @@ def set_axis(self, labels, axis=0, inplace=None):
2 3
dtype: int64
>>> s.set_axis(['a', 'b', 'c'], axis=0, inplace=False)
>>> s.set_axis(['a', 'b', 'c'], axis=0)
a 1
b 2
c 3
dtype: int64
The original object is not modified.
>>> s
0 1
1 2
2 3
dtype: int64
**DataFrame**
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
Change the row labels.
>>> df.set_axis(['a', 'b', 'c'], axis='index', inplace=False)
>>> df.set_axis(['a', 'b', 'c'], axis='index')
A B
a 1 4
b 2 5
c 3 6
Change the column labels.
>>> df.set_axis(['I', 'II'], axis='columns', inplace=False)
>>> df.set_axis(['I', 'II'], axis='columns')
I II
0 1 4
1 2 5
Expand All @@ -670,15 +656,6 @@ def set_axis(self, labels, axis=0, inplace=None):
)
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.",
FutureWarning,
stacklevel=2,
)
inplace = True
if inplace:
setattr(self, self._get_axis_name(axis), labels)
else:
Expand Down
19 changes: 6 additions & 13 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,30 +1515,23 @@ def test_set_axis_inplace(self):
expected["columns"] = expected[1]

for axis in expected:
# inplace=True
# The FutureWarning comes from the fact that we would like to have
# inplace default to False some day
for inplace, warn in (None, FutureWarning), (True, None):
kwargs = {"inplace": inplace}

result = df.copy()
with tm.assert_produces_warning(warn):
result.set_axis(list("abc"), axis=axis, **kwargs)
tm.assert_frame_equal(result, expected[axis])
result = df.copy()
result.set_axis(list("abc"), axis=axis, inplace=True)
tm.assert_frame_equal(result, expected[axis])

# inplace=False
result = df.set_axis(list("abc"), axis=axis, inplace=False)
result = df.set_axis(list("abc"), axis=axis)
tm.assert_frame_equal(expected[axis], result)

# omitting the "axis" parameter
with tm.assert_produces_warning(None):
result = df.set_axis(list("abc"), inplace=False)
result = df.set_axis(list("abc"))
tm.assert_frame_equal(result, expected[0])

# wrong values for the "axis" parameter
for axis in 3, "foo":
with pytest.raises(ValueError, match="No axis named"):
df.set_axis(list("abc"), axis=axis, inplace=False)
df.set_axis(list("abc"), axis=axis)

def test_set_axis_prior_to_deprecation_signature(self):
df = DataFrame(
Expand Down
9 changes: 3 additions & 6 deletions pandas/tests/series/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,9 @@ def test_set_axis_inplace_axes(self, axis_series):
# inplace=True
# The FutureWarning comes from the fact that we would like to have
# inplace default to False some day
for inplace, warn in [(None, FutureWarning), (True, None)]:
result = ser.copy()
kwargs = {"inplace": inplace}
with tm.assert_produces_warning(warn):
result.set_axis(list("abcd"), axis=axis_series, **kwargs)
tm.assert_series_equal(result, expected)
result = ser.copy()
result.set_axis(list("abcd"), axis=axis_series, inplace=True)
tm.assert_series_equal(result, expected)

def test_set_axis_inplace(self):
# GH14636
Expand Down

0 comments on commit 619f52b

Please sign in to comment.