Skip to content

All occurences of freq should be a Union[str, BaseOffset] #223

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
gandhis1 opened this issue Aug 24, 2022 · 8 comments
Closed

All occurences of freq should be a Union[str, BaseOffset] #223

gandhis1 opened this issue Aug 24, 2022 · 8 comments

Comments

@gandhis1
Copy link
Contributor

gandhis1 commented Aug 24, 2022

The one bug I could find was the method bdate_range which is annotated as a str, but my suspicion is there are other occurrences as freq is a common parameter

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Aug 24, 2022

Please provide an example that illustrates a type checker showing an error.

@gandhis1
Copy link
Contributor Author

import pandas as pd
from pandas.tseries.offsets import Day, BusinessDay

x = pd.date_range("1/1/2022", "2/1/2022", freq="1D")
y = pd.date_range("1/1/2022", "2/1/2022", freq=Day())
z = pd.bdate_range("1/1/2022", "2/1/2022", freq=BusinessDay())

test_case.py:6: error: Argument "freq" to "bdate_range" has incompatible type "BusinessDay"; expected "str"
Found 1 error in 1 file (checked 1 source file)

@ojdo
Copy link

ojdo commented Sep 7, 2022

Sorry to piggy-back here, but this seems like the best place to ask: in a tiny script, mypy complains about

Argument "freq" to "date_range" has incompatible type "timedelta"; expected "Union[str, BaseOffset]"

As far as I understand, and offsets.pyi seems to agree, argument "freq" should also support timedelta. Is this a bug in mypy, or should one of the lines in this PR include another timedelta | somewhere?

If so, I would be happy to provide a PR. I am just very new to mypy with such heavy stubbing.

@gandhis1
Copy link
Contributor Author

gandhis1 commented Sep 7, 2022

The Pandas documentation for some functions that accept an offset does not seem to include timedelta. This interface may vary.

https://pandas.pydata.org/docs/reference/api/pandas.date_range.html

@gandhis1
Copy link
Contributor Author

gandhis1 commented Sep 7, 2022

I will note that this may be in error as the underlying code ultimately calls to_offset. So whatever that supports should be supported by the overlying function.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Sep 7, 2022

As far as I understand, and offsets.pyi seems to agree, argument "freq" should also support timedelta. Is this a bug in mypy, or should one of the lines in this PR include another timedelta | somewhere?

The main issue here is that the docs for pd.date_range() don't say that a timedelta (or pd.Timedelta()) is a valid argument for freq, but the implementation accepts it. With the stubs, we have been following what's documented.

So can you open an issue in the pandas repo saying that pd.date_range accepts timedelta for the freq argument, but it's not documented, and then a determination can be made by the pandas team whether the docs are wrong or the implementation is wrong.

If the docs are wrong, then create a new issue here to update the stubs. If the implementation is wrong, we leave things here as they are.

@gandhis1
Copy link
Contributor Author

My PR to update the Pandas documentation to include timedelta was just accepted. I can open a PR here to match later, unless anyone else wants to go ahead and take care of it.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Sep 21, 2022

My PR to update the Pandas documentation to include timedelta was just accepted. I can open a PR here to match later, unless anyone else wants to go ahead and take care of it.

Please submit a PR here. Thanks!

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

No branches or pull requests

4 participants