Skip to content

Commit eae6000

Browse files
luke396phoflmroeschke
authored
DEPR: DataFrame.swapaxes (#51960)
* DEPR: DataFrame.swapaxes * add whatsnew * improve and fix ci * improve whatsnew * improve typo and blank line * fix layout * delete swapaxes in test_finalize * fix ci * fix ci * fix msg * fix ci numpydev * Update pandas/core/generic.py Co-authored-by: Matthew Roeschke <[email protected]> * use request.node.callspec.id instead --------- Co-authored-by: Patrick Hoefler <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 710052c commit eae6000

File tree

6 files changed

+53
-15
lines changed

6 files changed

+53
-15
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Deprecations
114114
- Deprecated 'method', 'limit', and 'fill_axis' keywords in :meth:`DataFrame.align` and :meth:`Series.align`, explicitly call ``fillna`` on the alignment results instead (:issue:`51856`)
115115
- Deprecated 'broadcast_axis' keyword in :meth:`Series.align` and :meth:`DataFrame.align`, upcast before calling ``align`` with ``left = DataFrame({col: left for col in right.columns}, index=right.index)`` (:issue:`51856`)
116116
- Deprecated the 'axis' keyword in :meth:`.GroupBy.idxmax`, :meth:`.GroupBy.idxmin`, :meth:`.GroupBy.fillna`, :meth:`.GroupBy.take`, :meth:`.GroupBy.skew`, :meth:`.GroupBy.rank`, :meth:`.GroupBy.cumprod`, :meth:`.GroupBy.cumsum`, :meth:`.GroupBy.cummax`, :meth:`.GroupBy.cummin`, :meth:`.GroupBy.pct_change`, :meth:`GroupBy.diff`, :meth:`.GroupBy.shift`, and :meth:`DataFrameGroupBy.corrwith`; for ``axis=1`` operate on the underlying :class:`DataFrame` instead (:issue:`50405`, :issue:`51046`)
117+
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
117118
-
118119

119120
.. ---------------------------------------------------------------------------

pandas/core/generic.py

+9
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,15 @@ def swapaxes(self, axis1: Axis, axis2: Axis, copy: bool_t | None = None) -> Self
741741
-------
742742
same as input
743743
"""
744+
warnings.warn(
745+
# GH#51946
746+
f"'{type(self).__name__}.swapaxes' is deprecated and "
747+
"will be removed in a future version. "
748+
f"Please use '{type(self).__name__}.transpose' instead.",
749+
FutureWarning,
750+
stacklevel=find_stack_level(),
751+
)
752+
744753
i = self._get_axis_number(axis1)
745754
j = self._get_axis_number(axis2)
746755

pandas/tests/copy_view/test_methods.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,13 @@ def test_methods_copy_keyword(
115115
index = date_range("2012-01-01", freq="D", periods=3, tz="Europe/Brussels")
116116

117117
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}, index=index)
118-
df2 = method(df, copy=copy)
118+
119+
if "swapaxes" in request.node.callspec.id:
120+
msg = "'DataFrame.swapaxes' is deprecated"
121+
with tm.assert_produces_warning(FutureWarning, match=msg):
122+
df2 = method(df, copy=copy)
123+
else:
124+
df2 = method(df, copy=copy)
119125

120126
share_memory = using_copy_on_write or copy is False
121127

@@ -184,7 +190,13 @@ def test_methods_series_copy_keyword(request, method, copy, using_copy_on_write)
184190
index = MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]])
185191

186192
ser = Series([1, 2, 3], index=index)
187-
ser2 = method(ser, copy=copy)
193+
194+
if "swapaxes" in request.node.callspec.id:
195+
msg = "'Series.swapaxes' is deprecated"
196+
with tm.assert_produces_warning(FutureWarning, match=msg):
197+
ser2 = method(ser, copy=copy)
198+
else:
199+
ser2 = method(ser, copy=copy)
188200

189201
share_memory = using_copy_on_write or copy is False
190202

@@ -625,7 +637,9 @@ def test_to_frame(using_copy_on_write):
625637
def test_swapaxes_noop(using_copy_on_write, ax):
626638
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
627639
df_orig = df.copy()
628-
df2 = df.swapaxes(ax, ax)
640+
msg = "'DataFrame.swapaxes' is deprecated"
641+
with tm.assert_produces_warning(FutureWarning, match=msg):
642+
df2 = df.swapaxes(ax, ax)
629643

630644
if using_copy_on_write:
631645
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
@@ -642,7 +656,9 @@ def test_swapaxes_noop(using_copy_on_write, ax):
642656
def test_swapaxes_single_block(using_copy_on_write):
643657
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}, index=["x", "y", "z"])
644658
df_orig = df.copy()
645-
df2 = df.swapaxes("index", "columns")
659+
msg = "'DataFrame.swapaxes' is deprecated"
660+
with tm.assert_produces_warning(FutureWarning, match=msg):
661+
df2 = df.swapaxes("index", "columns")
646662

647663
if using_copy_on_write:
648664
assert np.shares_memory(get_array(df2, "x"), get_array(df, "a"))
@@ -658,7 +674,9 @@ def test_swapaxes_single_block(using_copy_on_write):
658674

659675
def test_swapaxes_read_only_array():
660676
df = DataFrame({"a": [1, 2], "b": 3})
661-
df = df.swapaxes(axis1="index", axis2="columns")
677+
msg = "'DataFrame.swapaxes' is deprecated"
678+
with tm.assert_produces_warning(FutureWarning, match=msg):
679+
df = df.swapaxes(axis1="index", axis2="columns")
662680
df.iloc[0, 0] = 100
663681
expected = DataFrame({0: [100, 3], 1: [2, 3]}, index=["a", "b"])
664682
tm.assert_frame_equal(df, expected)

pandas/tests/frame/methods/test_swapaxes.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,30 @@
88
class TestSwapAxes:
99
def test_swapaxes(self):
1010
df = DataFrame(np.random.randn(10, 5))
11-
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))
12-
tm.assert_frame_equal(df.T, df.swapaxes(1, 0))
11+
msg = "'DataFrame.swapaxes' is deprecated"
12+
with tm.assert_produces_warning(FutureWarning, match=msg):
13+
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))
14+
tm.assert_frame_equal(df.T, df.swapaxes(1, 0))
1315

1416
def test_swapaxes_noop(self):
1517
df = DataFrame(np.random.randn(10, 5))
16-
tm.assert_frame_equal(df, df.swapaxes(0, 0))
18+
msg = "'DataFrame.swapaxes' is deprecated"
19+
with tm.assert_produces_warning(FutureWarning, match=msg):
20+
tm.assert_frame_equal(df, df.swapaxes(0, 0))
1721

1822
def test_swapaxes_invalid_axis(self):
1923
df = DataFrame(np.random.randn(10, 5))
20-
msg = "No axis named 2 for object type DataFrame"
21-
with pytest.raises(ValueError, match=msg):
22-
df.swapaxes(2, 5)
24+
msg = "'DataFrame.swapaxes' is deprecated"
25+
with tm.assert_produces_warning(FutureWarning, match=msg):
26+
msg = "No axis named 2 for object type DataFrame"
27+
with pytest.raises(ValueError, match=msg):
28+
df.swapaxes(2, 5)
2329

2430
def test_round_empty_not_input(self):
2531
# GH#51032
2632
df = DataFrame({"a": [1, 2]})
27-
result = df.swapaxes("index", "index")
33+
msg = "'DataFrame.swapaxes' is deprecated"
34+
with tm.assert_produces_warning(FutureWarning, match=msg):
35+
result = df.swapaxes("index", "index")
2836
tm.assert_frame_equal(df, result)
2937
assert df is not result

pandas/tests/generic/test_finalize.py

-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@
251251
operator.methodcaller("isin", pd.DataFrame({"A": [1]})),
252252
),
253253
),
254-
(pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)),
255254
(pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")),
256255
(pd.DataFrame, frame_data, operator.methodcaller("pop", "A")),
257256
pytest.param(

pandas/tests/generic/test_generic.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,11 @@ def test_size_compat(self, frame_or_series):
230230
def test_split_compat(self, frame_or_series):
231231
# xref GH8846
232232
o = construct(frame_or_series, shape=10)
233-
assert len(np.array_split(o, 5)) == 5
234-
assert len(np.array_split(o, 2)) == 2
233+
with tm.assert_produces_warning(
234+
FutureWarning, match=".swapaxes' is deprecated", check_stacklevel=False
235+
):
236+
assert len(np.array_split(o, 5)) == 5
237+
assert len(np.array_split(o, 2)) == 2
235238

236239
# See gh-12301
237240
def test_stat_unexpected_keyword(self, frame_or_series):

0 commit comments

Comments
 (0)