From 605cc0b8d08fb68fcf0d58fadb6aeaef10061340 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 5 Oct 2020 20:40:18 -0700 Subject: [PATCH] DEPR: Index.ravel returning an ndarray (#36900) --- doc/source/whatsnew/v1.2.0.rst | 1 + pandas/core/indexes/base.py | 6 ++++++ pandas/io/formats/format.py | 3 ++- pandas/tests/indexes/test_common.py | 5 +++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 95628350ad998..7dd3eb51bcaeb 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -268,6 +268,7 @@ Deprecations - Deprecated :meth:`Index.is_all_dates` (:issue:`27744`) - Deprecated automatic alignment on comparison operations between :class:`DataFrame` and :class:`Series`, do ``frame, ser = frame.align(ser, axis=1, copy=False)`` before e.g. ``frame == ser`` (:issue:`28759`) - :meth:`Rolling.count` with ``min_periods=None`` will default to the size of the window in a future version (:issue:`31302`) +- :meth:`Index.ravel` returning a ``np.ndarray`` is deprecated, in the future this will return a view on the same index (:issue:`19956`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ff3d8bf05f9a5..d603797370ce3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -659,6 +659,12 @@ def ravel(self, order="C"): -------- numpy.ndarray.ravel """ + warnings.warn( + "Index.ravel returning ndarray is deprecated; in a future version " + "this will return a view on self.", + FutureWarning, + stacklevel=2, + ) values = self._get_engine_target() return values.ravel(order=order) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index b9d41f142c2b5..13010bb2ef147 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1677,7 +1677,8 @@ def is_dates_only( values: Union[np.ndarray, DatetimeArray, Index, DatetimeIndex] ) -> bool: # return a boolean if we are only dates (and don't have a timezone) - values = values.ravel() + if not isinstance(values, Index): + values = values.ravel() values = DatetimeIndex(values) if values.tz is not None: diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 675ae388a28a4..e2dea7828b3ad 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -399,6 +399,11 @@ def test_astype_preserves_name(self, index, dtype): else: assert result.name == index.name + def test_ravel_deprecation(self, index): + # GH#19956 ravel returning ndarray is deprecated + with tm.assert_produces_warning(FutureWarning): + index.ravel() + @pytest.mark.parametrize("na_position", [None, "middle"]) def test_sort_values_invalid_na_position(index_with_missing, na_position):