Skip to content

DEPR: type argument in Index.view #56421

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
Dec 9, 2023
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/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 @@ -1012,6 +1012,16 @@ def view(self, cls=None):

result = self._data.view(cls)
else:
if cls is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still a if cls is not None and not hasattr(cls, "_typ"): branch above, should passing cls for those cases also be deprecated?
(otherwise we still can't remove the keyword after the current deprecation of this PR is enforced?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deprecated passing a type index.view(pd.DatetimeIndex), didnt handle passing a dtype

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see. But do you know of any usage internally with a dtype?
I know we use it regularly without any argument to get an "identical" object (that passes is_), but I don't directly find much usage with a dtype.

From a quick look (outside of direct tests for this), I only found CategoricalDtype._hash_categories that uses it, where it could easily be avoided.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id be fine with follow-up(s) to deprecate more/all of this

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