Skip to content

Commit cb56347

Browse files
authored
DEPR: type argument in Index.view (#56421)
1 parent 8614088 commit cb56347

File tree

7 files changed

+27
-6
lines changed

7 files changed

+27
-6
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ Other Deprecations
438438
- Deprecated :meth:`Series.view`, use :meth:`Series.astype` instead to change the dtype (:issue:`20251`)
439439
- Deprecated ``core.internals`` members ``Block``, ``ExtensionBlock``, and ``DatetimeTZBlock``, use public APIs instead (:issue:`55139`)
440440
- Deprecated ``year``, ``month``, ``quarter``, ``day``, ``hour``, ``minute``, and ``second`` keywords in the :class:`PeriodIndex` constructor, use :meth:`PeriodIndex.from_fields` instead (:issue:`55960`)
441+
- Deprecated accepting a type as an argument in :meth:`Index.view`, call without any arguments instead (:issue:`55709`)
441442
- Deprecated allowing non-integer ``periods`` argument in :func:`date_range`, :func:`timedelta_range`, :func:`period_range`, and :func:`interval_range` (:issue:`56036`)
442443
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_clipboard`. (:issue:`54229`)
443444
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_csv` except ``path_or_buf``. (:issue:`54229`)

pandas/core/indexes/base.py

+10
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,16 @@ def view(self, cls=None):
10151015

10161016
result = self._data.view(cls)
10171017
else:
1018+
if cls is not None:
1019+
warnings.warn(
1020+
# GH#55709
1021+
f"Passing a type in {type(self).__name__}.view is deprecated "
1022+
"and will raise in a future version. "
1023+
"Call view without any argument to retain the old behavior.",
1024+
FutureWarning,
1025+
stacklevel=find_stack_level(),
1026+
)
1027+
10181028
result = self._view()
10191029
if isinstance(result, Index):
10201030
result._id = self._id

pandas/tests/indexes/datetimes/test_setops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ def test_intersection(self):
535535
assert isinstance(the_int, DatetimeIndex)
536536
assert the_int.freq == rng.freq
537537

538-
the_int = rng1.intersection(rng2.view(DatetimeIndex))
538+
the_int = rng1.intersection(rng2)
539539
tm.assert_index_equal(the_int, expected)
540540

541541
# non-overlapping

pandas/tests/indexes/numeric/test_numeric.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ def test_cant_or_shouldnt_cast(self, dtype):
318318

319319
def test_view_index(self, simple_index):
320320
index = simple_index
321-
index.view(Index)
321+
msg = "Passing a type in .*Index.view is deprecated"
322+
with tm.assert_produces_warning(FutureWarning, match=msg):
323+
index.view(Index)
322324

323325
def test_prevent_casting(self, simple_index):
324326
index = simple_index

pandas/tests/indexes/ranges/test_range.py

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

202-
i_view = i.view(RangeIndex)
202+
msg = "Passing a type in RangeIndex.view is deprecated"
203+
with tm.assert_produces_warning(FutureWarning, match=msg):
204+
i_view = i.view(RangeIndex)
203205
tm.assert_index_equal(i, i_view)
204206

205207
def test_dtype(self, simple_index):
@@ -382,7 +384,9 @@ def test_cant_or_shouldnt_cast(self, start, stop, step):
382384

383385
def test_view_index(self, simple_index):
384386
index = simple_index
385-
index.view(Index)
387+
msg = "Passing a type in RangeIndex.view is deprecated"
388+
with tm.assert_produces_warning(FutureWarning, match=msg):
389+
index.view(Index)
386390

387391
def test_prevent_casting(self, simple_index):
388392
index = simple_index

pandas/tests/indexes/test_datetimelike.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ def test_view(self, simple_index):
8989
result = type(simple_index)(idx)
9090
tm.assert_index_equal(result, idx)
9191

92-
idx_view = idx.view(type(simple_index))
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))
9395
result = type(simple_index)(idx)
9496
tm.assert_index_equal(result, idx_view)
9597

pandas/tests/indexes/test_old_base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,9 @@ def test_view(self, simple_index):
960960
idx_view = idx.view(dtype)
961961
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True)
962962

963-
idx_view = idx.view(index_cls)
963+
msg = "Passing a type in .*Index.view is deprecated"
964+
with tm.assert_produces_warning(FutureWarning, match=msg):
965+
idx_view = idx.view(index_cls)
964966
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True)
965967

966968
def test_format(self, simple_index):

0 commit comments

Comments
 (0)