Skip to content

Wrong type hint for Series.all() and Series.any() #1180

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
Igorsmit00 opened this issue Apr 9, 2025 · 2 comments · Fixed by #1188
Closed

Wrong type hint for Series.all() and Series.any() #1180

Igorsmit00 opened this issue Apr 9, 2025 · 2 comments · Fixed by #1188

Comments

@Igorsmit00
Copy link

The Series.all() and Series.any() have the type bool for the output. However, the real output type is np.bool. This causes issues when trying to compile a function using mypyc.

To Reproduce
Compile the following code

def test_function(df: pd.Dataframe):
# some code
is_all_nan = df["column_name"].isna().all()
# some more code

Typechecker mypy/mypyc
TypeError: bool object expected; got numpy.bool_

  • OS: Windows
  • OS Version: 11
  • python version 3.11.6=7
  • mypy version 1.8.0
  • pandas-stubs version 2.2.3.250308
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Apr 9, 2025

Can you indicate the version of pandas and the version of numpy that you are using?

Also, a more complete example that fails with the type checker would be helpful.

@loicdiridollou
Copy link
Contributor

I looked into it by curiosity and here is a more comprehensive test.
@Igorsmit00 is correct that the type is not exact as pandas most likely uses a numpy function under the hood so the returns should be np.bool and not bool.

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2], [3, 4]])
>>> df
   0  1
0  1  2
1  3  4
>>> df[o]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'o' is not defined
>>> df[0]
0    1
1    3
Name: 0, dtype: int64
>>> df[0].isna()
0    False
1    False
Name: 0, dtype: bool
>>> df[0].isna().any()
np.False_
>>> df[0].isna().all()
np.False_
>>> (~df[0].isna())
0    True
1    True
Name: 0, dtype: bool
>>> (~df[0].isna()).all()
np.True_
>>> pd.__version__
'2.2.3'

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 a pull request may close this issue.

3 participants