Skip to content

Add callable condition type in Series.mask, DataFrame.mask #918

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

Merged

Conversation

georgesittas
Copy link
Contributor

@georgesittas georgesittas commented May 9, 2024

I played around a bit and found that Series.mask() actually had the same issue as DataFrame.mask():

import pandas as pd
s = pd.Series([1]).mask(cond=lambda x: x % 2 == 1, other=2)
print(s)
# 0    2
# dtype: int64
(.venv) ➜  test python -m mypy test.py
test.py:2: error: Argument "cond" to "mask" of "Series" has incompatible type "Callable[[Any], Any]"; expected "Series[bool] | ndarray[Any, dtype[bool_]] | list[bool]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

I tried to fix both in this PR. The new cond types match those used in Series.where and DataFrame.where. Let me know if it's desirable to factor the cond type out by moving it to _typing.pyi.

@georgesittas
Copy link
Contributor Author

georgesittas commented May 9, 2024

CI failed with:

error: Unnecessary "# pyright: ignore" rule: "reportIncompatibleMethodOverride"

I saw this one locally as well, but wasn't sure if it was due to my configuration. Should we get rid of that rule from the ignore hint? The test suite ran fine for me locally after removing it, but haven't investigated what it does / implications of removing it.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented May 9, 2024

CI failed with:

error: Unnecessary "# pyright: ignore" rule: "reportIncompatibleMethodOverride"

I saw this one locally as well, but wasn't sure if it was due to my configuration. Should we get rid of that rule from the ignore hint? The test suite ran fine for me locally after removing it, but haven't investigated what it does / implications of removing it.

I am guessing that pyright has a new version that says that the rule is unnecessary, so you can remove it as part of this PR. It won't have an effect - we put those in there when we disagree with the type checker, so in this case, they fixed a bug making the ignore statement unnecessary.

@georgesittas
Copy link
Contributor Author

Got it - done.

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @georgesittas

@Dr-Irv Dr-Irv merged commit d89824b into pandas-dev:main May 9, 2024
13 checks passed
@georgesittas georgesittas deleted the jo/handle_callables_in_mask_method branch May 9, 2024 15:01
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 this pull request may close these issues.

Passing a callable to DataFrame.mask's 'cond' argument results in arg-type error (mypy)
2 participants