From 1791f756e7e866aabd0bec87c2221df09532ad53 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 5 Oct 2020 13:41:16 -0700 Subject: [PATCH 1/2] DEPR: Index.ravel returning an ndarray --- doc/source/whatsnew/v1.2.0.rst | 1 + pandas/core/indexes/base.py | 6 ++++++ pandas/tests/indexes/test_common.py | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 031c74b1cc367..56b3b2d1bad70 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -214,6 +214,7 @@ Deprecations - :meth:`DataFrame.lookup` is deprecated and will be removed in a future version, use :meth:`DataFrame.melt` and :meth:`DataFrame.loc` instead (:issue:`18682`) - The :meth:`Index.to_native_types` is deprecated. Use ``.astype(str)`` instead (:issue:`28867`) - Deprecated indexing :class:`DataFrame` rows with datetime-like strings ``df[string]``, use ``df.loc[string]`` instead (:issue:`36179`) +- :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 84489c1033d8c..ef13b04f12b95 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/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): From e0ea962692bc989c571e39e24a4638c11f833597 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 5 Oct 2020 15:04:35 -0700 Subject: [PATCH 2/2] docbuild fixup --- pandas/io/formats/format.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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: