Skip to content

Commit 1da1ac6

Browse files
lukemanleyphofl
authored andcommitted
DEPR: inplace argument in set_axis (pandas-dev#49459)
* enforce deprecation of inplace argument in set_axis * default copy to True
1 parent 27b7579 commit 1da1ac6

File tree

6 files changed

+23
-194
lines changed

6 files changed

+23
-194
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Removal of prior version deprecations/changes
201201
- Removed ``keep_tz`` argument in :meth:`DatetimeIndex.to_series` (:issue:`29731`)
202202
- Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`)
203203
- Remove argument ``inplace`` from :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` (:issue:`35626`)
204+
- Removed argument ``inplace`` from :meth:`DataFrame.set_axis` and :meth:`Series.set_axis`, use ``obj = obj.set_axis(..., copy=False)`` instead (:issue:`48130`)
204205
- Disallow passing positional arguments to :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` (:issue:`41485`)
205206
- Removed :meth:`MultiIndex.is_lexsorted` and :meth:`MultiIndex.lexsort_depth` (:issue:`38701`)
206207
- Removed argument ``how`` from :meth:`PeriodIndex.astype`, use :meth:`PeriodIndex.to_timestamp` instead (:issue:`37982`)

pandas/core/frame.py

+3-37
Original file line numberDiff line numberDiff line change
@@ -5046,39 +5046,6 @@ def align(
50465046
broadcast_axis=broadcast_axis,
50475047
)
50485048

5049-
@overload
5050-
def set_axis(
5051-
self,
5052-
labels,
5053-
*,
5054-
axis: Axis = ...,
5055-
inplace: Literal[False] | lib.NoDefault = ...,
5056-
copy: bool | lib.NoDefault = ...,
5057-
) -> DataFrame:
5058-
...
5059-
5060-
@overload
5061-
def set_axis(
5062-
self,
5063-
labels,
5064-
*,
5065-
axis: Axis = ...,
5066-
inplace: Literal[True],
5067-
copy: bool | lib.NoDefault = ...,
5068-
) -> None:
5069-
...
5070-
5071-
@overload
5072-
def set_axis(
5073-
self,
5074-
labels,
5075-
*,
5076-
axis: Axis = ...,
5077-
inplace: bool | lib.NoDefault = ...,
5078-
copy: bool | lib.NoDefault = ...,
5079-
) -> DataFrame | None:
5080-
...
5081-
50825049
# error: Signature of "set_axis" incompatible with supertype "NDFrame"
50835050
@Appender(
50845051
"""
@@ -5123,10 +5090,9 @@ def set_axis(
51235090
labels,
51245091
*,
51255092
axis: Axis = 0,
5126-
inplace: bool | lib.NoDefault = lib.no_default,
5127-
copy: bool | lib.NoDefault = lib.no_default,
5128-
):
5129-
return super().set_axis(labels, axis=axis, inplace=inplace, copy=copy)
5093+
copy: bool = True,
5094+
) -> DataFrame:
5095+
return super().set_axis(labels, axis=axis, copy=copy)
51305096

51315097
@Substitution(**_shared_doc_kwargs)
51325098
@Appender(NDFrame.reindex.__doc__)

pandas/core/generic.py

+5-63
Original file line numberDiff line numberDiff line change
@@ -695,47 +695,13 @@ def size(self) -> int:
695695
# expected "int") [return-value]
696696
return np.prod(self.shape) # type: ignore[return-value]
697697

698-
@overload
699-
def set_axis(
700-
self: NDFrameT,
701-
labels,
702-
*,
703-
axis: Axis = ...,
704-
inplace: Literal[False] | lib.NoDefault = ...,
705-
copy: bool_t | lib.NoDefault = ...,
706-
) -> NDFrameT:
707-
...
708-
709-
@overload
710-
def set_axis(
711-
self,
712-
labels,
713-
*,
714-
axis: Axis = ...,
715-
inplace: Literal[True],
716-
copy: bool_t | lib.NoDefault = ...,
717-
) -> None:
718-
...
719-
720-
@overload
721-
def set_axis(
722-
self: NDFrameT,
723-
labels,
724-
*,
725-
axis: Axis = ...,
726-
inplace: bool_t | lib.NoDefault = ...,
727-
copy: bool_t | lib.NoDefault = ...,
728-
) -> NDFrameT | None:
729-
...
730-
731698
def set_axis(
732699
self: NDFrameT,
733700
labels,
734701
*,
735702
axis: Axis = 0,
736-
inplace: bool_t | lib.NoDefault = lib.no_default,
737-
copy: bool_t | lib.NoDefault = lib.no_default,
738-
) -> NDFrameT | None:
703+
copy: bool_t = True,
704+
) -> NDFrameT:
739705
"""
740706
Assign desired index to given axis.
741707
@@ -751,45 +717,21 @@ def set_axis(
751717
The axis to update. The value 0 identifies the rows. For `Series`
752718
this parameter is unused and defaults to 0.
753719
754-
inplace : bool, default False
755-
Whether to return a new %(klass)s instance.
756-
757-
.. deprecated:: 1.5.0
758-
759720
copy : bool, default True
760721
Whether to make a copy of the underlying data.
761722
762723
.. versionadded:: 1.5.0
763724
764725
Returns
765726
-------
766-
renamed : %(klass)s or None
767-
An object of type %(klass)s or None if ``inplace=True``.
727+
renamed : %(klass)s
728+
An object of type %(klass)s.
768729
769730
See Also
770731
--------
771732
%(klass)s.rename_axis : Alter the name of the index%(see_also_sub)s.
772733
"""
773-
if inplace is not lib.no_default:
774-
warnings.warn(
775-
f"{type(self).__name__}.set_axis 'inplace' keyword is deprecated "
776-
"and will be removed in a future version. Use "
777-
"`obj = obj.set_axis(..., copy=False)` instead",
778-
FutureWarning,
779-
stacklevel=find_stack_level(),
780-
)
781-
else:
782-
inplace = False
783-
784-
if inplace:
785-
if copy is True:
786-
raise ValueError("Cannot specify both inplace=True and copy=True")
787-
copy = False
788-
elif copy is lib.no_default:
789-
copy = True
790-
791-
self._check_inplace_and_allows_duplicate_labels(inplace)
792-
return self._set_axis_nocheck(labels, axis, inplace, copy=copy)
734+
return self._set_axis_nocheck(labels, axis, inplace=False, copy=copy)
793735

794736
@final
795737
def _set_axis_nocheck(self, labels, axis: Axis, inplace: bool_t, copy: bool_t):

pandas/core/series.py

+4-38
Original file line numberDiff line numberDiff line change
@@ -4925,39 +4925,6 @@ def rename(
49254925
else:
49264926
return self._set_name(index, inplace=inplace)
49274927

4928-
@overload
4929-
def set_axis(
4930-
self,
4931-
labels,
4932-
*,
4933-
axis: Axis = ...,
4934-
inplace: Literal[False] | lib.NoDefault = ...,
4935-
copy: bool | lib.NoDefault = ...,
4936-
) -> Series:
4937-
...
4938-
4939-
@overload
4940-
def set_axis(
4941-
self,
4942-
labels,
4943-
*,
4944-
axis: Axis = ...,
4945-
inplace: Literal[True],
4946-
copy: bool | lib.NoDefault = ...,
4947-
) -> None:
4948-
...
4949-
4950-
@overload
4951-
def set_axis(
4952-
self,
4953-
labels,
4954-
*,
4955-
axis: Axis = ...,
4956-
inplace: bool | lib.NoDefault = ...,
4957-
copy: bool | lib.NoDefault = ...,
4958-
) -> Series | None:
4959-
...
4960-
49614928
# error: Signature of "set_axis" incompatible with supertype "NDFrame"
49624929
@Appender(
49634930
"""
@@ -4984,15 +4951,14 @@ def set_axis(
49844951
see_also_sub="",
49854952
)
49864953
@Appender(NDFrame.set_axis.__doc__)
4987-
def set_axis( # type: ignore[override]
4954+
def set_axis(
49884955
self,
49894956
labels,
49904957
*,
49914958
axis: Axis = 0,
4992-
inplace: bool | lib.NoDefault = lib.no_default,
4993-
copy: bool | lib.NoDefault = lib.no_default,
4994-
) -> Series | None:
4995-
return super().set_axis(labels, axis=axis, inplace=inplace, copy=copy)
4959+
copy: bool = True,
4960+
) -> Series:
4961+
return super().set_axis(labels, axis=axis, copy=copy)
49964962

49974963
# error: Cannot determine type of 'reindex'
49984964
@doc(

pandas/tests/frame/methods/test_set_axis.py

+8-45
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ def obj(self):
1616
def test_set_axis(self, obj):
1717
# GH14636; this tests setting index for both Series and DataFrame
1818
new_index = list("abcd")[: len(obj)]
19-
2019
expected = obj.copy()
2120
expected.index = new_index
22-
23-
# inplace=False
24-
msg = "set_axis 'inplace' keyword is deprecated"
25-
with tm.assert_produces_warning(FutureWarning, match=msg):
26-
result = obj.set_axis(new_index, axis=0, inplace=False)
21+
result = obj.set_axis(new_index, axis=0)
2722
tm.assert_equal(expected, result)
2823

2924
def test_set_axis_copy(self, obj):
@@ -34,12 +29,6 @@ def test_set_axis_copy(self, obj):
3429
expected = obj.copy()
3530
expected.index = new_index
3631

37-
with pytest.raises(
38-
ValueError, match="Cannot specify both inplace=True and copy=True"
39-
):
40-
with tm.assert_produces_warning(FutureWarning):
41-
obj.set_axis(new_index, axis=0, inplace=True, copy=True)
42-
4332
result = obj.set_axis(new_index, axis=0, copy=True)
4433
tm.assert_equal(expected, result)
4534
assert result is not obj
@@ -77,51 +66,25 @@ def test_set_axis_copy(self, obj):
7766
for i in range(obj.shape[1])
7867
)
7968

80-
# Do this last since it alters obj inplace
81-
with tm.assert_produces_warning(FutureWarning):
82-
res = obj.set_axis(new_index, inplace=True, copy=False)
83-
assert res is None
84-
tm.assert_equal(expected, obj)
69+
res = obj.set_axis(new_index, copy=False)
70+
tm.assert_equal(expected, res)
8571
# check we did NOT make a copy
86-
if obj.ndim == 1:
87-
assert tm.shares_memory(obj, orig)
72+
if res.ndim == 1:
73+
assert tm.shares_memory(res, orig)
8874
else:
8975
assert all(
90-
tm.shares_memory(obj.iloc[:, i], orig.iloc[:, i])
91-
for i in range(obj.shape[1])
76+
tm.shares_memory(res.iloc[:, i], orig.iloc[:, i])
77+
for i in range(res.shape[1])
9278
)
9379

94-
@pytest.mark.parametrize("axis", [0, "index", 1, "columns"])
95-
def test_set_axis_inplace_axis(self, axis, obj):
96-
# GH#14636
97-
if obj.ndim == 1 and axis in [1, "columns"]:
98-
# Series only has [0, "index"]
99-
return
100-
101-
new_index = list("abcd")[: len(obj)]
102-
103-
expected = obj.copy()
104-
if axis in [0, "index"]:
105-
expected.index = new_index
106-
else:
107-
expected.columns = new_index
108-
109-
result = obj.copy()
110-
with tm.assert_produces_warning(FutureWarning):
111-
result.set_axis(new_index, axis=axis, inplace=True)
112-
tm.assert_equal(result, expected)
113-
11480
def test_set_axis_unnamed_kwarg_warns(self, obj):
11581
# omitting the "axis" parameter
11682
new_index = list("abcd")[: len(obj)]
11783

11884
expected = obj.copy()
11985
expected.index = new_index
12086

121-
with tm.assert_produces_warning(
122-
FutureWarning, match="set_axis 'inplace' keyword"
123-
):
124-
result = obj.set_axis(new_index, inplace=False)
87+
result = obj.set_axis(new_index)
12588
tm.assert_equal(result, expected)
12689

12790
@pytest.mark.parametrize("axis", [3, "foo"])

pandas/tests/generic/test_duplicate_labels.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ def test_dataframe_insert_raises():
414414
"method, frame_only",
415415
[
416416
(operator.methodcaller("set_index", "A", inplace=True), True),
417-
(operator.methodcaller("set_axis", ["A", "B"], inplace=True), False),
418417
(operator.methodcaller("reset_index", inplace=True), True),
419418
(operator.methodcaller("rename", lambda x: x, inplace=True), False),
420419
],
@@ -427,19 +426,11 @@ def test_inplace_raises(method, frame_only):
427426
s.flags.allows_duplicate_labels = False
428427
msg = "Cannot specify"
429428

430-
warn_msg = "Series.set_axis 'inplace' keyword"
431-
if "set_axis" in str(method):
432-
warn = FutureWarning
433-
else:
434-
warn = None
435-
436429
with pytest.raises(ValueError, match=msg):
437-
with tm.assert_produces_warning(warn, match=warn_msg):
438-
method(df)
430+
method(df)
439431
if not frame_only:
440432
with pytest.raises(ValueError, match=msg):
441-
with tm.assert_produces_warning(warn, match=warn_msg):
442-
method(s)
433+
method(s)
443434

444435

445436
def test_pickle():

0 commit comments

Comments
 (0)