Skip to content

Commit

Permalink
REF: refactor ArrowExtensionArray._reduce (#52890)
Browse files Browse the repository at this point in the history
* REF: refactor ArrowExtensionArray._reduce

* fix

* add return type to _reduce_pyarrow
  • Loading branch information
topper-123 authored Apr 25, 2023
1 parent 194b6bb commit c9dbb81
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,9 +1223,9 @@ def _accumulate(

return type(self)(result)

def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
def _reduce_pyarrow(self, name: str, *, skipna: bool = True, **kwargs) -> pa.Scalar:
"""
Return a scalar result of performing the reduction operation.
Return a pyarrow scalar result of performing the reduction operation.
Parameters
----------
Expand All @@ -1241,7 +1241,7 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
Returns
-------
scalar
pyarrow scalar
Raises
------
Expand Down Expand Up @@ -1321,7 +1321,7 @@ def pyarrow_meth(data, skip_nulls, **kwargs):
# GH 52679: Use quantile instead of approximate_median; returns array
result = result[0]
if pc.is_null(result).as_py():
return self.dtype.na_value
return result

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

return result

def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
"""
Return a scalar result of performing the reduction operation.
Parameters
----------
name : str
Name of the function, supported values are:
{ any, all, min, max, sum, mean, median, prod,
std, var, sem, kurt, skew }.
skipna : bool, default True
If True, skip NaN values.
**kwargs
Additional keyword arguments passed to the reduction function.
Currently, `ddof` is the only supported kwarg.
Returns
-------
scalar
Raises
------
TypeError : subclass does not define reductions
"""
result = self._reduce_pyarrow(name, skipna=skipna, **kwargs)

if pc.is_null(result).as_py():
return self.dtype.na_value

return result.as_py()

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

0 comments on commit c9dbb81

Please sign in to comment.