Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 1 addition & 3 deletions pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,7 @@ def _quantile(
else:
# e.g. test_quantile_empty we are empty integer dtype and res_values
# has floating dtype
# TODO: technically __init__ isn't defined here.
# Should we raise NotImplementedError and handle this on NumpyEA?
return type(self)(res_values) # type: ignore[call-arg]
return type(self)._from_sequence(res_values) # type: ignore[call-arg]
Copy link
Member

Choose a reason for hiding this comment

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

While having an else clause that actually cannot work doesn't seem great (and so this change might be good anyway), looking at the code above (if res_values.dtype == self._ndarray.dtype:), I am wondering if the problem might not be that quantile_with_mask is not returning the correct dtype in case of empty data.

In that case it returns NaT in an array, and it seems to return nanoseconds, which seems wrong?

Copy link
Member

Choose a reason for hiding this comment

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

Looking a bit more, quantile_with_mask is returning nanoseconds in this case because of the fill_value being passed, which is self._internal_fill_value, and that seems to be defined as np.timedelta64('NaT','ns'). That probably need to take the unit of self into account?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice find. The problem was _internal_fill_value assuming nanoseconds for NaT.

I reverted the changes to ._quantile to just be conservative, though probably the right long term change


# ------------------------------------------------------------------------
# numpy-like methods
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/methods/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,10 @@ def test_quantile_dtype_size(self, any_int_ea_dtype):
result = ser.quantile([0.1, 0.5])
expected = Series([1, 1], dtype=any_int_ea_dtype, index=[0.1, 0.5])
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize("typ", ["datetime64", "timedelta64"])
def test_quantile_empty_datetimelike(typ, unit):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it would be good to also add a dataframe version of this test, such that there is an actual dtype to verify (to assert it has preserved the unit)

ser = Series([], dtype=f"{typ}[{unit}]")
result = ser.quantile()
assert result is pd.NaT
Loading