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

Bug with iterating df.columns #502

Closed
jonashaag opened this issue Jan 9, 2023 · 3 comments · Fixed by #760
Closed

Bug with iterating df.columns #502

jonashaag opened this issue Jan 9, 2023 · 3 comments · Fixed by #760
Labels
Generic Index Issues where a Generic Index would help

Comments

@jonashaag
Copy link

Describe the bug

import pandas as pd

t: pd.DataFrame
s: list[str] = list(t.columns)
t.py:4: error: Argument 1 to "list" has incompatible type "Index"; expected
"Iterable[str]"
    s: list[str] = list(t.columns)
                        ^~~~~~~~~
t.py:4: note: Following member(s) of "Index" have conflicts:
t.py:4: note:     Expected:
t.py:4: note:         def __iter__(self) -> Iterator[str]
t.py:4: note:     Got:
t.py:4: note:         def __iter__(self) -> Iterator[Union[Union[str, bytes, date, datetime, timedelta, bool, int, float, complex, Timesta
mp, Timedelta], Tuple[Hashable, ...]]]
Found 1 error in 1 file (checked 1 source file)

Variant:

import pandas as pd

t: pd.DataFrame
s: str
for s in t.columns:
    pass
t.py:5: error: Incompatible types in assignment (expression has type
"Union[Union[str, bytes, date, datetime, timedelta, bool, int, float, complex, Timestamp, Timedelta], Tuple[Hashable, ...]]",
variable has type "str")
    for s in t.columns:
    ^
Found 1 error in 1 file (checked 1 source file)

To Reproduce
See above

Please complete the following information:

  • Version 1.5.2.230105
  • MyPy 0.991
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jan 9, 2023

You have to use cast here for now. We are not tracking the type of values inside of an Index. This is on our TODO list. But even with your example, there is no way to know the dtype of t.columns for any particular DataFrame.

You'd get a similar error in your first example like this:

import pandas as pd

t: pd.DataFrame
s: list[int] = list(t.columns)

So the solution here is to do:

import pandas as pd
from typing import cast

t: pd.DataFrame
s: list[str] = cast(list[str], list(t.columns))

@Dr-Irv Dr-Irv added the Generic Index Issues where a Generic Index would help label Jan 9, 2023
@davetapley
Copy link
Contributor

I also ran in to this using date_range.

Workaround is good for now:

for ts in cast(Iterator[datetime], date_range(start='1/1/2023', end='1/08/2023', freq='6H', tz=timezone.utc)):
   ....

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jun 1, 2023

I also ran in to this using date_range.

Different issue - this can be fixed. Created a new issue #723

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Generic Index Issues where a Generic Index would help
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants