-
-
Notifications
You must be signed in to change notification settings - Fork 143
Fix groupby.apply() and sum(axis=1) #168
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
pandas-stubs/core/frame.pyi
Outdated
@@ -1902,14 +1902,24 @@ class DataFrame(NDFrame, OpsMixin): | |||
fill_value: float | None = ..., | |||
) -> DataFrame: ... | |||
@overload | |||
def sum( | |||
self, | |||
axis: Literal[1, "columns"], |
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.
Isn't the 3rd overload the same as this one but has a broader axis
annotation? Is it possible to simply move the third overload up?
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.
Yes, that's right. Changed in next commit
Thanks for looking into this so quickly. Unfortunately I don't think this is quite right.
But returning a scalar is the only situation in which
So I think there are three cases here:
|
I'm not sure I got all the cases right. Would need better tests. Current version passes the example you initially supplied. One thing to be aware of is that using a |
self, func: Callable[[DataFrame], Series | Scalar], *args, **kwargs | ||
) -> Series: ... | ||
@overload | ||
def apply( # type: ignore[misc] |
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 replaced my local version of generic.pyi with this one and it seems that neither pyright nor mypy need these two ignores (I would have thought both of them need ignores, a DataFrame is Iterable).
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.
If I remove both of these ignores, I get errors from both pyright
and mypy
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.
you are right, not sure what I messed up. Unless you add a test covering this second overload, I would be tempted to remove it.
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.
you are right, not sure what I messed up. Unless you add a test covering this second overload, I would be tempted to remove it.
There is a test already. That's why I needed to add the second overload.
pandas-stubs/tests/test_frame.py
Line 558 in 77b357f
df7: pd.DataFrame = df.groupby(by="col1").apply(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.
Okay, I'm not a fan of this overload but it seems to work :)
Thanks @Dr-Irv |
test_frame.py:test_groupby_apply()
This revealed an issue with
DataFrame.sum(axis=1)
and we probably need to fix things likeDataFrame.mean(axis=1)
, etc., but I think we should just create an issue for that.