diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index e798f19bc1a23..d770054da5040 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -206,6 +206,7 @@ Removal of prior version deprecations/changes - :meth:`SeriesGroupBy.agg` no longer pins the name of the group to the input passed to the provided ``func`` (:issue:`51703`) - All arguments except ``name`` in :meth:`Index.rename` are now keyword only (:issue:`56493`) - All arguments except the first ``path``-like argument in IO writers are now keyword only (:issue:`54229`) +- Disallow passing a pandas type to :meth:`Index.view` (:issue:`55709`) - Removed "freq" keyword from :class:`PeriodArray` constructor, use "dtype" instead (:issue:`52462`) - Removed deprecated "method" and "limit" keywords from :meth:`Series.replace` and :meth:`DataFrame.replace` (:issue:`53492`) - Removed the "closed" and "normalize" keywords in :meth:`DatetimeIndex.__new__` (:issue:`52628`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index e510d487ac954..ae9b0eb4e48aa 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1013,7 +1013,7 @@ def ravel(self, order: str_t = "C") -> Self: def view(self, cls=None): # we need to see if we are subclassing an # index type here - if cls is not None and not hasattr(cls, "_typ"): + if cls is not None: dtype = cls if isinstance(cls, str): dtype = pandas_dtype(cls) @@ -1030,16 +1030,6 @@ def view(self, cls=None): result = self._data.view(cls) else: - if cls is not None: - warnings.warn( - # GH#55709 - f"Passing a type in {type(self).__name__}.view is deprecated " - "and will raise in a future version. " - "Call view without any argument to retain the old behavior.", - FutureWarning, - stacklevel=find_stack_level(), - ) - result = self._view() if isinstance(result, Index): result._id = self._id diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index 4416034795463..088fcfcd7d75f 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -312,8 +312,8 @@ def test_cant_or_shouldnt_cast(self, dtype): def test_view_index(self, simple_index): index = simple_index - msg = "Passing a type in .*Index.view is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + msg = "Cannot change data-type for object array" + with pytest.raises(TypeError, match=msg): index.view(Index) def test_prevent_casting(self, simple_index): diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index c9ddbf4464b29..8d41efa586411 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -200,11 +200,6 @@ def test_view(self): i_view = i.view("i8") tm.assert_numpy_array_equal(i.values, i_view) - msg = "Passing a type in RangeIndex.view is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - i_view = i.view(RangeIndex) - tm.assert_index_equal(i, i_view) - def test_dtype(self, simple_index): index = simple_index assert index.dtype == np.int64 @@ -380,8 +375,8 @@ def test_cant_or_shouldnt_cast(self, start, stop, step): def test_view_index(self, simple_index): index = simple_index - msg = "Passing a type in RangeIndex.view is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): + msg = "Cannot change data-type for object array" + with pytest.raises(TypeError, match=msg): index.view(Index) def test_prevent_casting(self, simple_index): diff --git a/pandas/tests/indexes/test_datetimelike.py b/pandas/tests/indexes/test_datetimelike.py index 7ec73070836b8..0ad5888a44392 100644 --- a/pandas/tests/indexes/test_datetimelike.py +++ b/pandas/tests/indexes/test_datetimelike.py @@ -85,15 +85,12 @@ def test_str(self, simple_index): def test_view(self, simple_index): idx = simple_index - idx_view = idx.view("i8") result = type(simple_index)(idx) tm.assert_index_equal(result, idx) - msg = "Passing a type in .*Index.view is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - idx_view = idx.view(type(simple_index)) - result = type(simple_index)(idx) - tm.assert_index_equal(result, idx_view) + msg = "Cannot change data-type for object array" + with pytest.raises(TypeError, match=msg): + idx.view(type(simple_index)) def test_map_callable(self, simple_index): index = simple_index diff --git a/pandas/tests/indexes/test_old_base.py b/pandas/tests/indexes/test_old_base.py index 85eec7b7c018d..d4dc248b39dc5 100644 --- a/pandas/tests/indexes/test_old_base.py +++ b/pandas/tests/indexes/test_old_base.py @@ -891,10 +891,10 @@ def test_view(self, simple_index): idx_view = idx.view(dtype) tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True) - msg = "Passing a type in .*Index.view is deprecated" - with tm.assert_produces_warning(FutureWarning, match=msg): - idx_view = idx.view(index_cls) - tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True) + msg = "Cannot change data-type for object array" + with pytest.raises(TypeError, match=msg): + # GH#55709 + idx.view(index_cls) def test_insert_non_na(self, simple_index): # GH#43921 inserting an element that we know we can hold should