Skip to content

Commit 471b777

Browse files
committed
ENH: support min/max/sum for pyarrow duration dtypes
1 parent 1128f5e commit 471b777

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

pandas/core/arrays/arrow/array.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,8 +1033,15 @@ def pyarrow_meth(data, skip_nulls, **kwargs):
10331033
if pyarrow_meth is None:
10341034
# Let ExtensionArray._reduce raise the TypeError
10351035
return super()._reduce(name, skipna=skipna, **kwargs)
1036+
1037+
data_to_reduce = self._data
1038+
1039+
pa_dtype = self._data.type
1040+
if name in ["min", "max", "sum"] and pa.types.is_duration(pa_dtype):
1041+
data_to_reduce = self._data.cast(pa.int64())
1042+
10361043
try:
1037-
result = pyarrow_meth(self._data, skip_nulls=skipna, **kwargs)
1044+
result = pyarrow_meth(data_to_reduce, skip_nulls=skipna, **kwargs)
10381045
except (AttributeError, NotImplementedError, TypeError) as err:
10391046
msg = (
10401047
f"'{type(self).__name__}' with dtype {self.dtype} "
@@ -1045,6 +1052,9 @@ def pyarrow_meth(data, skip_nulls, **kwargs):
10451052
raise TypeError(msg) from err
10461053
if pc.is_null(result).as_py():
10471054
return self.dtype.na_value
1055+
1056+
if name in ["min", "max", "sum"] and pa.types.is_duration(pa_dtype):
1057+
result = result.cast(pa_dtype)
10481058
return result.as_py()
10491059

10501060
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
@@ -539,10 +539,6 @@ def test_reduce_series(self, data, all_numeric_reductions, skipna, request):
539539
"sem",
540540
] and pa.types.is_temporal(pa_dtype):
541541
request.node.add_marker(xfail_mark)
542-
elif all_numeric_reductions in ["sum", "min", "max"] and pa.types.is_duration(
543-
pa_dtype
544-
):
545-
request.node.add_marker(xfail_mark)
546542
elif pa.types.is_boolean(pa_dtype) and all_numeric_reductions in {
547543
"sem",
548544
"std",

0 commit comments

Comments
 (0)