Skip to content

Commit fdba1e6

Browse files
authored
ENH: support min/max/sum for pyarrow duration dtypes (#50928)
1 parent 1269a3b commit fdba1e6

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

pandas/core/arrays/arrow/array.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,9 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
10431043
not_eq = pc.not_equal(data_to_cmp, 0)
10441044
data_to_reduce = not_eq
10451045

1046+
elif name in ["min", "max", "sum"] and pa.types.is_duration(pa_type):
1047+
data_to_reduce = self._data.cast(pa.int64())
1048+
10461049
if name == "sem":
10471050

10481051
def pyarrow_meth(data, skip_nulls, **kwargs):
@@ -1077,6 +1080,9 @@ def pyarrow_meth(data, skip_nulls, **kwargs):
10771080
raise TypeError(msg) from err
10781081
if pc.is_null(result).as_py():
10791082
return self.dtype.na_value
1083+
1084+
if name in ["min", "max", "sum"] and pa.types.is_duration(pa_type):
1085+
result = result.cast(pa_type)
10801086
return result.as_py()
10811087

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

pandas/tests/extension/base/groupby.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def test_in_numeric_groupby(self, data_for_grouping):
137137
or is_string_dtype(dtype)
138138
or is_period_dtype(dtype)
139139
or is_object_dtype(dtype)
140+
or dtype.kind == "m" # in particular duration[*][pyarrow]
140141
):
141142
expected = pd.Index(["B", "C"])
142143
result = df.groupby("A").sum().columns

pandas/tests/extension/test_arrow.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,6 @@ def test_reduce_series(self, data, all_numeric_reductions, skipna, request):
549549
"sem",
550550
] and pa.types.is_temporal(pa_dtype):
551551
request.node.add_marker(xfail_mark)
552-
elif all_numeric_reductions in ["sum", "min", "max"] and pa.types.is_duration(
553-
pa_dtype
554-
):
555-
request.node.add_marker(xfail_mark)
556552
elif pa.types.is_boolean(pa_dtype) and all_numeric_reductions in {
557553
"sem",
558554
"std",

0 commit comments

Comments
 (0)