Skip to content

DEPR: passing pandas type to Index.view #58047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
12 changes: 1 addition & 11 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/numeric/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 2 additions & 7 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 3 additions & 6 deletions pandas/tests/indexes/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/test_old_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading