Skip to content
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

BUG: GroupBy.quantile fails with pd.NA #43150

Merged
merged 9 commits into from
Sep 4, 2021
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Fixed regressions
- Fixed regression in :class:`DataFrame` constructor failing to broadcast for defined :class:`Index` and len one list of :class:`Timestamp` (:issue:`42810`)
- Performance regression in :meth:`core.window.ewm.ExponentialMovingWindow.mean` (:issue:`42333`)
- Fixed regression in :meth:`.GroupBy.agg` incorrectly raising in some cases (:issue:`42390`)
-
- Fixed regression in :meth:`.GroupBy.quantile` which was failing with ``pandas.NA`` (:issue:`42849`)

.. ---------------------------------------------------------------------------

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class providing the base-class of operations.
from pandas.core.dtypes.common import (
is_bool_dtype,
is_datetime64_dtype,
is_float_dtype,
is_integer_dtype,
is_numeric_dtype,
is_object_dtype,
Expand Down Expand Up @@ -2448,6 +2449,9 @@ def pre_processor(vals: ArrayLike) -> tuple[np.ndarray, np.dtype | None]:
elif is_timedelta64_dtype(vals.dtype):
inference = np.dtype("timedelta64[ns]")
out = np.asarray(vals).astype(float)
elif isinstance(vals, ExtensionArray) and is_float_dtype(vals):
inference = np.dtype(np.float64)
jbrockmendel marked this conversation as resolved.
Show resolved Hide resolved
out = vals.to_numpy(dtype=float, na_value=np.nan)
jbrockmendel marked this conversation as resolved.
Show resolved Hide resolved
else:
out = np.asarray(vals)

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/groupby/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ def test_groupby_quantile_skips_invalid_dtype(q):
tm.assert_frame_equal(result, expected)


def test_groupby_quantile_NA(any_float_dtype):
debnathshoham marked this conversation as resolved.
Show resolved Hide resolved
# GH#42849
df = DataFrame({"x": [1, 1], "y": [0.2, np.nan]}, dtype=any_float_dtype)
result = df.groupby("x")["y"].quantile(0.5)
Copy link
Member

Choose a reason for hiding this comment

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

can you do a case with a listlike qs e.g. [0.5, 0.75]

Copy link
Member Author

Choose a reason for hiding this comment

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

added below

expected = pd.Series([0.2], dtype=float, index=[1.0], name="y")
expected.index.name = "x"
tm.assert_series_equal(expected, result)


def test_groupby_timedelta_quantile():
# GH: 29485
df = DataFrame(
Expand Down