-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix remaining cases of groupby(...).transform with dropna=True #46367
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
Conversation
…sform_dropna � Conflicts: � doc/source/whatsnew/v1.5.0.rst
…sform_dropna � Conflicts: � pandas/core/groupby/generic.py � pandas/core/groupby/groupby.py � pandas/core/groupby/ops.py � pandas/tests/groupby/transform/test_transform.py
… into transform_dropna
…sform_dropna � Conflicts: � pandas/core/groupby/groupby.py � pandas/core/groupby/ops.py � pandas/tests/groupby/transform/test_transform.py
pandas/core/groupby/ops.py
Outdated
@@ -969,7 +985,7 @@ def _cython_operation( | |||
""" | |||
assert kind in ["transform", "aggregate"] | |||
|
|||
cy_op = WrappedCythonOp(kind=kind, how=how) | |||
cy_op = WrappedCythonOp(grouper=self, kind=kind, how=how) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbrockmendel - It would suffice to pass self.has_dropped_na
instead of the full grouper here, wasn't sure if there was a preference one way or the others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd much rather just has_dropped_na be part of WrappedCythonOop's state
@@ -592,6 +604,10 @@ def _call_cython_op( | |||
|
|||
result = result.T | |||
|
|||
if self.how == "rank" and self.grouper.has_dropped_na: | |||
# TODO: Wouldn't need this if group_rank supported mask |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would this be a matter of supporting a mask arg in the libgroupby function, or would this be just for Nullable dtype? bc i have a branch on deck that does the former
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The former - I believe rank had to be singled out here because it was the one transform that didn't support a mask arg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhshadrach mask just got added to group_rank, but commenting this out causes a few failures. is more needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment was incorrect; opened #46953
pandas/core/groupby/ops.py
Outdated
@@ -462,6 +469,11 @@ def _cython_op_ndim_compat( | |||
# otherwise we have OHLC | |||
return res.T | |||
|
|||
if self.kind == "transform" and self.grouper.has_dropped_na: | |||
mask = comp_ids == -1 | |||
# make mask 2d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be rolled into the mask-reshaping done above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block turned out to not be necessary. It will be removed.
pandas/core/groupby/ops.py
Outdated
@@ -260,6 +264,9 @@ def _get_output_shape(self, ngroups: int, values: np.ndarray) -> Shape: | |||
def _get_out_dtype(self, dtype: np.dtype) -> np.dtype: | |||
how = self.how | |||
|
|||
if self.kind == "transform" and self.grouper.has_dropped_na: | |||
dtype = ensure_dtype_can_hold_na(dtype) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ATM we do something similar to this L578-L591. could this be rolled into that? (admittedly thats a bit messy so this may just b A Better Solution)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems better to me to determine the result dtype upfront, rather than cast after, as much as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i dont have an opinion on before vs after so am happy to defer to you, but do strongly prefer doing things Just One Way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense - I didn't realize that my change to _get_cython_vals
actually makes values
float, and so this is indeed not necessary at all.
Last thought before I go guzzle some caffeine: this looks tangentially related to #43943. Might be something worth salvaging from that. |
doc/source/whatsnew/v1.5.0.rst
Outdated
In [3]: df.groupby('a', dropna=True).transform(lambda x: x.sum()) | ||
Out[3]: | ||
b | ||
0 5 | ||
1 5 | ||
|
||
In [3]: df.groupby('a', dropna=True).transform('ffill') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comment here on the casting of the fill value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're saying to explain why the old result comes out as -9223372036854775808
, which is np.nan
when interpreted as an integer. Will do.
pandas/core/groupby/groupby.py
Outdated
return self._python_apply_general(curried, self._obj_with_exclusions) | ||
result = self._python_apply_general(curried, self._obj_with_exclusions) | ||
|
||
if result.ndim == 1 and self.obj.ndim == 1 and result.name != self.obj.name: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result.name != self.obj.name
will be wrong with np.nan (and will raise with pd.NA)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh, thanks. This highlights the fact that we shouldn't be fixing this here, but rather the logic in wrapping the apply results. I didn't want this PR spreading out to that method though. I've opened #46369 for this; I'm thinking here we ignore testing the name, and fix properly.
…sform_dropna � Conflicts: � pandas/core/groupby/ops.py
Thanks for all the feedback @jbrockmendel and @jreback - ready for another review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good. pls rebas some small comments. ping on greeen.
doc/source/whatsnew/v1.5.0.rst
Outdated
@@ -83,32 +83,41 @@ did not have the same index as the input. | |||
|
|||
.. code-block:: ipython | |||
|
|||
In [3]: df.groupby('a', dropna=True).transform('sum') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe be worth commenting on each of these what is changing (e.g. like you did for [3])
@@ -394,10 +407,14 @@ def test_transform_transformation_func(request, transformation_func): | |||
mock_op = lambda x: getattr(x, transformation_func)() | |||
|
|||
result = test_op(df.groupby("A")) | |||
groups = [df[["B"]].iloc[:4], df[["B"]].iloc[4:6], df[["B"]].iloc[6:]] | |||
expected = concat([mock_op(g) for g in groups]) | |||
# pass the group in same order as iterating `for ... in df.groupby(...)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you link the issue/PR here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Within this test, the issue number is on L374.
…sform_dropna � Conflicts: � pandas/tests/groupby/transform/test_transform.py
thanks @rhshadrach |
transform
groupby
withNaN
s #17093 (Replace xxxx with the Github issue number)doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.