Skip to content

Commit 6c301c6

Browse files
authored
DEPR: passing pandas type to Index.view (#58047)
1 parent e45057a commit 6c301c6

File tree

6 files changed

+13
-30
lines changed

6 files changed

+13
-30
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ Removal of prior version deprecations/changes
206206
- :meth:`SeriesGroupBy.agg` no longer pins the name of the group to the input passed to the provided ``func`` (:issue:`51703`)
207207
- All arguments except ``name`` in :meth:`Index.rename` are now keyword only (:issue:`56493`)
208208
- All arguments except the first ``path``-like argument in IO writers are now keyword only (:issue:`54229`)
209+
- Disallow passing a pandas type to :meth:`Index.view` (:issue:`55709`)
209210
- Removed "freq" keyword from :class:`PeriodArray` constructor, use "dtype" instead (:issue:`52462`)
210211
- Removed deprecated "method" and "limit" keywords from :meth:`Series.replace` and :meth:`DataFrame.replace` (:issue:`53492`)
211212
- Removed the "closed" and "normalize" keywords in :meth:`DatetimeIndex.__new__` (:issue:`52628`)

pandas/core/indexes/base.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ def ravel(self, order: str_t = "C") -> Self:
10131013
def view(self, cls=None):
10141014
# we need to see if we are subclassing an
10151015
# index type here
1016-
if cls is not None and not hasattr(cls, "_typ"):
1016+
if cls is not None:
10171017
dtype = cls
10181018
if isinstance(cls, str):
10191019
dtype = pandas_dtype(cls)
@@ -1030,16 +1030,6 @@ def view(self, cls=None):
10301030

10311031
result = self._data.view(cls)
10321032
else:
1033-
if cls is not None:
1034-
warnings.warn(
1035-
# GH#55709
1036-
f"Passing a type in {type(self).__name__}.view is deprecated "
1037-
"and will raise in a future version. "
1038-
"Call view without any argument to retain the old behavior.",
1039-
FutureWarning,
1040-
stacklevel=find_stack_level(),
1041-
)
1042-
10431033
result = self._view()
10441034
if isinstance(result, Index):
10451035
result._id = self._id

pandas/tests/indexes/numeric/test_numeric.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ def test_cant_or_shouldnt_cast(self, dtype):
312312

313313
def test_view_index(self, simple_index):
314314
index = simple_index
315-
msg = "Passing a type in .*Index.view is deprecated"
316-
with tm.assert_produces_warning(FutureWarning, match=msg):
315+
msg = "Cannot change data-type for object array"
316+
with pytest.raises(TypeError, match=msg):
317317
index.view(Index)
318318

319319
def test_prevent_casting(self, simple_index):

pandas/tests/indexes/ranges/test_range.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,6 @@ def test_view(self):
200200
i_view = i.view("i8")
201201
tm.assert_numpy_array_equal(i.values, i_view)
202202

203-
msg = "Passing a type in RangeIndex.view is deprecated"
204-
with tm.assert_produces_warning(FutureWarning, match=msg):
205-
i_view = i.view(RangeIndex)
206-
tm.assert_index_equal(i, i_view)
207-
208203
def test_dtype(self, simple_index):
209204
index = simple_index
210205
assert index.dtype == np.int64
@@ -380,8 +375,8 @@ def test_cant_or_shouldnt_cast(self, start, stop, step):
380375

381376
def test_view_index(self, simple_index):
382377
index = simple_index
383-
msg = "Passing a type in RangeIndex.view is deprecated"
384-
with tm.assert_produces_warning(FutureWarning, match=msg):
378+
msg = "Cannot change data-type for object array"
379+
with pytest.raises(TypeError, match=msg):
385380
index.view(Index)
386381

387382
def test_prevent_casting(self, simple_index):

pandas/tests/indexes/test_datetimelike.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,12 @@ def test_str(self, simple_index):
8585
def test_view(self, simple_index):
8686
idx = simple_index
8787

88-
idx_view = idx.view("i8")
8988
result = type(simple_index)(idx)
9089
tm.assert_index_equal(result, idx)
9190

92-
msg = "Passing a type in .*Index.view is deprecated"
93-
with tm.assert_produces_warning(FutureWarning, match=msg):
94-
idx_view = idx.view(type(simple_index))
95-
result = type(simple_index)(idx)
96-
tm.assert_index_equal(result, idx_view)
91+
msg = "Cannot change data-type for object array"
92+
with pytest.raises(TypeError, match=msg):
93+
idx.view(type(simple_index))
9794

9895
def test_map_callable(self, simple_index):
9996
index = simple_index

pandas/tests/indexes/test_old_base.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -891,10 +891,10 @@ def test_view(self, simple_index):
891891
idx_view = idx.view(dtype)
892892
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True)
893893

894-
msg = "Passing a type in .*Index.view is deprecated"
895-
with tm.assert_produces_warning(FutureWarning, match=msg):
896-
idx_view = idx.view(index_cls)
897-
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True)
894+
msg = "Cannot change data-type for object array"
895+
with pytest.raises(TypeError, match=msg):
896+
# GH#55709
897+
idx.view(index_cls)
898898

899899
def test_insert_non_na(self, simple_index):
900900
# GH#43921 inserting an element that we know we can hold should

0 commit comments

Comments
 (0)