Skip to content

ENH: support min/max/sum for pyarrow duration dtypes #50928

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 2 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,9 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
not_eq = pc.not_equal(data_to_cmp, 0)
data_to_reduce = not_eq

elif name in ["min", "max", "sum"] and pa.types.is_duration(pa_type):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you open up a pyarrow issue to see if they would support min/max/sum for duration types in the future?

data_to_reduce = self._data.cast(pa.int64())

if name == "sem":

def pyarrow_meth(data, skip_nulls, **kwargs):
Expand Down Expand Up @@ -1066,6 +1069,9 @@ def pyarrow_meth(data, skip_nulls, **kwargs):
raise TypeError(msg) from err
if pc.is_null(result).as_py():
return self.dtype.na_value

if name in ["min", "max", "sum"] and pa.types.is_duration(pa_type):
result = result.cast(pa_type)
return result.as_py()

def __setitem__(self, key, value) -> None:
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/extension/base/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def test_in_numeric_groupby(self, data_for_grouping):
or is_string_dtype(dtype)
or is_period_dtype(dtype)
or is_object_dtype(dtype)
or dtype.kind == "m" # in particular duration[*][pyarrow]
):
expected = pd.Index(["B", "C"])
result = df.groupby("A").sum().columns
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,6 @@ def test_reduce_series(self, data, all_numeric_reductions, skipna, request):
"sem",
] and pa.types.is_temporal(pa_dtype):
request.node.add_marker(xfail_mark)
elif all_numeric_reductions in ["sum", "min", "max"] and pa.types.is_duration(
pa_dtype
):
request.node.add_marker(xfail_mark)
elif pa.types.is_boolean(pa_dtype) and all_numeric_reductions in {
"sem",
"std",
Expand Down