Skip to content
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

CLN: remove unnecessary check needs_i8_conversion if Index subclass does not support any or all #58006

Merged
Show file tree
Hide file tree
Changes from 11 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
11 changes: 2 additions & 9 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@
)
from pandas.core.missing import clean_reindex_fill_method
from pandas.core.ops import get_op_result_name
from pandas.core.ops.invalid import make_invalid_op
from pandas.core.sorting import (
ensure_key_mapped,
get_group_index_sorter,
Expand Down Expand Up @@ -6938,14 +6937,8 @@ def _maybe_disable_logical_methods(self, opname: str_t) -> None:
"""
raise if this Index subclass does not support any or all.
"""
if (
isinstance(self, ABCMultiIndex)
# TODO(3.0): PeriodArray and DatetimeArray any/all will raise,
# so checking needs_i8_conversion will be unnecessary
or (needs_i8_conversion(self.dtype) and self.dtype.kind != "m")
):
# This call will raise
make_invalid_op(opname)(self)
if isinstance(self, ABCMultiIndex):
raise TypeError(f"cannot perform {opname} with {type(self).__name__}")

@Appender(IndexOpsMixin.argmin.__doc__)
def argmin(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
Expand Down
9 changes: 2 additions & 7 deletions pandas/tests/indexes/test_old_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def test_numeric_compat(self, simple_index):
1 // idx

def test_logical_compat(self, simple_index):
if simple_index.dtype in (object, "string"):
if simple_index.dtype in (object, "string", "datetime64[ns]"):
Copy link
Member

Choose a reason for hiding this comment

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

Is the datetime skip required here? I am not overly familiar with this part of the code base but judging by the deleted comment I didn't think this needed to change

Copy link
Contributor Author

@natmokval natmokval Apr 2, 2024

Choose a reason for hiding this comment

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

I think we shouldn't skip datetime64 here. We need the separate branch for datetime64 because we get different error message: "datetime64 type does not support operation: '(any|all)'"

If we replace the error message

if values.dtype.kind == "M":
# GH#34479
raise TypeError("datetime64 type does not support operation: 'any'")

with the msg "does not support reduction '(any|all)'" we can combine these two checks. Can I do it in this PR or a new one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suggest to align the error messages and assign msg = “.* does not support operation .*“ . I replaced reduction with operation and operation: with operation to unify error messages. Tests passed.

pytest.skip("Tested elsewhere.")
idx = simple_index
if idx.dtype.kind in "iufcbm":
Expand All @@ -222,12 +222,7 @@ def test_logical_compat(self, simple_index):
assert idx.any() == idx._values.any()
assert idx.any() == idx.to_series().any()
else:
msg = "cannot perform (any|all)"
if isinstance(idx, IntervalIndex):
msg = (
r"'IntervalArray' with dtype interval\[.*\] does "
"not support reduction '(any|all)'"
)
msg = "does not support reduction '(any|all)'"
with pytest.raises(TypeError, match=msg):
idx.all()
with pytest.raises(TypeError, match=msg):
Expand Down