Skip to content

REF: refactor ArrowExtensionArray._reduce #52890

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

Merged
merged 3 commits into from
Apr 25, 2023
Merged
Changes from all 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
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