Skip to content

ENH: support reductions for pyarrow temporal types #50998

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 5 commits into from
Feb 9, 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
2 changes: 2 additions & 0 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
pa_version_under7p0,
pa_version_under8p0,
pa_version_under9p0,
pa_version_under11p0,
)


Expand Down Expand Up @@ -159,6 +160,7 @@ def get_lzma_file() -> type[pandas.compat.compressors.LZMAFile]:
"pa_version_under7p0",
"pa_version_under8p0",
"pa_version_under9p0",
"pa_version_under11p0",
"IS64",
"PY39",
"PY310",
Expand Down
2 changes: 2 additions & 0 deletions pandas/compat/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
pa_version_under8p0 = _palv < Version("8.0.0")
pa_version_under9p0 = _palv < Version("9.0.0")
pa_version_under10p0 = _palv < Version("10.0.0")
pa_version_under11p0 = _palv < Version("11.0.0")
except ImportError:
pa_version_under7p0 = True
pa_version_under8p0 = True
pa_version_under9p0 = True
pa_version_under10p0 = True
pa_version_under11p0 = True
34 changes: 34 additions & 0 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
pa_version_under7p0,
pa_version_under8p0,
pa_version_under9p0,
pa_version_under11p0,
)
from pandas.util._decorators import doc
from pandas.util._validators import validate_fillna_kwargs
Expand Down Expand Up @@ -130,6 +131,16 @@ def floordiv_compat(
ArrowExtensionArrayT = TypeVar("ArrowExtensionArrayT", bound="ArrowExtensionArray")


def get_unit_from_pa_dtype(pa_dtype):
# https://github.com/pandas-dev/pandas/pull/50998#discussion_r1100344804
if pa_version_under11p0:
unit = str(pa_dtype).split("[", 1)[-1][:-1]
if unit not in ["s", "ms", "us", "ns"]:
raise ValueError(pa_dtype)
return unit
return pa_dtype.unit


def to_pyarrow_type(
dtype: ArrowDtype | pa.DataType | Dtype | None,
) -> pa.DataType | None:
Expand Down Expand Up @@ -1039,6 +1050,13 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
elif name in ["min", "max", "sum"] and pa.types.is_duration(pa_type):
data_to_reduce = self._data.cast(pa.int64())

elif name in ["median", "mean", "std", "sem"] and pa.types.is_temporal(pa_type):
nbits = pa_type.bit_width
if nbits == 32:
data_to_reduce = self._data.cast(pa.int32())
else:
data_to_reduce = self._data.cast(pa.int64())

if name == "sem":

def pyarrow_meth(data, skip_nulls, **kwargs):
Expand Down Expand Up @@ -1076,6 +1094,22 @@ def pyarrow_meth(data, skip_nulls, **kwargs):

if name in ["min", "max", "sum"] and pa.types.is_duration(pa_type):
result = result.cast(pa_type)
if name in ["median", "mean"] and pa.types.is_temporal(pa_type):
result = result.cast(pa_type)
if name in ["std", "sem"] and pa.types.is_temporal(pa_type):
result = result.cast(pa.int64())
if pa.types.is_duration(pa_type):
result = result.cast(pa_type)
elif pa.types.is_time(pa_type):
unit = get_unit_from_pa_dtype(pa_type)
result = result.cast(pa.duration(unit))
elif pa.types.is_date(pa_type):
# go with closest available unit, i.e. "s"
result = result.cast(pa.duration("s"))
else:
# i.e. timestamp
result = result.cast(pa.duration(pa_type.unit))

return result.as_py()

def __setitem__(self, key, value) -> None:
Expand Down
7 changes: 0 additions & 7 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,6 @@ def test_reduce_series(self, data, all_numeric_reductions, skipna, request):
elif all_numeric_reductions == "sem" and pa_version_under8p0:
request.node.add_marker(xfail_mark)

elif all_numeric_reductions in [
"mean",
"median",
"std",
"sem",
] and pa.types.is_temporal(pa_dtype):
request.node.add_marker(xfail_mark)
elif pa.types.is_boolean(pa_dtype) and all_numeric_reductions in {
"sem",
"std",
Expand Down