Skip to content

Commit 7e6bd88

Browse files
committed
Deprecate NDFrame.swapaxes
1 parent e25fd0d commit 7e6bd88

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

doc/source/whatsnew/v0.25.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ Other Deprecations
476476
the :meth:`SparseArray.to_dense` method instead (:issue:`26421`).
477477
- The functions :func:`pandas.to_datetime` and :func:`pandas.to_timedelta` have deprecated the ``box`` keyword. Instead, use :meth:`to_numpy` or :meth:`Timestamp.to_datetime64` or :meth:`Timedelta.to_timedelta64`. (:issue:`24416`)
478478
- The :meth:`DataFrame.compound` and :meth:`Series.compound` methods are deprecated and will be removed in a future version (:issue:`26405`).
479+
- The :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes` methods are deprecated and will be removed in a future version.
480+
Use :meth:`DataFrame.transpose` and :meth:`Series.transpose` instead(:issue:`26654`).
479481

480482
.. _whatsnew_0250.prior_deprecations:
481483

pandas/core/generic.py

+7
Original file line numberDiff line numberDiff line change
@@ -695,10 +695,17 @@ def swapaxes(self, axis1, axis2, copy=True):
695695
"""
696696
Interchange axes and swap values axes appropriately.
697697
698+
.. deprecated:: 0.25.0
699+
Use ``.T`` or ``.transpose()`` instead.
700+
698701
Returns
699702
-------
700703
y : same as input
701704
"""
705+
warnings.warn("{obj}.swapaxes is deprecated and will be removed "
706+
"in a future version. Use {obj}.T or {obj}.transpose() "
707+
"instead".format(obj=type(self).__name__),
708+
FutureWarning, stacklevel=2)
702709
i = self._get_axis_number(axis1)
703710
j = self._get_axis_number(axis2)
704711

pandas/core/groupby/ops.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,14 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1,
468468
arity = self._cython_arity.get(how, 1)
469469

470470
vdim = values.ndim
471-
swapped = False
471+
transposed = False
472472
if vdim == 1:
473473
values = values[:, None]
474474
out_shape = (self.ngroups, arity)
475475
else:
476476
if axis > 0:
477-
swapped = True
478-
values = values.swapaxes(0, axis)
477+
transposed = True
478+
values = values.transpose()
479479
if arity > 1:
480480
raise NotImplementedError("arity of more than 1 is not "
481481
"supported for the 'how' argument")
@@ -567,8 +567,8 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1,
567567
else:
568568
names = None
569569

570-
if swapped:
571-
result = result.swapaxes(0, axis)
570+
if transposed:
571+
result = result.transpose()
572572

573573
return result, names
574574

pandas/tests/frame/test_api.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,13 @@ def test_transpose(self, float_frame):
363363
for col, s in mixed_T.items():
364364
assert s.dtype == np.object_
365365

366-
def test_swapaxes(self):
366+
def test_swapaxes_deprecated(self):
367367
df = self.klass(np.random.randn(10, 5))
368-
self._assert_frame_equal(df.T, df.swapaxes(0, 1))
369-
self._assert_frame_equal(df.T, df.swapaxes(1, 0))
370-
self._assert_frame_equal(df, df.swapaxes(0, 0))
371-
msg = ("No axis named 2 for object type"
372-
r" <class 'pandas.core(.sparse)?.frame.(Sparse)?DataFrame'>")
373-
with pytest.raises(ValueError, match=msg):
374-
df.swapaxes(2, 5)
368+
369+
with tm.assert_produces_warning(FutureWarning):
370+
swapped = df.swapaxes(0, 1)
371+
372+
tm.assert_frame_equal(swapped, df.T)
375373

376374
def test_axis_aliases(self, float_frame):
377375
f = float_frame

0 commit comments

Comments
 (0)