Skip to content

BUG 58031 -- groupby aggregate dtype consistency #58258

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

Closed
wants to merge 3 commits into from

Conversation

longovin
Copy link

@longovin longovin requested a review from rhshadrach as a code owner April 15, 2024 01:32
@mroeschke mroeschke added Groupby Dtype Conversions Unexpected or buggy dtype conversions Apply Apply, Aggregate, Transform, Map labels Apr 15, 2024
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

@@ -914,7 +914,8 @@ def agg_series(
np.ndarray or ExtensionArray
"""

if not isinstance(obj._values, np.ndarray):
# if objtype is not in np.dtypes, type is preserved
if not isinstance(obj._values, np.ndarray) and obj.dtype != "boolean":
Copy link
Member

@rhshadrach rhshadrach Apr 15, 2024

Choose a reason for hiding this comment

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

I don't think this is the right fix. While the issue raised is with boolean, it can occur with any dtype.

One possible solution is to take npvalues and convert it to the corresponding dtype_backend - e.g. if it starts out as a numpy_nullable, then we can do:

from pandas.core.dtypes.cast import convert_dtypes
            
dtype = convert_dtypes(npvalues, dtype_backend="numpy_nullable")
# We don't actually want Series here - should be returning a NumPy or E array
out = Series(npvalues, dtype=dtype)

Similarly for the pyarrow backend. This seems to me to be the right behavior because it parallel's the inference we're doing with NumPy. But I can't say I feel very confident there aren't some cases where this would still go wrong.

cc @jbrockmendel @WillAyd for any thoughts.

Copy link
Member

Choose a reason for hiding this comment

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

Not sure what the best alternative would be but that's unfortunate we'd have to convert pyarrow boolean types to numpy

Copy link
Member

Choose a reason for hiding this comment

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

still reading, but first thought is that instead of comparing to the string "boolean" the check should be isinstance(dtype, BooleanDtype)

Copy link
Member

Choose a reason for hiding this comment

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

OK, looking at the test makes it clear what the underlying issue is, and its a tough one. maybe_cast_pointwise_result goes through _from_scalars, which it turns out was a mis-feature (xref #56430). What we need is an EA method that does family-preserving inference (i.e. if you start with pyarrow/masked/numpy/sparse, you end up with pyarrow/masked/numpy/sparse).

Shorter-term, it looks like BooleanArray._from_scalars is insufficiently strict; it should raise when it sees floats.

Copy link
Member

Choose a reason for hiding this comment

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

maybe_convert_objects already has a convert_to_nullable_dtype - but only for bool/int. Could this be expanded upon - both for other nullable dtypes as well as pyarrow? It's already quite large and complex, but it seems to me there is an advantage of having a single function to call when you are in the situation of "I have an object NumPy array, and need to determine how to do type inference".

That being said, if the EA method @jbrockmendel is proposing completely supplants maybe_convert_objects in the long term, that sounds like a good route to me.

Copy link
Member

Choose a reason for hiding this comment

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

knee-jerk: that might work in this case, but im wary of introducing even more ways of accomplishing family-retention across the code base. they're bound to accumulate small inconsistencies which will then be a hassle to smooth out

Copy link
Member

Choose a reason for hiding this comment

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

but im wary of introducing even more ways

No disagreement there. It just seems like your proposed EA method from #58258 (comment) needs to go through all the values, determine what is seen, come up with the output dtype, and then make it so. The first two steps are done in maybe_convert_objects, and I'd be wary of duplicating that logic elsewhere.

Copy link
Member

Choose a reason for hiding this comment

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

i suspect that what you're describing is likely to be the basis of the MaskedArray._whatever implementation.

.dtypes.values[0]
)

assert boolean_return_type == bool_return_type
Copy link
Member

Choose a reason for hiding this comment

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

When writing tests, always test the full result (using tm.assert_frame_equals here) instead of just one part of it. See the tests above for examples.

Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label May 17, 2024
@mroeschke
Copy link
Member

Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.

@mroeschke mroeschke closed this Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Dtype Conversions Unexpected or buggy dtype conversions Groupby Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent behaviour of GroupBy for BooleanArray series
5 participants