Skip to content

Commit

Permalink
DEPR: type argument in Index.view (#56421)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Dec 9, 2023
1 parent 8614088 commit cb56347
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ Other Deprecations
- Deprecated :meth:`Series.view`, use :meth:`Series.astype` instead to change the dtype (:issue:`20251`)
- Deprecated ``core.internals`` members ``Block``, ``ExtensionBlock``, and ``DatetimeTZBlock``, use public APIs instead (:issue:`55139`)
- Deprecated ``year``, ``month``, ``quarter``, ``day``, ``hour``, ``minute``, and ``second`` keywords in the :class:`PeriodIndex` constructor, use :meth:`PeriodIndex.from_fields` instead (:issue:`55960`)
- Deprecated accepting a type as an argument in :meth:`Index.view`, call without any arguments instead (:issue:`55709`)
- Deprecated allowing non-integer ``periods`` argument in :func:`date_range`, :func:`timedelta_range`, :func:`period_range`, and :func:`interval_range` (:issue:`56036`)
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_clipboard`. (:issue:`54229`)
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_csv` except ``path_or_buf``. (:issue:`54229`)
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,16 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def test_intersection(self):
assert isinstance(the_int, DatetimeIndex)
assert the_int.freq == rng.freq

the_int = rng1.intersection(rng2.view(DatetimeIndex))
the_int = rng1.intersection(rng2)
tm.assert_index_equal(the_int, expected)

# non-overlapping
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexes/numeric/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ def test_cant_or_shouldnt_cast(self, dtype):

def test_view_index(self, simple_index):
index = simple_index
index.view(Index)
msg = "Passing a type in .*Index.view is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
index.view(Index)

def test_prevent_casting(self, simple_index):
index = simple_index
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ def test_view(self):
i_view = i.view("i8")
tm.assert_numpy_array_equal(i.values, i_view)

i_view = i.view(RangeIndex)
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):
Expand Down Expand Up @@ -382,7 +384,9 @@ def test_cant_or_shouldnt_cast(self, start, stop, step):

def test_view_index(self, simple_index):
index = simple_index
index.view(Index)
msg = "Passing a type in RangeIndex.view is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
index.view(Index)

def test_prevent_casting(self, simple_index):
index = simple_index
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexes/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def test_view(self, simple_index):
result = type(simple_index)(idx)
tm.assert_index_equal(result, idx)

idx_view = idx.view(type(simple_index))
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)

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexes/test_old_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,9 @@ def test_view(self, simple_index):
idx_view = idx.view(dtype)
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True)

idx_view = idx.view(index_cls)
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)

def test_format(self, simple_index):
Expand Down

0 comments on commit cb56347

Please sign in to comment.