Skip to content
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

In DataFrame.select_dtypes type values like np.number or np.datetime64 should be allowed #887

Closed
benediktsatalia opened this issue Mar 7, 2024 · 2 comments · Fixed by #900
Labels
Dtype Conversions Unexpected or buggy dtype conversions

Comments

@benediktsatalia
Copy link

Describe the bug
The documentation of DataFrame.select_dtypes clearly states that it is allowed to use values like np.number or np.datetime64 for the include and exclude parameters, but the current stubs don't allow it, they enforce include: _str | list[_str] | None and exclude: _str | list[_str] | None. This leads to typecheckers like mypy reporting false positives on such calls.

To Reproduce
If I run mypy on the following short snippet I get an error:

import numpy as np
import pandas as pd

df = pd.DataFrame()
df.select_dtypes(np.number)

the error is:

min_example.py:5: error: Argument 1 to "select_dtypes" of "DataFrame" has incompatible type "type[number[Any]]"; expected "str | list[str] | None"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Please complete the following information:

  • OS: Linux
  • OS Version: Arch Linux
  • python version: 3.11
  • version of type checker: mypy 1.8.0
  • version of installed pandas-stubs: 2.2.0.240218
@Dr-Irv Dr-Irv added the Dtype Conversions Unexpected or buggy dtype conversions label Mar 7, 2024
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 7, 2024

Thanks for the report. Definitely needs improvement on that particular stub.

Should use AsTypeArg from pandas-stubs/_typing.pyi instead of str PLUS add the other possible strings (e.g., "number") and object types (e.g., np.number) listed in the docs.

PR plus tests welcome.

mutricyl pushed a commit to mutricyl/pandas-stubs that referenced this issue Apr 2, 2024
@mutricyl
Copy link
Contributor

mutricyl commented Apr 2, 2024

I made an attempt for this issue. It is not that simple to cover all the restrictions stated in the documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants