Closed
Description
Describe the bug
df.columns.str.match(reg_ex_pattern)
actually gives npt.NDArray[np.bool_]
, but mypy thinks it's pd.Index[str]
To Reproduce
Provide a minimal runnable pandas
example that is not properly checked by the stubs.
from typing import TYPE_CHECKING, cast
import numpy as np
import pandas as pd
if TYPE_CHECKING:
from numpy import typing as npt
df = pd.DataFrame({"1": [2, 3], "2": 3, "4": 5, "a": 1})
mask = df.columns.str.match(r"\d")
print(mask) # array([ True, True, True, False])
print(type(mask)) # <class 'numpy.ndarray'>
df.loc[:, mask] # mypy: error: Invalid index type "tuple[slice, Index[str]]" for "_LocIndexerFrame"; expected type "slice | ndarray[Any, dtype[integer[Any]]] | Index[Any] | list[int] | Series[int] | <6 more items>"
df.loc[:, cast("npt.NDArray[np.bool_]", mask)] # mypy: fine
Indicate which type checker you are using (mypy
or pyright
).
I am using mypy
.
Show the error message received from that type checker while checking your example.
error: Invalid index type "tuple[slice, Index[str]]" for "_LocIndexerFrame"; expected type "slice | ndarray[Any, dtype[integer[Any]]] | Index[Any] | list[int] | Series[int] | <6 more items>"
Please complete the following information:
- OS: Windows
- OS Version:
cmd /c ver # Microsoft Windows [Version 10.0.19045.4651]
- python version
poetry run python --version # Python 3.12.5
- version of type checker
poetry run mypy --version # mypy 1.11.1 (compiled: yes)
- version of installed
pandas-stubs
poetry run pip freeze | findstr pandas-stubs # pandas-stubs==2.2.2.240603
Additional context
Nothing