From 78ced4c1222b4795d1c6bf7f7f8c24c50cc67fde Mon Sep 17 00:00:00 2001 From: tp Date: Wed, 5 Jun 2019 11:10:02 +0200 Subject: [PATCH 1/3] Deprecate NDFrame.swapaxes --- doc/source/whatsnew/v0.25.0.rst | 2 ++ pandas/core/generic.py | 7 +++++++ pandas/core/groupby/ops.py | 10 +++++----- pandas/tests/frame/test_api.py | 14 ++++++-------- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 4018418294963..6e5313503d227 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -478,6 +478,8 @@ Other Deprecations - The :meth:`DataFrame.compound` and :meth:`Series.compound` methods are deprecated and will be removed in a future version (:issue:`26405`). - The internal attributes ``_start``, ``_stop`` and ``_step`` attributes of :class:`RangeIndex` have been deprecated. Use the public attributes :attr:`~RangeIndex.start`, :attr:`~RangeIndex.stop` and :attr:`~RangeIndex.step` instead (:issue:`26581`). +- The :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes` methods are deprecated and will be removed in a future version. + Use :meth:`DataFrame.transpose` and :meth:`Series.transpose` instead(:issue:`26654`). .. _whatsnew_0250.prior_deprecations: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 19d093dd29457..e7606e527715f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -695,10 +695,17 @@ def swapaxes(self, axis1, axis2, copy=True): """ Interchange axes and swap values axes appropriately. + .. deprecated:: 0.25.0 + Use ``.T`` or ``.transpose()`` instead. + Returns ------- y : same as input """ + warnings.warn("{obj}.swapaxes is deprecated and will be removed " + "in a future version. Use {obj}.T or {obj}.transpose() " + "instead".format(obj=type(self).__name__), + FutureWarning, stacklevel=2) i = self._get_axis_number(axis1) j = self._get_axis_number(axis2) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index ee9d57a537340..761bed8799148 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -468,14 +468,14 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1, arity = self._cython_arity.get(how, 1) vdim = values.ndim - swapped = False + transposed = False if vdim == 1: values = values[:, None] out_shape = (self.ngroups, arity) else: if axis > 0: - swapped = True - values = values.swapaxes(0, axis) + transposed = True + values = values.transpose() if arity > 1: raise NotImplementedError("arity of more than 1 is not " "supported for the 'how' argument") @@ -567,8 +567,8 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1, else: names = None - if swapped: - result = result.swapaxes(0, axis) + if transposed: + result = result.transpose() return result, names diff --git a/pandas/tests/frame/test_api.py b/pandas/tests/frame/test_api.py index ce841b302a037..712d8cc6296df 100644 --- a/pandas/tests/frame/test_api.py +++ b/pandas/tests/frame/test_api.py @@ -363,15 +363,13 @@ def test_transpose(self, float_frame): for col, s in mixed_T.items(): assert s.dtype == np.object_ - def test_swapaxes(self): + def test_swapaxes_deprecated(self): df = self.klass(np.random.randn(10, 5)) - self._assert_frame_equal(df.T, df.swapaxes(0, 1)) - self._assert_frame_equal(df.T, df.swapaxes(1, 0)) - self._assert_frame_equal(df, df.swapaxes(0, 0)) - msg = ("No axis named 2 for object type" - r" ") - with pytest.raises(ValueError, match=msg): - df.swapaxes(2, 5) + + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + swapped = df.swapaxes(0, 1) + + tm.assert_frame_equal(swapped, df.T) def test_axis_aliases(self, float_frame): f = float_frame From 68d5efbe9d4cdeb60e6053c0cc5864ac17427b22 Mon Sep 17 00:00:00 2001 From: tp Date: Wed, 5 Jun 2019 13:15:32 +0200 Subject: [PATCH 2/3] change to pass on np_dev --- pandas/core/internals/blocks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index f86ef40a97299..5b1889e41f805 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1350,6 +1350,7 @@ def func(cond, values, other): # might need to separate out blocks axis = cond.ndim - 1 cond = cond.swapaxes(axis, 0) + mask = np.array([cond[i].all() for i in range(cond.shape[0])], dtype=bool) From 518ef5d097cd37220612ffe253289c594d1a4734 Mon Sep 17 00:00:00 2001 From: tp Date: Wed, 5 Jun 2019 14:31:44 +0200 Subject: [PATCH 3/3] Changes --- doc/source/whatsnew/v0.25.0.rst | 2 +- pandas/tests/generic/test_generic.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 6e5313503d227..f68ab78f78bca 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -479,7 +479,7 @@ Other Deprecations - The internal attributes ``_start``, ``_stop`` and ``_step`` attributes of :class:`RangeIndex` have been deprecated. Use the public attributes :attr:`~RangeIndex.start`, :attr:`~RangeIndex.stop` and :attr:`~RangeIndex.step` instead (:issue:`26581`). - The :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes` methods are deprecated and will be removed in a future version. - Use :meth:`DataFrame.transpose` and :meth:`Series.transpose` instead(:issue:`26654`). + Use :meth:`DataFrame.transpose` and :meth:`Series.transpose` instead (:issue:`26654`). .. _whatsnew_0250.prior_deprecations: diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index e6d9851b1bb99..632c16822cf5a 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -431,7 +431,7 @@ def test_size_compat(self): def test_split_compat(self): # xref GH8846 - o = self._construct(shape=10) + o = self._construct(shape=10).values assert len(np.array_split(o, 5)) == 5 assert len(np.array_split(o, 2)) == 2