question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: loc.__getitem__ stricter than it should be with list indexers

See original GitHub issue

Describe 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

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
  2. Indicate which type checker you are using (mypy or pyright).
  3. 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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Dr-Irvcommented, Aug 10, 2022

I think thats a bit different, the list elements are restricted to str, was this deliberate?

No, it’s just a result of how the types evolved over time by Microsoft.

I guess we have to change StrLike to Hashable or HashableT based on whether it is “alone” (use Hashable) or as a parameter to a type, e.g., list[StrLike] becomes list[HashableT]

0reactions
phoflcommented, Aug 13, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found