Skip to content

BUG/ENH: fix pyarrow quantile xfails #50983

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
Feb 1, 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
9 changes: 7 additions & 2 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ def _quantile(
pa_dtype = self._data.type

data = self._data
if pa.types.is_temporal(pa_dtype) and interpolation in ["lower", "higher"]:
if pa.types.is_temporal(pa_dtype):
# https://github.com/apache/arrow/issues/33769 in these cases
# we can cast to ints and back
nbits = pa_dtype.bit_width
Expand All @@ -1254,7 +1254,12 @@ def _quantile(

result = pc.quantile(data, q=qs, interpolation=interpolation)

if pa.types.is_temporal(pa_dtype) and interpolation in ["lower", "higher"]:
if pa.types.is_temporal(pa_dtype):
nbits = pa_dtype.bit_width
if nbits == 32:
result = result.cast(pa.int32())
else:
result = result.cast(pa.int64())
result = result.cast(pa_dtype)

return type(self)(result)
Expand Down
24 changes: 23 additions & 1 deletion pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ def test_quantile(data, interpolation, quantile, request):

if pa.types.is_integer(pa_dtype) or pa.types.is_floating(pa_dtype):
pass
elif pa.types.is_temporal(data._data.type) and interpolation in ["lower", "higher"]:
elif pa.types.is_temporal(data._data.type):
pass
else:
request.node.add_marker(
Expand All @@ -1337,6 +1337,28 @@ def test_quantile(data, interpolation, quantile, request):
data = data.take([0, 0, 0])
ser = pd.Series(data)
result = ser.quantile(q=quantile, interpolation=interpolation)

if pa.types.is_timestamp(pa_dtype) and interpolation not in ["lower", "higher"]:
# rounding error will make the check below fail
Copy link
Member

Choose a reason for hiding this comment

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

For the rounding errors for interpolation not in ["lower", "higher"], is it due to the int casting or just the quantile computation?

Copy link
Member Author

Choose a reason for hiding this comment

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

not sure where it comes in. just checked that the results match what we get with DTA

# (e.g. '2020-01-01 01:01:01.000001' vs '2020-01-01 01:01:01.000001024'),
# so we'll check for now that we match the numpy analogue
if pa_dtype.tz:
pd_dtype = f"M8[{pa_dtype.unit}, {pa_dtype.tz}]"
else:
pd_dtype = f"M8[{pa_dtype.unit}]"
ser_np = ser.astype(pd_dtype)

expected = ser_np.quantile(q=quantile, interpolation=interpolation)
if quantile == 0.5:
if pa_dtype.unit == "us":
expected = expected.to_pydatetime(warn=False)
assert result == expected
else:
if pa_dtype.unit == "us":
expected = expected.dt.floor("us")
tm.assert_series_equal(result, expected.astype(data.dtype))
return

if quantile == 0.5:
assert result == data[0]
else:
Expand Down