-
-
Notifications
You must be signed in to change notification settings - Fork 145
add proper type when grouping by a Series #708
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
Changes from 3 commits
ac92a2f
413f08d
4eeb52a
b984743
903b662
8d692fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,12 +427,28 @@ ByT = TypeVar( | |
Interval[Timedelta], | ||
tuple, | ||
) | ||
SeriesByT = TypeVar( | ||
"SeriesByT", | ||
str, | ||
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. We've been recently advised to no longer use constrained 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. Hi, What is the difference between I tried to replace the SeriesByT with the
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.
You probably also need to adjust the type used for 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. Thanks I actually tried that and got rid of the errors. |
||
bytes, | ||
datetime.date, | ||
bool, | ||
int, | ||
float, | ||
complex, | ||
Timestamp, | ||
Timedelta, | ||
Period, | ||
Interval[int], | ||
Interval[float], | ||
Interval[Timestamp], | ||
Interval[Timedelta], | ||
) | ||
GroupByObjectNonScalar: TypeAlias = ( | ||
tuple | ||
| list[_HashableTa] | ||
| Function | ||
| list[Function] | ||
| Series | ||
| list[Series] | ||
| np.ndarray | ||
| list[np.ndarray] | ||
|
@@ -442,7 +458,7 @@ GroupByObjectNonScalar: TypeAlias = ( | |
| Grouper | ||
| list[Grouper] | ||
) | ||
GroupByObject: TypeAlias = Scalar | Index | GroupByObjectNonScalar | ||
GroupByObject: TypeAlias = Scalar | Index | GroupByObjectNonScalar | Series | ||
|
||
StataDateFormat: TypeAlias = Literal[ | ||
"tc", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -985,6 +985,20 @@ def test_types_groupby_any() -> None: | |
) | ||
|
||
|
||
def test_types_groupby_iter() -> None: | ||
df = pd.DataFrame(data={"col1": [1, 1, 2], "col2": [3, 4, 5]}) | ||
series_groupby = pd.Series([True, True, False], dtype=bool) | ||
first_group = next(iter(df.groupby(series_groupby))) | ||
check( | ||
assert_type(first_group[0], "bool"), | ||
bool, | ||
) | ||
check( | ||
assert_type(first_group[1], "pd.DataFrame"), | ||
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. You don't need quotes around the types in the two |
||
pd.DataFrame, | ||
) | ||
|
||
|
||
def test_types_merge() -> None: | ||
df = pd.DataFrame(data={"col1": [1, 1, 2], "col2": [3, 4, 5]}) | ||
df2 = pd.DataFrame(data={"col1": [1, 1, 2], "col2": [0, 1, 0]}) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -731,6 +731,20 @@ def test_types_group_by_with_dropna_keyword() -> None: | |||||||||
s.groupby(level=0).sum() | ||||||||||
|
||||||||||
|
||||||||||
def test_types_groupby_iter() -> None: | ||||||||||
s = pd.Series([1, 1, 2], dtype=int) | ||||||||||
series_groupby = pd.Series([True, True, False], dtype=bool) | ||||||||||
first_group = next(iter(s.groupby(series_groupby))) | ||||||||||
check( | ||||||||||
assert_type(first_group[0], "bool"), | ||||||||||
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. no quotes needed here |
||||||||||
bool, | ||||||||||
) | ||||||||||
check( | ||||||||||
assert_type(first_group[1], "pd.Series[int]"), | ||||||||||
pd.Series, | ||||||||||
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.
Suggested change
You do need quotes here because |
||||||||||
) | ||||||||||
|
||||||||||
|
||||||||||
def test_types_plot() -> None: | ||||||||||
s = pd.Series([0, 1, 1, 0, -10]) | ||||||||||
if TYPE_CHECKING: # skip pytest | ||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.