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

Index.intersection does not return correct sub type #744

Closed
StefanBrand opened this issue Jul 12, 2023 · 4 comments · Fixed by #760
Closed

Index.intersection does not return correct sub type #744

StefanBrand opened this issue Jul 12, 2023 · 4 comments · Fixed by #760
Assignees
Labels
good first issue Index Related to the Index class or subclasses

Comments

@StefanBrand
Copy link

Describe the bug
When I form the intersection of two pd.DatetimeIndex, the return type is Index. This leads to problems with index entries: mypy complains about intersection[0].date().

To Reproduce

  1. Minimal example
import pandas as pd
intersection = pd.DatetimeIndex().intersection(pd.DatetimeIndex)

reveal_type(intersection)  # Index
reveal_type(intersection[0])  # "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex"
  1. mypy

  2. error message

agri_data/utils.py:96: error: Item "str" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "bytes" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "date" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "timedelta" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "datetime64" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "timedelta64" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "bool" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "int" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "float" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "Timedelta" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
agri_data/utils.py:96: error: Item "complex" of "str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex" has no attribute "date"  [union-attr]
  1. Expected type: reveal_type(intersection[0]) # Timestamp

Please complete the following information:

  • OS: Ubuntu
  • OS Version: bullseye
  • python version: 3.11.1
  • version of type checker: 1.2.0
  • version of installed pandas-stubs: 2.0.2.230605

Additional context
Possibly related: #723

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jul 12, 2023

Thanks for the report.

Need to add intersection() to pandas-stubs/core/indexes/datetimes.pyi

@Dr-Irv Dr-Irv added Index Related to the Index class or subclasses good first issue labels Jul 12, 2023
@teacherc
Copy link

take

@teacherc
Copy link

@Dr-Irv Can I work on this issue? (it wasn't assigned to me automatically with 'take')

@teacherc
Copy link

@Dr-Irv Hello there! I'm new to open source. I just did a pull request. Let me know if you have any changes/questions. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Index Related to the Index class or subclasses
Projects
None yet
3 participants