-
-
Notifications
You must be signed in to change notification settings - Fork 143
GH456 First attempt GroupBy.transform improved typing #1242
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
Open
loicdiridollou
wants to merge
6
commits into
pandas-dev:main
Choose a base branch
from
loicdiridollou:gh456_groupby
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
020f93d
GH456 First attempt GroupBy.transform improved typing
loicdiridollou 106a6f5
GH456 Attempt GroupBy.aggregate improved typing
loicdiridollou 3bba101
GH456 Attempt GroupBy.aggregate improved typing
loicdiridollou 053b7e7
GH456 PR Feedback
loicdiridollou 4141a06
GH456 PR Feedback
loicdiridollou f9863d0
GH456 PR Feedback
loicdiridollou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,56 @@ | ||
from collections.abc import Hashable | ||
import dataclasses | ||
from typing import ( | ||
Literal, | ||
TypeAlias, | ||
) | ||
|
||
@dataclasses.dataclass(order=True, frozen=True) | ||
class OutputKey: | ||
label: Hashable | ||
position: int | ||
|
||
ReductionKernelType: TypeAlias = Literal[ | ||
"all", | ||
"any", | ||
"corrwith", | ||
"count", | ||
"first", | ||
"idxmax", | ||
"idxmin", | ||
"last", | ||
"max", | ||
"mean", | ||
"median", | ||
"min", | ||
"nunique", | ||
"prod", | ||
# as long as `quantile`'s signature accepts only | ||
# a single quantile value, it's a reduction. | ||
# GH#27526 might change that. | ||
"quantile", | ||
"sem", | ||
"size", | ||
"skew", | ||
"std", | ||
"sum", | ||
"var", | ||
] | ||
|
||
TransformationKernelType: TypeAlias = Literal[ | ||
"bfill", | ||
"cumcount", | ||
"cummax", | ||
"cummin", | ||
"cumprod", | ||
"cumsum", | ||
"diff", | ||
"ffill", | ||
"fillna", | ||
"ngroup", | ||
"pct_change", | ||
"rank", | ||
"shift", | ||
] | ||
|
||
TransformReductionListType: TypeAlias = ReductionKernelType | TransformationKernelType |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1078,25 +1078,109 @@ def test_types_groupby_agg() -> None: | |
r"The provided callable <built-in function (min|sum)> is currently using", | ||
upper="2.2.99", | ||
): | ||
check(assert_type(s.groupby(level=0).agg(sum), pd.Series), pd.Series) | ||
|
||
def sum_sr(s: pd.Series[int]) -> int: | ||
# type of `sum` not well inferred by mypy | ||
return s.sum() | ||
|
||
check( | ||
assert_type(s.groupby(level=0).agg(sum_sr), "pd.Series[int]"), | ||
pd.Series, | ||
np.integer, | ||
) | ||
check( | ||
assert_type(s.groupby(level=0).agg([min, sum]), pd.DataFrame), pd.DataFrame | ||
) | ||
|
||
|
||
def test_types_groupby_transform() -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should add tests for two of the string transform arguments (e.g., "mean", "first") |
||
s: pd.Series[int] = pd.Series([4, 2, 1, 8], index=["a", "b", "a", "b"]) | ||
|
||
def transform_func( | ||
x: pd.Series[int], pos_arg: bool, kw_arg: str | ||
) -> pd.Series[float]: | ||
return x / (2.0 if pos_arg else 1.0) | ||
|
||
check( | ||
assert_type( | ||
s.groupby(lambda x: x).transform(transform_func, True, kw_arg="foo"), | ||
"pd.Series[float]", | ||
), | ||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pd.Series, | ||
float, | ||
) | ||
check( | ||
assert_type( | ||
s.groupby(lambda x: x).transform( | ||
transform_func, True, engine="cython", kw_arg="foo" | ||
), | ||
"pd.Series[float]", | ||
), | ||
pd.Series, | ||
float, | ||
) | ||
check( | ||
assert_type( | ||
s.groupby(lambda x: x).transform("mean"), | ||
"pd.Series", | ||
), | ||
pd.Series, | ||
) | ||
check( | ||
assert_type( | ||
s.groupby(lambda x: x).transform("first"), | ||
"pd.Series", | ||
), | ||
pd.Series, | ||
) | ||
|
||
|
||
def test_types_groupby_aggregate() -> None: | ||
s = pd.Series([4, 2, 1, 8], index=["a", "b", "a", "b"]) | ||
check(assert_type(s.groupby(level=0).aggregate("sum"), pd.Series), pd.Series) | ||
check( | ||
assert_type(s.groupby(level=0).aggregate(["min", "sum"]), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
|
||
def func(s: pd.Series[int]) -> float: | ||
return s.astype(float).min() | ||
|
||
s = pd.Series([1, 2, 3, 4]) | ||
check( | ||
assert_type(s.groupby([1, 1, 2, 2]).agg(func), "pd.Series[float]"), | ||
pd.Series, | ||
np.floating, | ||
) | ||
check( | ||
assert_type(s.groupby(level=0).aggregate(func), "pd.Series[float]"), | ||
pd.Series, | ||
np.floating, | ||
) | ||
check( | ||
assert_type( | ||
s.groupby(level=0).aggregate(func, engine="cython"), "pd.Series[float]" | ||
), | ||
pd.Series, | ||
np.floating, | ||
) | ||
check(assert_type(s.groupby([1,1,2,2]).agg(lambda x: x.astype(float).min()), "pd.Series[float]"), pd.Series, int) | ||
|
||
with pytest_warns_bounded( | ||
FutureWarning, | ||
r"The provided callable <built-in function (min|sum)> is currently using", | ||
upper="2.2.99", | ||
): | ||
check(assert_type(s.groupby(level=0).aggregate(sum), pd.Series), pd.Series) | ||
|
||
def sum_sr(s: pd.Series[int]) -> int: | ||
# type of `sum` not well inferred by mypy | ||
return s.sum() | ||
|
||
check( | ||
assert_type(s.groupby(level=0).aggregate(sum_sr), "pd.Series[int]"), | ||
pd.Series, | ||
np.integer, | ||
) | ||
check( | ||
assert_type(s.groupby(level=0).aggregate([min, sum]), pd.DataFrame), | ||
pd.DataFrame, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Before this overload, you could add this overload:
Then you know that if you start with a
Series
with a known type, then the return type would be inferred from the callable. And it works with a lambda function, e.g.:In this case,
q
would have typeSeries[float]
, which is what you want.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.
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 that's because the type of
new_func
isn't clear.But I think it would work if you did
check(assert_type(s.groupby([1,1,2,2]).agg(lambda x: x.astype(float).min()), "pd.Series[int]"), pd.Series, int)
Because then it can know that
x
is aSeries[int]
and that thelambda
becomesSeries[int]
Can you try that?
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 tried that for the last push, see
pandas-stubs/tests/test_series.py
Line 1167 in f9863d0
It fails in all CI: