Skip to content

Series.loc() typecheck fails with a lambda (Callable[[Any], Any]) #586

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
tcrasset opened this issue Mar 22, 2023 · 1 comment · Fixed by #670
Closed

Series.loc() typecheck fails with a lambda (Callable[[Any], Any]) #586

tcrasset opened this issue Mar 22, 2023 · 1 comment · Fixed by #670
Labels
good first issue Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@tcrasset
Copy link

Describe the bug
Series.loc does not accept a lambda (or more generally a Callable[[Any], Any]) to filter the rows. of getitem.

I suspected this is the case for pd.DataFrame too, but I haven't tested it.

To Reproduce

import pandas as pd
s = pd.Series([1, 2])
filtered = s.loc[lambda x: x > 1]

Using mypy.

Error message:

test_loc_with_callable.py:7: error: No overload variant of "__getitem__" of "_LocIndexerSeries" matches argument type "Callable[[Any], Any]"  [call-overload]
    filtered = s.loc[lambda x: x > 1]

Please complete the following information:

  • OS: Linux
  • OS Version: output of uname -a: 5.14.0-1058-oem #66-Ubuntu SMP Fri Feb 10 09:46:18 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
  • python version: Python 3.9.5
  • version of type checker: mypy 1.1.1
  • version of installed pandas-stubs: 1.5.3.230304
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 22, 2023

DataFrame is OK. Need to change

idx: MaskType | Index | Sequence[float] | list[str] | slice | _IndexSliceTuple,

to include Callable

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 22, 2023
@Dr-Irv Dr-Irv mentioned this issue May 1, 2023
4 tasks
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
Development

Successfully merging a pull request may close this issue.

2 participants