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: Fix SeriesGroupBy.quantile for nullable integers #33138

Merged
merged 13 commits into from
Apr 7, 2020
Merged

BUG: Fix SeriesGroupBy.quantile for nullable integers #33138

merged 13 commits into from
Apr 7, 2020

Conversation

dsaxton
Copy link
Member

@dsaxton dsaxton commented Mar 30, 2020

@@ -2241,6 +2242,10 @@ def _get_cythonized_result(
for idx, obj in enumerate(self._iterate_slices()):
name = obj.name
values = obj._values
if is_extension_array_dtype(values.dtype) and is_integer_dtype(
Copy link
Member

Choose a reason for hiding this comment

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

I think there is a more generic way to handle this? @jbrockmendel

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, agree this seems like an overly specific check that probably isn't catching other bugs. I tried just casting any ExtensionArray but got bunches of test failures (I think they had to do with datetimes).

Copy link
Member

Choose a reason for hiding this comment

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

hmm this definitely seems not-great. i think to do this right we need some EA method that gives the ndarray to use when we get here, and maybe a way to round-trip the results if the method is dtype-preserving.

Am I right in thinking that dt64tz (and maybe period?) get cast to i8 somewhere around here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at this again I think it might make sense to update the pre_processor function: https://github.com/pandas-dev/pandas/blob/master/pandas/core/groupby/groupby.py#L1857 . Does that look right to you @jbrockmendel ?

Copy link
Member

Choose a reason for hiding this comment

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

that seems plausible. im hoping there's a way to do this with values_for_argsort that can be shared by EAs, and by other order-based methods

Copy link
Member Author

Choose a reason for hiding this comment

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

Would that handle NAs in a way that would still give the right quantiles?

@WillAyd WillAyd added ExtensionArray Extending pandas with custom dtypes or arrays. Groupby labels Mar 30, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1 milestone Mar 30, 2020
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

so if you quantile BooleanDtype is another test case

@@ -1861,9 +1863,13 @@ def pre_processor(vals: np.ndarray) -> Tuple[np.ndarray, Optional[Type]]:
)

inference = None
if is_integer_dtype(vals):
if is_integer_dtype(vals.dtype):
Copy link
Contributor

Choose a reason for hiding this comment

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

this is fine for here, but I think we need a generic 'convert_this_to_something_we_can_use_in_cython)` method on EA. @jbrockmendel @jorisvandenbossche @TomAugspurger

Copy link
Member

Choose a reason for hiding this comment

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

As discussed here, i think the EA.convert_this_to_something_we_can_use_in_cython is going to look something like _ordinal_values, at least for methods like quantile that are ordering-based

@WillAyd WillAyd merged commit 8267427 into pandas-dev:master Apr 7, 2020
@WillAyd
Copy link
Member

WillAyd commented Apr 7, 2020

Great thanks @dsaxton

@dsaxton dsaxton deleted the integer-quantile branch April 7, 2020 23:13
idx = pd.Index(["x", "y"], name="a")
true_quantiles = [0.5]

expected = pd.Series(true_quantiles * 2, index=idx, name="b")
Copy link
Member

Choose a reason for hiding this comment

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

tracking down #51424 it looks like support for nullable-bool was added here, but it seems weird that we'd return Float64 for that. only discussion is see is a comment from @jreback saying it would need a separate test. is this intentional?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays. Groupby
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SeriesGroupBy.quantile doesn't work for nullable integers
5 participants