Skip to content

Return type from pandas.core.groupby.generic.DataFrameGroupBy.resample #825

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

Closed
vectro opened this issue Dec 10, 2023 · 1 comment · Fixed by #848
Closed

Return type from pandas.core.groupby.generic.DataFrameGroupBy.resample #825

vectro opened this issue Dec 10, 2023 · 1 comment · Fixed by #848

Comments

@vectro
Copy link

vectro commented Dec 10, 2023

Describe the bug
The type hint for pandas.core.groupby.generic.DataFrameGroupBy.resample says it returns a pandas.core.groupby.grouper.Grouper; however in at least some situations this function can return a pandas.core.resample.DatetimeIndexResamplerGroupby, which is not a Grouper.

To Reproduce
This results in a false positive on MyPy 1.7.1:

df.set_index("date").groupby("intensity").resample("90D")["duration"]

The error message is

error: Value of type "Grouper" is not indexable  [index]

Please complete the following information:
Python 3.11
Mypy 1.7.1
pandas-stubs 2.1.1.230928

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Dec 11, 2023

Thanks for the report. Looking at the docs, we should change

to return a union of pandas.api.typing.DatetimeIndexResamplerGroupby,
pandas.api.typing.PeriodIndexResamplerGroupby, and
pandas.api.typing.TimedeltaIndexResamplerGroupby

Unfortunately, we haven't added the types for pandas.api.typing yet, nor do we have DatetimeIndexResamplerGroupby in core/resample.pyi . So that latter type should get added there, and can be the return type of resample(), since the other 2 types are subclasses of the first one.

Some care will need to be exercised, because in the stubs we are doing some things with respect to generic types that will have to be appropriately passed down for things to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants