BUG: loc.__getitem__ stricter than it should be with list indexers
See original GitHub issueDescribe the bug
loc.__getitem__
is stricter than it should be I think
error: List item 0 has incompatible type "int"; expected "str" [list-item]
error: List item 0 has incompatible type "None"; expected "str" [list-item]
To Reproduce
- Provide a minimal runnable
pandas
example that is not properly checked by the stubs. - Indicate which type checker you are using (
mypy
orpyright
). - Show the error message received from that type checker while checking your example.
df = pd.DataFrame([[1, 2]], columns=Index([1, None], dtype=object))
df.loc[:, [None]]
df.loc[:, [1]]
Alternatively you could define df as
df = pd.DataFrame({"a": [1, 2], None: 5})
Both of them should be valid. Especially since
df.loc[:, None]
df.loc[:, 1]
both work.
Please complete the following information:
- OS: ubuntu
- OS Version 22.04
- python version 3.10
- version of type checker mypy 0.971
- version of installed
pandas-stubs
newest 20220807
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
IndexError: positional indexers are out-of-bounds iloc boolean ...
I have confirmed this bug exists on the latest version of pandas. ... boolean array labels should be treated differently than list labels....
Read more >Indexing and selecting data — pandas 1.5.2 documentation
loc is strict when you present slicers that are not compatible (or convertible) with the index type. For example using integers in a...
Read more >Indexing and selecting data with Pandas
Pandas now supports three types of multi-axis indexing. .loc is primarily label based, but may also be used with a boolean array. .loc...
Read more >MultiIndex / advanced indexing — pandas 1.0.0rc0+111 ...
A MultiIndex can be created from a list of arrays (using ... cases where the passed indexer could be mis-interpreted as indexing both...
Read more >SettingWithCopyWarning in Pandas: Views vs Copies
Indexing in NumPy: Copies and Views; Indexing in Pandas: Copies and Views ... you should know about the SettingWithCopyWarning is that it's not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No, it’s just a result of how the types evolved over time by Microsoft.
I guess we have to change
StrLike
toHashable
orHashableT
based on whether it is “alone” (useHashable
) or as a parameter to a type, e.g.,list[StrLike]
becomeslist[HashableT]
It does, but you have to use different column names. You can either use a non integer as the other columns or do columns=Index([1, None], dtype=object)
if the column is a true None and not NaN this returns the column as expected
Edit: Sorry for the shit example in the op, don’t know how I mixed that up. Updated it now