Skip to content

Commit 028dc2c

Browse files
implement any/all reductions
1 parent d3ad7b0 commit 028dc2c

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pandas/core/arrays/string_.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@
4343
pandas_dtype,
4444
)
4545

46-
from pandas.core import ops
46+
from pandas.core import (
47+
nanops,
48+
ops,
49+
)
4750
from pandas.core.array_algos import masked_reductions
4851
from pandas.core.arrays.base import ExtensionArray
4952
from pandas.core.arrays.floating import (
@@ -780,6 +783,17 @@ def _from_backing_data(self, arr: np.ndarray) -> StringArrayNumpySemantics:
780783
# we always preserve the dtype
781784
return NDArrayBacked._from_backing_data(self, arr)
782785

786+
def _reduce(
787+
self, name: str, *, skipna: bool = True, keepdims: bool = False, **kwargs
788+
):
789+
if name in ["any", "all"]:
790+
if name == "any":
791+
return nanops.nanany(self._ndarray, skipna=skipna)
792+
else:
793+
return nanops.nanall(self._ndarray, skipna=skipna)
794+
else:
795+
return super()._reduce(name, skipna=skipna, keepdims=keepdims, **kwargs)
796+
783797
def _wrap_reduction_result(self, axis: AxisInt | None, result) -> Any:
784798
# the masked_reductions use pd.NA
785799
if result is libmissing.NA:

pandas/tests/extension/test_string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def _get_expected_exception(
193193
def _supports_reduction(self, ser: pd.Series, op_name: str) -> bool:
194194
return (
195195
op_name in ["min", "max"]
196-
or (ser.dtype.storage == "pyarrow" and ser.dtype.na_value is np.nan) # type: ignore[union-attr]
196+
or ser.dtype.na_value is np.nan # type: ignore[union-attr]
197197
and op_name in ("any", "all")
198198
)
199199

0 commit comments

Comments
 (0)