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 13 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
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ def pyarrow_meth(data, skip_nulls, **kwargs):
except (AttributeError, NotImplementedError, TypeError) as err:
msg = (
f"'{type(self).__name__}' with dtype {self.dtype} "
f"does not support reduction '{name}' with pyarrow "
f"does not support operation '{name}' with pyarrow "
f"version {pa.__version__}. '{name}' may be supported by "
f"upgrading pyarrow."
)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,7 @@ def _reduce(

Raises
------
TypeError : subclass does not define reductions
TypeError : subclass does not define operations

Examples
--------
Expand All @@ -1897,7 +1897,7 @@ def _reduce(
if meth is None:
raise TypeError(
f"'{type(self).__name__}' with dtype {self.dtype} "
f"does not support reduction '{name}'"
f"does not support operation '{name}'"
)
result = meth(skipna=skipna, **kwargs)
if keepdims:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ def _groupby_op(
if dtype.kind == "M":
# Adding/multiplying datetimes is not valid
if how in ["any", "all", "sum", "prod", "cumsum", "cumprod", "var", "skew"]:
raise TypeError(f"datetime64 type does not support operation: '{how}'")
raise TypeError(f"datetime64 type does not support operation '{how}'")

elif isinstance(dtype, PeriodDtype):
# Adding/multiplying Periods is not valid
Expand Down
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
4 changes: 2 additions & 2 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def nanany(

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

values, _ = _get_values(values, skipna, fill_value=False, mask=mask)

Expand Down Expand Up @@ -576,7 +576,7 @@ def nanall(

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

values, _ = _get_values(values, skipna, fill_value=True, mask=mask)

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ def test_agg_multiple_mixed_raises():
)

# sorted index
msg = "does not support reduction"
msg = "does not support operation"
with pytest.raises(TypeError, match=msg):
mdf.agg(["min", "sum"])

Expand Down Expand Up @@ -1309,15 +1309,15 @@ def test_nuiscance_columns():
)
tm.assert_frame_equal(result, expected)

msg = "does not support reduction"
msg = "does not support operation"
with pytest.raises(TypeError, match=msg):
df.agg("sum")

result = df[["A", "B", "C"]].agg("sum")
expected = Series([6, 6.0, "foobarbaz"], index=["A", "B", "C"])
tm.assert_series_equal(result, expected)

msg = "does not support reduction"
msg = "does not support operation"
with pytest.raises(TypeError, match=msg):
df.agg(["sum"])

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,14 @@ def test_numeric_like_ops(self):
# min/max)
s = df["value_group"]
for op in ["kurt", "skew", "var", "std", "mean", "sum", "median"]:
msg = f"does not support reduction '{op}'"
msg = f"does not support operation '{op}'"
with pytest.raises(TypeError, match=msg):
getattr(s, op)(numeric_only=False)

def test_numeric_like_ops_series(self):
# numpy ops
s = Series(Categorical([1, 2, 3, 4]))
with pytest.raises(TypeError, match="does not support reduction 'sum'"):
with pytest.raises(TypeError, match="does not support operation 'sum'"):
np.sum(s)

@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def test_scalar_from_string(self, arr1d):
assert result == arr1d[0]

def test_reduce_invalid(self, arr1d):
msg = "does not support reduction 'not a method'"
msg = "does not support operation 'not a method'"
with pytest.raises(TypeError, match=msg):
arr1d._reduce("not a method")

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/base/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_in_numeric_groupby(self, data_for_grouping):
# period
"does not support sum operations",
# datetime
"does not support operation: 'sum'",
"does not support operation 'sum'",
# all others
re.escape(f"agg function failed [how->sum,dtype->{dtype}"),
]
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/extension/base/reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna):
# TODO: the message being checked here isn't actually checking anything
msg = (
"[Cc]annot perform|Categorical is not ordered for operation|"
"does not support reduction|"
"does not support operation|"
)

with pytest.raises(TypeError, match=msg):
Expand All @@ -105,7 +105,7 @@ def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna):
# TODO: the message being checked here isn't actually checking anything
msg = (
"[Cc]annot perform|Categorical is not ordered for operation|"
"does not support reduction|"
"does not support operation|"
)

with pytest.raises(TypeError, match=msg):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _supports_reduction(self, obj, op_name: str) -> bool:
@pytest.mark.parametrize("skipna", [True, False])
def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna):
meth = all_boolean_reductions
msg = f"datetime64 type does not support operation: '{meth}'"
msg = f"datetime64 type does not support operation '{meth}'"
with pytest.raises(TypeError, match=msg):
super().test_reduce_series_boolean(data, all_boolean_reductions, skipna)

Expand Down
32 changes: 16 additions & 16 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ def test_sum_mixed_datetime(self):
df = DataFrame({"A": date_range("2000", periods=4), "B": [1, 2, 3, 4]}).reindex(
[2, 3, 4]
)
with pytest.raises(TypeError, match="does not support reduction 'sum'"):
with pytest.raises(TypeError, match="does not support operation 'sum'"):
df.sum()

def test_mean_corner(self, float_frame, float_string_frame):
Expand Down Expand Up @@ -1381,7 +1381,7 @@ def test_any_datetime(self):
]
df = DataFrame({"A": float_data, "B": datetime_data})

msg = "datetime64 type does not support operation: 'any'"
msg = "datetime64 type does not support operation 'any'"
with pytest.raises(TypeError, match=msg):
df.any(axis=1)

Expand Down Expand Up @@ -1466,18 +1466,18 @@ def test_any_all_np_func(self, func, data, expected):

if any(isinstance(x, CategoricalDtype) for x in data.dtypes):
with pytest.raises(
TypeError, match="dtype category does not support reduction"
TypeError, match=".* dtype category does not support operation"
):
func(data)

# method version
with pytest.raises(
TypeError, match="dtype category does not support reduction"
TypeError, match=".* dtype category does not support operation"
):
getattr(DataFrame(data), func.__name__)(axis=None)
if data.dtypes.apply(lambda x: x.kind == "M").any():
# GH#34479
msg = "datetime64 type does not support operation: '(any|all)'"
msg = "datetime64 type does not support operation '(any|all)'"
with pytest.raises(TypeError, match=msg):
func(data)

Expand Down Expand Up @@ -1734,19 +1734,19 @@ def test_any_all_categorical_dtype_nuisance_column(self, all_boolean_reductions)
df = ser.to_frame()

# Double-check the Series behavior is to raise
with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
getattr(ser, all_boolean_reductions)()

with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
getattr(np, all_boolean_reductions)(ser)

with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
getattr(df, all_boolean_reductions)(bool_only=False)

with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
getattr(df, all_boolean_reductions)(bool_only=None)

with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
getattr(np, all_boolean_reductions)(df, axis=0)

def test_median_categorical_dtype_nuisance_column(self):
Expand All @@ -1755,22 +1755,22 @@ def test_median_categorical_dtype_nuisance_column(self):
ser = df["A"]

# Double-check the Series behavior is to raise
with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
ser.median()

with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
df.median(numeric_only=False)

with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
df.median()

# same thing, but with an additional non-categorical column
df["B"] = df["A"].astype(int)

with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
df.median(numeric_only=False)

with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
df.median()

# TODO: np.median(df, axis=0) gives np.array([2.0, 2.0]) instead
Expand Down Expand Up @@ -1964,7 +1964,7 @@ def test_minmax_extensionarray(method, numeric_only):
def test_frame_mixed_numeric_object_with_timestamp(ts_value):
# GH 13912
df = DataFrame({"a": [1], "b": [1.1], "c": ["foo"], "d": [ts_value]})
with pytest.raises(TypeError, match="does not support reduction"):
with pytest.raises(TypeError, match="does not support operation"):
df.sum()


Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def test_raises_on_nuisance(df):
df = df.loc[:, ["A", "C", "D"]]
df["E"] = datetime.now()
grouped = df.groupby("A")
msg = "datetime64 type does not support operation: 'sum'"
msg = "datetime64 type does not support operation 'sum'"
with pytest.raises(TypeError, match=msg):
grouped.agg("sum")
with pytest.raises(TypeError, match=msg):
Expand Down Expand Up @@ -1794,7 +1794,7 @@ def get_categorical_invalid_expected():
else:
msg = "category type does not support"
if op == "skew":
msg = "|".join([msg, "does not support reduction 'skew'"])
msg = "|".join([msg, "does not support operation 'skew'"])
with pytest.raises(TypeError, match=msg):
get_result()

Expand Down
Loading