Skip to content

REF: let EAs override WrappedCythonOp groupby implementations #51166

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 17 commits into from
Apr 5, 2023

Conversation

jbrockmendel
Copy link
Member

Goal is to allow EAs (including our own pyarrow-backed) to implement performant GroupBy reductions/transforms without necessarily having to convert to numpy.

Analogous to #51003 (quantile) and #51116 (any, all, std).

This implements EA.grouby_op which takes a "how" keyword to specify which reduction/transform is being done. By contrast, #51003 implements EA.groupby_quantile and #51116 implements EA.groupby_std and EA.groupby_any_all. If we go this direction (we should), we should choose one naming scheme for all these methods instead of three slightly different ones. i.e. either a) shove everything into something like groupby_op or b) explicitly have groupby_foo for each of the relevant methods.

Also will need docs before moving forward.

@jbrockmendel
Copy link
Member Author

changed groupby_op to _groupby_op and added a docstring making explicit that this API is experimental.

Open to formatting suggestions for the kwargs part of the docstring.

@jbrockmendel
Copy link
Member Author

jbrockmendel commented Feb 7, 2023

Thinking about the namespace question whether to have a single dispatching method (_groupby_op) vs a bunch (_grouped_sum, _grouped_std, ...). With the single-method (this PR), we have **kwargs in the signature, and it is clumsy to document what kwargs to expect for each "how". Also should (but dont yet) document the expected shape of the returned array, which varies depending on which method we're talking about (fortunately only a few cases here).

Maybe a middle ground with e.g. _grouped_reduce, _grouped_transform, _grouped_quantile, _grouped_rank so that each of them can have their kwargs made explicit? cc @phofl @mroeschke @jorisvandenbossche

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Feb 8, 2023

Goal is to allow EAs (including our own pyarrow-backed) to implement performant GroupBy reductions/transforms without necessarily having to convert to numpy.

Is there actually a demand from ExtensionArray authors to be able to implement the groupby reductions? (I mean, I can certainly see that it could be useful for certain kind of extension dtypes, but I would still be wary to implement it if it's not really requested)
Or is this more as a way to clean up the dispatching in the groupby code for our own EAs? Because then we should discuss if we actually want to make this public of external EAs, or only do it for our internal ones for now (also the pyarrow-backed arrays could be done internally just like we currently special case the masked arrays)

@jbrockmendel
Copy link
Member Author

Is there actually a demand from ExtensionArray authors to be able to implement the groupby reductions? (I mean, I can certainly see that it could be useful for certain kind of extension dtypes, but I would still be wary to implement it if it's not really requested)

No, but I think there is a chicken/egg issue in that some libraries (i have in mind cudf, dask, modin) haven't implemented EAs at all and this is a necessary step to making EAs a viable option for them.

Or is this more as a way to clean up the dispatching in the groupby code for our own EAs? Because then we should discuss if we actually want to make this public of external EAs, or only do it for our internal ones for now (also the pyarrow-backed arrays could be done internally just like we currently special case the masked arrays)

That is definitely a motivator. This is much nicer abstraction-wise. We agreed long ago that ideally our internal EAs would be treated symmetrically with 3rd party EAs.

@jbrockmendel
Copy link
Member Author

@MarcoGorelli this is the one i mentioned

@simonjayhawkins simonjayhawkins added Groupby ExtensionArray Extending pandas with custom dtypes or arrays. labels Feb 22, 2023
@jbrockmendel
Copy link
Member Author

@jorisvandenbossche feedback in #51471 seems mostly positive. any thoughts?

@jbrockmendel
Copy link
Member Author

@MarcoGorelli gentle ping

@jreback
Copy link
Contributor

jreback commented Mar 6, 2023

+1 here. to the extent this cleans up generic pandas code this is quite valuable (not to mention that EA authors can use this).

@jbrockmendel
Copy link
Member Author

@mroeschke gentle ping, this is the way forward for e.g. #51996

@mroeschke
Copy link
Member

I get this solves #51996 in the short term, but my only reservation here is the dependence on the existing grouping machinery (the determination of ids) w.r.t. pyarrow. It would be nice if we could utilize pyarrow.Table's groupby machinery which then leads to using their groupby aggregation machinery too. If we had some notion of "object that manages EA" (#51471), that could be a pyarrow Table for ArrowExtensionArrays where we can dispatch these groupby ops to.

This also applies probably to other dataframe libraries like cuDF/Dask/etc that we hope could utilize the EA interface, but groupby is a "frame-wise" operation where this concern also applies.

@jbrockmendel
Copy link
Member Author

Aside from the 3rd-party concerns, our own EA-specific logic fits much better here than it does in groupby.ops. I've changed this to use a private name to somewhat safeguard against future changes.

@mroeschke
Copy link
Member

Okay I see the benefit for our EAs currently. Just hoping that you are open to a future API where 3rd party EAs (including pyarrow) could also dictate the grouping + op steps together.

@jbrockmendel
Copy link
Member Author

Just hoping that you are open to a future API where 3rd party EAs (including pyarrow) could also dictate the grouping + op steps together

Absolutely open to it. I'd love to rip out a bunch of our libgroupby code one day.

@MarcoGorelli
Copy link
Member

@MarcoGorelli gentle ping

Apologies, was off last week (though still did some minor things). I've made it a priority to look at this next week

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

No objections, generally looks good, but I'm not familiar enough with this part of the code to say whether this should be the way to go

Comment on lines +1798 to +1804
if isinstance(self.dtype, StringDtype):
dtype = self.dtype
string_array_cls = dtype.construct_array_type()
return string_array_cls._from_sequence(res_values, dtype=dtype)

else:
raise NotImplementedError
Copy link
Member

Choose a reason for hiding this comment

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

hasn't this if-then already been done above? or is it just to future-proof the code?

Copy link
Member Author

Choose a reason for hiding this comment

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

future-proof is a nice way of putting it, yes. this is transplanted from its current position in WrappedCythonOp where the redundant checks are in separate methods

@mroeschke
Copy link
Member

@mroeschke mroeschke added this to the 2.1 milestone Apr 4, 2023
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Should be fine, looks like the only changes since I last reviewed are minor mypy fixups and merges from upstream/main

op = WrappedCythonOp(how=how, kind=kind, has_dropped_na=has_dropped_na)

# libgroupby functions are responsible for NOT altering mask
mask = self._mask
Copy link
Member

Choose a reason for hiding this comment

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

We should probably add tests covering this at some point. Not sure if we already have them

@phofl phofl merged commit b635369 into pandas-dev:main Apr 5, 2023
@phofl
Copy link
Member

phofl commented Apr 5, 2023

thx @jbrockmendel

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

Successfully merging this pull request may close these issues.

ENH: generic implementation of ea_wrap_cython_operation
7 participants