Skip to content

ENH: support cumsum with pyarrow durations #50927

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 1 commit into from
Jan 24, 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
13 changes: 12 additions & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,18 @@ def _accumulate(
pyarrow_meth = getattr(pc, pyarrow_name, None)
if pyarrow_meth is None:
return super()._accumulate(name, skipna=skipna, **kwargs)
result = pyarrow_meth(self._data, skip_nulls=skipna, **kwargs)

data_to_accum = self._data

pa_dtype = data_to_accum.type
if pa.types.is_duration(pa_dtype):
data_to_accum = data_to_accum.cast(pa.int64())

result = pyarrow_meth(data_to_accum, skip_nulls=skipna, **kwargs)

if pa.types.is_duration(pa_dtype):
result = result.cast(pa_dtype)

return type(self)(result)

def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
Expand Down
23 changes: 16 additions & 7 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,27 @@ def test_getitem_scalar(self, data):


class TestBaseAccumulateTests(base.BaseAccumulateTests):
def check_accumulate(self, s, op_name, skipna):
result = getattr(s, op_name)(skipna=skipna).astype("Float64")
expected = getattr(s.astype("Float64"), op_name)(skipna=skipna)
def check_accumulate(self, ser, op_name, skipna):
result = getattr(ser, op_name)(skipna=skipna)

if ser.dtype.kind == "m":
# Just check that we match the integer behavior.
ser = ser.astype("int64[pyarrow]")
result = result.astype("int64[pyarrow]")

result = result.astype("Float64")
expected = getattr(ser.astype("Float64"), op_name)(skipna=skipna)
self.assert_series_equal(result, expected, check_dtype=False)

@pytest.mark.parametrize("skipna", [True, False])
def test_accumulate_series_raises(self, data, all_numeric_accumulations, skipna):
pa_type = data.dtype.pyarrow_dtype
if (
(pa.types.is_integer(pa_type) or pa.types.is_floating(pa_type))
(
pa.types.is_integer(pa_type)
or pa.types.is_floating(pa_type)
or pa.types.is_duration(pa_type)
)
and all_numeric_accumulations == "cumsum"
and not pa_version_under9p0
):
Expand Down Expand Up @@ -423,9 +434,7 @@ def test_accumulate_series(self, data, all_numeric_accumulations, skipna, reques
raises=NotImplementedError,
)
)
elif all_numeric_accumulations == "cumsum" and (
pa.types.is_duration(pa_type) or pa.types.is_boolean(pa_type)
):
elif all_numeric_accumulations == "cumsum" and (pa.types.is_boolean(pa_type)):
request.node.add_marker(
pytest.mark.xfail(
reason=f"{all_numeric_accumulations} not implemented for {pa_type}",
Expand Down