Skip to content

Commit b196c06

Browse files
topper-123Yi Wei
authored and
Yi Wei
committed
REF: refactor ArrowExtensionArray._reduce (pandas-dev#52890)
* REF: refactor ArrowExtensionArray._reduce * fix * add return type to _reduce_pyarrow
1 parent fd85f6d commit b196c06

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

pandas/core/arrays/arrow/array.py

+35-4
Original file line numberDiff line numberDiff line change
@@ -1223,9 +1223,9 @@ def _accumulate(
12231223

12241224
return type(self)(result)
12251225

1226-
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
1226+
def _reduce_pyarrow(self, name: str, *, skipna: bool = True, **kwargs) -> pa.Scalar:
12271227
"""
1228-
Return a scalar result of performing the reduction operation.
1228+
Return a pyarrow scalar result of performing the reduction operation.
12291229
12301230
Parameters
12311231
----------
@@ -1241,7 +1241,7 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
12411241
12421242
Returns
12431243
-------
1244-
scalar
1244+
pyarrow scalar
12451245
12461246
Raises
12471247
------
@@ -1321,7 +1321,7 @@ def pyarrow_meth(data, skip_nulls, **kwargs):
13211321
# GH 52679: Use quantile instead of approximate_median; returns array
13221322
result = result[0]
13231323
if pc.is_null(result).as_py():
1324-
return self.dtype.na_value
1324+
return result
13251325

13261326
if name in ["min", "max", "sum"] and pa.types.is_duration(pa_type):
13271327
result = result.cast(pa_type)
@@ -1341,6 +1341,37 @@ def pyarrow_meth(data, skip_nulls, **kwargs):
13411341
# i.e. timestamp
13421342
result = result.cast(pa.duration(pa_type.unit))
13431343

1344+
return result
1345+
1346+
def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
1347+
"""
1348+
Return a scalar result of performing the reduction operation.
1349+
1350+
Parameters
1351+
----------
1352+
name : str
1353+
Name of the function, supported values are:
1354+
{ any, all, min, max, sum, mean, median, prod,
1355+
std, var, sem, kurt, skew }.
1356+
skipna : bool, default True
1357+
If True, skip NaN values.
1358+
**kwargs
1359+
Additional keyword arguments passed to the reduction function.
1360+
Currently, `ddof` is the only supported kwarg.
1361+
1362+
Returns
1363+
-------
1364+
scalar
1365+
1366+
Raises
1367+
------
1368+
TypeError : subclass does not define reductions
1369+
"""
1370+
result = self._reduce_pyarrow(name, skipna=skipna, **kwargs)
1371+
1372+
if pc.is_null(result).as_py():
1373+
return self.dtype.na_value
1374+
13441375
return result.as_py()
13451376

13461377
def __setitem__(self, key, value) -> None:

0 commit comments

Comments
 (0)