From d8079e4e1b958874a70648ec7bbde78c8be4c330 Mon Sep 17 00:00:00 2001 From: Tristan Marechaux Date: Tue, 18 Apr 2023 15:22:10 +0200 Subject: [PATCH] DEPR: Series.ravel --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/core/dtypes/cast.py | 2 +- pandas/core/series.py | 7 +++++++ pandas/tests/series/test_api.py | 4 +++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 55bdac97d1ac7..4643e795300b8 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -235,6 +235,7 @@ Deprecations - Deprecated :func:`is_interval_dtype`, check ``isinstance(dtype, pd.IntervalDtype)`` instead (:issue:`52607`) - Deprecated :meth:`DataFrame.applymap`. Use the new :meth:`DataFrame.map` method instead (:issue:`52353`) - Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`) +- Deprecated :meth:`Series.ravel` (:issue:`52511`) - Deprecated ``freq`` parameter in :class:`PeriodArray` constructor, pass ``dtype`` instead (:issue:`52462`) - Deprecated behavior of :func:`concat` when :class:`DataFrame` has columns that are all-NA, in a future version these will not be discarded when determining the resulting dtype (:issue:`40893`) - Deprecated behavior of :meth:`Series.dt.to_pydatetime`, in a future version this will return a :class:`Series` containing python ``datetime`` objects instead of an ``ndarray`` of datetimes; this matches the behavior of other :meth:`Series.dt` properties (:issue:`20306`) @@ -247,7 +248,6 @@ Deprecations - Deprecated the methods :meth:`Series.bool` and :meth:`DataFrame.bool` (:issue:`51749`) - Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`52628`) - Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`52628`) -- .. --------------------------------------------------------------------------- .. _whatsnew_210.performance: diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 3929775283f6a..bc28137baa564 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -351,7 +351,7 @@ def trans(x): return trans(result).astype(dtype) # do a test on the first element, if it fails then we are done - r = result.ravel() + r = np.ravel(result) arr = np.array([r[0]]) if isna(arr).any(): diff --git a/pandas/core/series.py b/pandas/core/series.py index 43ebdc2f9dff0..9adc33c3a3bfa 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -771,6 +771,13 @@ def ravel(self, order: str = "C") -> ArrayLike: -------- numpy.ndarray.ravel : Return a flattened array. """ + warnings.warn( + # GH#52511 + f"'{type(self).__name__}.ravel' is deprecated and will be removed in a " + f"future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) arr = self._values.ravel(order=order) if isinstance(arr, np.ndarray) and using_copy_on_write(): arr.flags.writeable = False diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index e4e276af121f9..904fe2f2d4eca 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -142,7 +142,9 @@ def test_ndarray_compat_like_func(self): def test_ndarray_compat_ravel(self): # ravel s = Series(np.random.randn(10)) - tm.assert_almost_equal(s.ravel(order="F"), s.values.ravel(order="F")) + msg = "'Series.ravel' is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + tm.assert_almost_equal(s.ravel(order="F"), s.values.ravel(order="F")) def test_empty_method(self): s_empty = Series(dtype=object)