Skip to content

DataFrame.loc[] accepts list[str] #1162

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
ndawe opened this issue Mar 12, 2025 · 6 comments · Fixed by #1165
Closed

DataFrame.loc[] accepts list[str] #1162

ndawe opened this issue Mar 12, 2025 · 6 comments · Fixed by #1165
Labels
good first issue Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@ndawe
Copy link

ndawe commented Mar 12, 2025

Describe the bug

DataFrame.loc[] should accept list[str] as stated in the pandas docs: "A list or array of labels, e.g. ['a', 'b', 'c']."

To Reproduce

import pandas as pd                                                             
                                                                                
df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],                                     
                  index=['cobra', 'viper', 'sidewinder'],                       
                  columns=['max_speed', 'shield'])                              
                                                                                
df.loc[['viper', 'sidewinder']] 
$ mypy pd_test.py 
pd_test.py:7: error: Invalid index type "list[str]" for "_LocIndexerFrame[DataFrame]"; expected type "slice[Any, Any, Any] | ndarray[Any, dtype[integer[Any]]] | Index[Any] | list[int] | Series[int] | <6 more items>"  [index]
Found 1 error in 1 file (checked 1 source file)
1

Please complete the following information:

  • OS: Linux
  • OS Version: Ubuntu 24.04.2 LTS
  • python version: 3.12.9
  • version of type checker: mypy 1.15.0 (compiled: yes)
  • version of installed pandas-stubs: 2.2.3.250308
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 12, 2025

df.loc[['viper', 'sidewinder']]

To avoid some ambiguities from a typing perspective, we recommend using one of the following 2 patterns:

df.loc[:, ['viper', 'sidewinder']]
df[['viper', 'sidewinder']]

@ndawe
Copy link
Author

ndawe commented Mar 12, 2025

Those examples attempt to select columns, but df.loc[['viper', 'sidewinder']] selects labels along the index.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 12, 2025

Those examples attempt to select columns, but df.loc[['viper', 'sidewinder']] selects labels along the index.

Then use df.loc[['viper', 'sidewinder'], :]

@ndawe
Copy link
Author

ndawe commented Mar 12, 2025

Yes, and is it possible for the type declarations here to support .loc[Sequence]? If that's difficult because of ambiguity then I'm happy to switch to the df.loc[['viper', 'sidewinder'], :] pattern.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 12, 2025

Yes, and is it possible for the type declarations here to support .loc[Sequence]? If that's difficult because of ambiguity then I'm happy to switch to the df.loc[['viper', 'sidewinder'], :] pattern.

Actually, yes. This revealed a bug. Need to change

| list[Hashable]
from list[Hashable] to list[HashableT]

PR with tests welcome.

@Dr-Irv Dr-Irv added good first issue Indexing Related to indexing on series/frames, not to indexes themselves labels Mar 12, 2025
@shirzady1934
Copy link
Contributor

@Dr-Irv I have made the required changes. Please review and let me know if anything else is needed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
3 participants