Skip to content

DataFrame.__getitem__ returns wrong type for a dict_keys object #770

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 31, 2023 · 2 comments · Fixed by #771
Closed

DataFrame.__getitem__ returns wrong type for a dict_keys object #770

gandhis1 opened this issue Aug 31, 2023 · 2 comments · Fixed by #771

Comments

@gandhis1
Copy link
Contributor

gandhis1 commented Aug 31, 2023

import pandas as pd

some_columns = {
    "a": 1,
    "b": 2,
}
x  = pd.DataFrame.from_dict(some_columns)
y = x[some_columns.keys()]
reveal_type(y)  # note: Revealed type is "pandas.core.series.Series[Any]"

__getitem__ with a Generator is already annotated correctly, not clear to me why it doesn't use that overload here.

@twoertwein
Copy link
Member

Generator requires a few different methods. I assume dict.keys() is Hashable and therefore the first matching overload is the Series overload.

Assuming str is the only iterable that we want to special case (@Dr-Irv), we could first have an overload for str, and then use Iterable (covers, lists, generators and dict.keys) but add an ignore comment (overlapping overloads).

There might be more places were we could first special case the exceptions, e.g., str, to then replace large list-like Unions with Iterable+ignore.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Aug 31, 2023

Assuming str is the only iterable that we want to special case (@Dr-Irv), we could first have an overload for str, and then use Iterable (covers, lists, generators and dict.keys) but add an ignore comment (overlapping overloads).

No issue for me on that. PR welcome

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.

3 participants