Skip to content
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

Mypy complains about slicing a DateTimeIndex #397

Open
gandhis1 opened this issue Oct 24, 2022 · 5 comments
Open

Mypy complains about slicing a DateTimeIndex #397

gandhis1 opened this issue Oct 24, 2022 · 5 comments
Labels
mypy bug Requires mypy to fix a bug

Comments

@gandhis1
Copy link
Contributor

gandhis1 commented Oct 24, 2022

Shortened code:

  reveal_type(start) # Union[datetime.datetime, datetime.date, pandas._libs.tslibs.timestamps.Timestamp, None]"
  reveal_type(end)  # Union[datetime.datetime, datetime.date, pandas._libs.tslibs.timestamps.Timestamp, None]"
  concat_df = concat_df.sort_index().loc[start:end, :]

Error:

error: Slice index must be an integer or None

I don't know yet whether this is a stubs issue or potentially a MyPy issue - could even be an issue with my code - I will investigate further

@gandhis1
Copy link
Contributor Author

On second glance, this may be something where my code hasn't narrowed down the index type of DataFrame to a DatetimeIndex, and if it could do that, then this code would work. But how do I go about doing that properly?

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Oct 24, 2022

I think you'll need a more complete example that can be tested by others.

Having said that, I get the same error on this simple code with mypy, so it seems it is a mypy problem, as pyright allows this:

import pandas as pd
df = pd.DataFrame({"c1":[1,2,3,4], "c2":[10,20,30,40]}, index=["a", "b", "c" , "d"])

x =  df.loc["b":"c", :]

Seems like a long standing issue. See
python/mypy#2410
python/typeshed#8647

I'm going to close this given that it seems to be a known issue with mypy

@Dr-Irv Dr-Irv closed this as completed Oct 24, 2022
@Dr-Irv Dr-Irv added the mypy bug Requires mypy to fix a bug label Oct 24, 2022
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Feb 17, 2023

reopening so we can track if mypy ever fixes the bug

@Dr-Irv Dr-Irv reopened this Feb 17, 2023
@gandhis1
Copy link
Contributor Author

gandhis1 commented Oct 30, 2023

It seems that PEP 696 is going to be deferred indefinitely. I was thinking - given that now typeshed added SupportsIndex to slice, are there any downsides to adding an __index__ method to pd.Timestamp so that at least there's some semblance of support? It would be technically incorrect, but if there are no downsides, it would provide a stop-gap until the PEP moves forward. Of course, this wouldn't be viable with datetime or np.datetime64, and it doesn't fix the test case I put in the original post of this thread, but I suspect a good number of users are using pandas timestamps.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Oct 30, 2023

It seems that PEP 696 is going to be deferred indefinitely. I was thinking - given that now typeshed added SupportsIndex to slice, are there any downsides to adding an __index__ method to pd.Timestamp so that at least there's some semblance of support? It would be technically incorrect, but if there are no downsides, it would provide a stop-gap until the PEP moves forward. Of course, this wouldn't be viable with datetime or np.datetime64, and it doesn't fix the test case I put in the original post of this thread, but I suspect a good number of users are using pandas timestamps.

I'm open to a PR that does this. Should probably add a comment that you're adding __index__ to Timestamp to support slicing.

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

No branches or pull requests

2 participants