Skip to content

Commit 8207bd2

Browse files
authored
Remove DataFrame.swapaxes (#57363)
* Remove swapaxes * Remove swapaxes
1 parent be6d7d6 commit 8207bd2

File tree

7 files changed

+5
-173
lines changed

7 files changed

+5
-173
lines changed

doc/redirects.csv

-2
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ generated/pandas.DataFrame.style,../reference/api/pandas.DataFrame.style
481481
generated/pandas.DataFrame.sub,../reference/api/pandas.DataFrame.sub
482482
generated/pandas.DataFrame.subtract,../reference/api/pandas.DataFrame.subtract
483483
generated/pandas.DataFrame.sum,../reference/api/pandas.DataFrame.sum
484-
generated/pandas.DataFrame.swapaxes,../reference/api/pandas.DataFrame.swapaxes
485484
generated/pandas.DataFrame.swaplevel,../reference/api/pandas.DataFrame.swaplevel
486485
generated/pandas.DataFrame.tail,../reference/api/pandas.DataFrame.tail
487486
generated/pandas.DataFrame.take,../reference/api/pandas.DataFrame.take
@@ -1206,7 +1205,6 @@ generated/pandas.Series.str.zfill,../reference/api/pandas.Series.str.zfill
12061205
generated/pandas.Series.sub,../reference/api/pandas.Series.sub
12071206
generated/pandas.Series.subtract,../reference/api/pandas.Series.subtract
12081207
generated/pandas.Series.sum,../reference/api/pandas.Series.sum
1209-
generated/pandas.Series.swapaxes,../reference/api/pandas.Series.swapaxes
12101208
generated/pandas.Series.swaplevel,../reference/api/pandas.Series.swaplevel
12111209
generated/pandas.Series.tail,../reference/api/pandas.Series.tail
12121210
generated/pandas.Series.take,../reference/api/pandas.Series.take

doc/source/reference/frame.rst

-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ Reshaping, sorting, transposing
235235
DataFrame.swaplevel
236236
DataFrame.stack
237237
DataFrame.unstack
238-
DataFrame.swapaxes
239238
DataFrame.melt
240239
DataFrame.explode
241240
DataFrame.squeeze

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Removal of prior version deprecations/changes
109109
- Removed :meth:`DataFrame.applymap`, :meth:`Styler.applymap` and :meth:`Styler.applymap_index` (:issue:`52364`)
110110
- Removed ``DataFrame.bool`` and ``Series.bool`` (:issue:`51756`)
111111
- Removed ``DataFrame.first`` and ``DataFrame.last`` (:issue:`53710`)
112+
- Removed ``DataFrame.swapaxes`` and ``Series.swapaxes`` (:issue:`51946`)
112113
- Removed ``DataFrameGroupBy.grouper`` and ``SeriesGroupBy.grouper`` (:issue:`56521`)
113114
- Removed ``DataFrameGroupby.fillna`` and ``SeriesGroupBy.fillna``` (:issue:`55719`)
114115
- Removed ``Index.format``, use :meth:`Index.astype` with ``str`` or :meth:`Index.map` with a ``formatter`` function instead (:issue:`55439`)

pandas/core/generic.py

-61
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@
161161
ensure_index,
162162
)
163163
from pandas.core.internals import BlockManager
164-
from pandas.core.internals.construction import ndarray_to_mgr
165164
from pandas.core.methods.describe import describe_ndframe
166165
from pandas.core.missing import (
167166
clean_fill_method,
@@ -755,66 +754,6 @@ def _set_axis(self, axis: AxisInt, labels: AnyArrayLike | list) -> None:
755754
labels = ensure_index(labels)
756755
self._mgr.set_axis(axis, labels)
757756

758-
@final
759-
def swapaxes(self, axis1: Axis, axis2: Axis, copy: bool | None = None) -> Self:
760-
"""
761-
Interchange axes and swap values axes appropriately.
762-
763-
.. deprecated:: 2.1.0
764-
``swapaxes`` is deprecated and will be removed.
765-
Please use ``transpose`` instead.
766-
767-
Returns
768-
-------
769-
same as input
770-
771-
Examples
772-
--------
773-
Please see examples for :meth:`DataFrame.transpose`.
774-
"""
775-
warnings.warn(
776-
# GH#51946
777-
f"'{type(self).__name__}.swapaxes' is deprecated and "
778-
"will be removed in a future version. "
779-
f"Please use '{type(self).__name__}.transpose' instead.",
780-
FutureWarning,
781-
stacklevel=find_stack_level(),
782-
)
783-
784-
i = self._get_axis_number(axis1)
785-
j = self._get_axis_number(axis2)
786-
787-
if i == j:
788-
return self.copy(deep=False)
789-
790-
mapping = {i: j, j: i}
791-
792-
new_axes = [self._get_axis(mapping.get(k, k)) for k in range(self._AXIS_LEN)]
793-
new_values = self._values.swapaxes(i, j) # type: ignore[union-attr]
794-
if self._mgr.is_single_block and isinstance(self._mgr, BlockManager):
795-
# This should only get hit in case of having a single block, otherwise a
796-
# copy is made, we don't have to set up references.
797-
new_mgr = ndarray_to_mgr(
798-
new_values,
799-
new_axes[0],
800-
new_axes[1],
801-
dtype=None,
802-
copy=False,
803-
)
804-
assert isinstance(new_mgr, BlockManager)
805-
assert isinstance(self._mgr, BlockManager)
806-
new_mgr.blocks[0].refs = self._mgr.blocks[0].refs
807-
new_mgr.blocks[0].refs.add_reference(new_mgr.blocks[0])
808-
out = self._constructor_from_mgr(new_mgr, axes=new_mgr.axes)
809-
return out.__finalize__(self, method="swapaxes")
810-
811-
return self._constructor(
812-
new_values,
813-
*new_axes,
814-
# The no-copy case for CoW is handled above
815-
copy=False,
816-
).__finalize__(self, method="swapaxes")
817-
818757
@final
819758
@doc(klass=_shared_doc_kwargs["klass"])
820759
def droplevel(self, level: IndexLabel, axis: Axis = 0) -> Self:

pandas/tests/copy_view/test_methods.py

+2-67
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ def test_copy_shallow(using_copy_on_write):
8686
lambda df, copy: df.rename_axis(columns="test", copy=copy),
8787
lambda df, copy: df.astype({"b": "int64"}, copy=copy),
8888
# lambda df, copy: df.swaplevel(0, 0, copy=copy),
89-
lambda df, copy: df.swapaxes(0, 0, copy=copy),
9089
lambda df, copy: df.truncate(0, 5, copy=copy),
9190
lambda df, copy: df.infer_objects(copy=copy),
9291
lambda df, copy: df.to_timestamp(copy=copy),
@@ -105,7 +104,6 @@ def test_copy_shallow(using_copy_on_write):
105104
"rename_axis1",
106105
"astype",
107106
# "swaplevel", # only series
108-
"swapaxes",
109107
"truncate",
110108
"infer_objects",
111109
"to_timestamp",
@@ -127,13 +125,7 @@ def test_methods_copy_keyword(request, method, copy, using_copy_on_write):
127125
index = date_range("2012-01-01", freq="D", periods=3, tz="Europe/Brussels")
128126

129127
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [0.1, 0.2, 0.3]}, index=index)
130-
131-
if "swapaxes" in request.node.callspec.id:
132-
msg = "'DataFrame.swapaxes' is deprecated"
133-
with tm.assert_produces_warning(FutureWarning, match=msg):
134-
df2 = method(df, copy=copy)
135-
else:
136-
df2 = method(df, copy=copy)
128+
df2 = method(df, copy=copy)
137129

138130
share_memory = using_copy_on_write or copy is False
139131

@@ -161,7 +153,6 @@ def test_methods_copy_keyword(request, method, copy, using_copy_on_write):
161153
lambda ser, copy: ser.rename_axis(index="test", copy=copy),
162154
lambda ser, copy: ser.astype("int64", copy=copy),
163155
lambda ser, copy: ser.swaplevel(0, 1, copy=copy),
164-
lambda ser, copy: ser.swapaxes(0, 0, copy=copy),
165156
lambda ser, copy: ser.truncate(0, 5, copy=copy),
166157
lambda ser, copy: ser.infer_objects(copy=copy),
167158
lambda ser, copy: ser.to_timestamp(copy=copy),
@@ -180,7 +171,6 @@ def test_methods_copy_keyword(request, method, copy, using_copy_on_write):
180171
"rename_axis0",
181172
"astype",
182173
"swaplevel",
183-
"swapaxes",
184174
"truncate",
185175
"infer_objects",
186176
"to_timestamp",
@@ -204,13 +194,7 @@ def test_methods_series_copy_keyword(request, method, copy, using_copy_on_write)
204194
index = MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]])
205195

206196
ser = Series([1, 2, 3], index=index)
207-
208-
if "swapaxes" in request.node.callspec.id:
209-
msg = "'Series.swapaxes' is deprecated"
210-
with tm.assert_produces_warning(FutureWarning, match=msg):
211-
ser2 = method(ser, copy=copy)
212-
else:
213-
ser2 = method(ser, copy=copy)
197+
ser2 = method(ser, copy=copy)
214198

215199
share_memory = using_copy_on_write or copy is False
216200

@@ -688,55 +672,6 @@ def test_to_frame(using_copy_on_write):
688672
tm.assert_frame_equal(df, expected)
689673

690674

691-
@pytest.mark.parametrize("ax", ["index", "columns"])
692-
def test_swapaxes_noop(using_copy_on_write, ax):
693-
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
694-
df_orig = df.copy()
695-
msg = "'DataFrame.swapaxes' is deprecated"
696-
with tm.assert_produces_warning(FutureWarning, match=msg):
697-
df2 = df.swapaxes(ax, ax)
698-
699-
if using_copy_on_write:
700-
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
701-
else:
702-
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
703-
704-
# mutating df2 triggers a copy-on-write for that column/block
705-
df2.iloc[0, 0] = 0
706-
if using_copy_on_write:
707-
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
708-
tm.assert_frame_equal(df, df_orig)
709-
710-
711-
def test_swapaxes_single_block(using_copy_on_write):
712-
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}, index=["x", "y", "z"])
713-
df_orig = df.copy()
714-
msg = "'DataFrame.swapaxes' is deprecated"
715-
with tm.assert_produces_warning(FutureWarning, match=msg):
716-
df2 = df.swapaxes("index", "columns")
717-
718-
if using_copy_on_write:
719-
assert np.shares_memory(get_array(df2, "x"), get_array(df, "a"))
720-
else:
721-
assert not np.shares_memory(get_array(df2, "x"), get_array(df, "a"))
722-
723-
# mutating df2 triggers a copy-on-write for that column/block
724-
df2.iloc[0, 0] = 0
725-
if using_copy_on_write:
726-
assert not np.shares_memory(get_array(df2, "x"), get_array(df, "a"))
727-
tm.assert_frame_equal(df, df_orig)
728-
729-
730-
def test_swapaxes_read_only_array():
731-
df = DataFrame({"a": [1, 2], "b": 3})
732-
msg = "'DataFrame.swapaxes' is deprecated"
733-
with tm.assert_produces_warning(FutureWarning, match=msg):
734-
df = df.swapaxes(axis1="index", axis2="columns")
735-
df.iloc[0, 0] = 100
736-
expected = DataFrame({0: [100, 3], 1: [2, 3]}, index=["a", "b"])
737-
tm.assert_frame_equal(df, expected)
738-
739-
740675
@pytest.mark.parametrize(
741676
"method, idx",
742677
[

pandas/tests/frame/methods/test_swapaxes.py

-37
This file was deleted.

pandas/tests/generic/test_generic.py

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

241238
# See gh-12301
242239
def test_stat_unexpected_keyword(self, frame_or_series):

0 commit comments

Comments
 (0)