Skip to content

is_numeric_dtype not considering Index as a valid type #415

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
williamjamir opened this issue Nov 8, 2022 · 10 comments · Fixed by #422
Closed

is_numeric_dtype not considering Index as a valid type #415

williamjamir opened this issue Nov 8, 2022 · 10 comments · Fixed by #422

Comments

@williamjamir
Copy link

williamjamir commented Nov 8, 2022

Describe the bug
As stated in the docs, the is_numeric_dtype function accepts Dtypes, Arrays, and Index as well.
https://pandas.pydata.org/docs/reference/api/pandas.api.types.is_numeric_dtype.html?highlight=example

However, the typings are only contemplating _ArrayOrDtype

def is_numeric_dtype(arr_or_dtype: _ArrayOrDtype) -> bool: ...

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
from pandas.core.dtypes.common import is_numeric_dtype

is_numeric_dtype(pd.Index([1, 2.0]))
>>> mypy --version 
mypy 0.982 (compiled: yes)

Show the error message received from that type checker while checking your example.
(I put one type per line to make the output more readable)

>>> mypy test.py     
/tmp/test.py:4: error: Argument 1 to "is_numeric_dtype" has incompatible type "Index"; 
expected 
"
Union[
    Union[ExtensionArray, ndarray[Any, Any]],
    Union[
        dtype[Any],
        None,
        Type[Any],
        _SupportsDType[dtype[Any]],
        str,
        Union[
            Tuple[Any, int],
            Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]],
            List[Any],
            _DTypeDict,
            Tuple[Any, Any],
        ],
    ],
    Series[Any],
    DataFrame,
]

"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Please complete the following information:
OS: MacOS M1
OS Version Ventura 13.0
python version: 3.9.12
version of type checker: mypy 0.982 (compiled: yes)
version of installed pandas-stubs
pandas==1.5.1
pandas-stubs==1.5.1.221024

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Nov 8, 2022

Need to expand definition of _ArrayOrDtype to include pd.Index at

_ArrayOrDtype: TypeAlias = Union[ArrayLike, npt.DTypeLike, pd.Series, pd.DataFrame]

Need to add more tests to tests/test_api_types.py to cover the various types that can be passed to is_numeric_dtype as well as the other functions defined in pandas-stubs/core/dtypes/common.pyi .

Maybe @bashtage can have a look, although this should be pretty straightforward to do. Just have to add lots of tests.

@ramvikrams
Copy link
Contributor

ramvikrams commented Nov 10, 2022


def test_is_numeric_dtype() -> None:
    check(assert_type(api.is_numeric_dtype(pd.Index([1, 2.0])), bool), bool)

Sir can you please tell if this test is correct for pd.Index.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Nov 10, 2022


def test_is_numeric_dtype() -> None:
    check(assert_type(api.is_numeric_dtype(pd.Index([1, 2.0])), bool), bool)

Sir can you please tell if this test is correct for pd.Index.

Yes, it appears to be so. If you work on this, you should add a lot of tests for all the other functions there, as well as the other types that those functions accept. Test locally before creating a PR.

@ramvikrams
Copy link
Contributor

Yes sir

@ramvikrams
Copy link
Contributor

ramvikrams commented Nov 10, 2022

Sir I ran test on my local system it is working with no issues and for as well as the othe types that those functions accept are you talking about adding test for pd.DataFrame, pd.Series

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Nov 10, 2022

Sir I ran on my local system it is working with no issues and as for as well as the othe types that those functions accept are you talking about adding test for pd.DataFrame, pd.Series

Yes, and for the other functions that are in pandas-stubs/pandas-stubs/core/dtypes/common.pyi

What you should do is modify each of the single test functions that exist for each of the functions that are in there. E.g., one for is_numeric_dtype, one for is_float_dtype , etc. Then within each function, you have a test for the 5 possible argument types: ArrayLike, npt.DTypeLike, pd.Series, pd.DataFrame, pd.Index . Only do this for the functions that accept _ArrayOrDtype as an argument type.

@ramvikrams
Copy link
Contributor

Sir I ran on my local system it is working with no issues and as for as well as the othe types that those functions accept are you talking about adding test for pd.DataFrame, pd.Series

Yes, and for the other functions that are in pandas-stubs/pandas-stubs/core/dtypes/common.pyi

What you should do is modify each of the single test functions that exist for each of the functions that are in there. E.g., one for is_numeric_dtype, one for is_float_dtype , etc. Then within each function, you have a test for the 5 possible argument types: ArrayLike, npt.DTypeLike, pd.Series, pd.DataFrame, pd.Index . Only do this for the functions that accept _ArrayOrDtype as an argument type.

Thanks sir I'll do it

@ramvikrams
Copy link
Contributor

Sir just a small question for ArrayLike should I use pd.array and for npt.DtypeLike should I use np.array for writing tests for them respectively

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Nov 10, 2022

For ArrayLike, use np.array (already declared at top as nparr. For npt.DtypeLike, use np.dtype(np.int32) npt.DtypeLike is a numpy dtype, so let's pass in a dtype for that test.

@ramvikrams
Copy link
Contributor

Thanks Sir

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