Skip to content

DEPR: DataFrame.swapaxes #51960

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

Merged
merged 22 commits into from
Mar 25, 2023
Merged
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
9 changes: 9 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,15 @@ def swapaxes(
-------
same as input
"""
warnings.warn(
# GH#51946
f"'{type(self).__name__}.swapaxes' is deprecated and \
will be removed in a future version. \
Please use '{type(self).__name__}.transpose' instead.",
FutureWarning,
stacklevel=find_stack_level(),
)

i = self._get_axis_number(axis1)
j = self._get_axis_number(axis2)

Expand Down
14 changes: 8 additions & 6 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_copy_shallow(using_copy_on_write):
lambda df, copy: df.rename_axis(columns="test", copy=copy),
lambda df, copy: df.astype({"b": "int64"}, copy=copy),
# lambda df, copy: df.swaplevel(0, 0, copy=copy),
lambda df, copy: df.swapaxes(0, 0, copy=copy),
# lambda df, copy: df.swapaxes(0, 0, copy=copy),
lambda df, copy: df.truncate(0, 5, copy=copy),
lambda df, copy: df.infer_objects(copy=copy),
lambda df, copy: df.to_timestamp(copy=copy),
Expand All @@ -91,7 +91,6 @@ def test_copy_shallow(using_copy_on_write):
"rename_axis1",
"astype",
# "swaplevel", # only series
"swapaxes",
"truncate",
"infer_objects",
"to_timestamp",
Expand Down Expand Up @@ -142,7 +141,7 @@ def test_methods_copy_keyword(
lambda ser, copy: ser.rename_axis(index="test", copy=copy),
lambda ser, copy: ser.astype("int64", copy=copy),
lambda ser, copy: ser.swaplevel(0, 1, copy=copy),
lambda ser, copy: ser.swapaxes(0, 0, copy=copy),
# lambda ser, copy: ser.swapaxes(0, 0, copy=copy),
lambda ser, copy: ser.truncate(0, 5, copy=copy),
lambda ser, copy: ser.infer_objects(copy=copy),
lambda ser, copy: ser.to_timestamp(copy=copy),
Expand All @@ -160,7 +159,6 @@ def test_methods_copy_keyword(
"rename_axis0",
"astype",
"swaplevel",
"swapaxes",
"truncate",
"infer_objects",
"to_timestamp",
Expand Down Expand Up @@ -610,7 +608,9 @@ def test_to_frame(using_copy_on_write):
def test_swapaxes_noop(using_copy_on_write, ax):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
df_orig = df.copy()
df2 = df.swapaxes(ax, ax)
msg = "'DataFrame.swapaxes' is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
df2 = df.swapaxes(ax, ax)

if using_copy_on_write:
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
Expand All @@ -627,7 +627,9 @@ def test_swapaxes_noop(using_copy_on_write, ax):
def test_swapaxes_single_block(using_copy_on_write):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}, index=["x", "y", "z"])
df_orig = df.copy()
df2 = df.swapaxes("index", "columns")
msg = "'DataFrame.swapaxes' is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
df2 = df.swapaxes("index", "columns")

if using_copy_on_write:
assert np.shares_memory(get_array(df2, "x"), get_array(df, "a"))
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/extension/base/dim2.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def test_frame_from_2d_array(self, data):

def test_swapaxes(self, data):
arr2d = data.repeat(2).reshape(-1, 2)

result = arr2d.swapaxes(0, 1)
msg = "'DataFrame.swapaxes' is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = arr2d.swapaxes(0, 1)
expected = arr2d.T
self.assert_extension_array_equal(result, expected)

Expand Down
22 changes: 15 additions & 7 deletions pandas/tests/frame/methods/test_swapaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@
class TestSwapAxes:
def test_swapaxes(self):
df = DataFrame(np.random.randn(10, 5))
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))
tm.assert_frame_equal(df.T, df.swapaxes(1, 0))
msg = "'DataFrame.swapaxes' is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))
tm.assert_frame_equal(df.T, df.swapaxes(1, 0))

def test_swapaxes_noop(self):
df = DataFrame(np.random.randn(10, 5))
tm.assert_frame_equal(df, df.swapaxes(0, 0))
msg = "'DataFrame.swapaxes' is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_frame_equal(df, df.swapaxes(0, 0))

def test_swapaxes_invalid_axis(self):
df = DataFrame(np.random.randn(10, 5))
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.swapaxes(2, 5)
msg = "'DataFrame.swapaxes' is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.swapaxes(2, 5)
Comment on lines +24 to +28
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if nested blocks of code are better expressed. (pytest may break pre-commit)


def test_round_empty_not_input(self):
# GH#51032
df = DataFrame({"a": [1, 2]})
result = df.swapaxes("index", "index")
msg = "'DataFrame.swapaxes' is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.swapaxes("index", "index")
tm.assert_frame_equal(df, result)
assert df is not result
1 change: 0 additions & 1 deletion pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@
operator.methodcaller("isin", pd.DataFrame({"A": [1]})),
),
),
(pd.DataFrame, frame_data, operator.methodcaller("swapaxes", 0, 1)),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that this file is testing all existing finalize methods, so I removed swapaxes due to its depreciation.

(pd.DataFrame, frame_mi_data, operator.methodcaller("droplevel", "A")),
(pd.DataFrame, frame_data, operator.methodcaller("pop", "A")),
pytest.param(
Expand Down