Closed
Description
When using a multi-index dataframe, accessing values with loc returns another dataframe. However, mypy seems to report this as an error expecting a series. I extensively use multi-index dataframes and it makes usage of stubs very difficult.
To Reproduce
- Provide a minimal runnable
pandas
example that is not properly checked by the stubs.
import pandas as pd
def main() -> None:
idx = pd.MultiIndex.from_product(
[["a", "b"], [1, 2]], names=["alpha", "num"]
)
df = pd.DataFrame({"a": range(4)}, index=idx)
df2: pd.DataFrame = df.loc["a"]
print(type(df))
print(df)
print(type(df2))
print(df2)
# reveal_locals()
if __name__ == "__main__":
main()
- Indicate which type checker you are using (
mypy
orpyright
).
mypy - Show the error message received from that type checker while checking your example.
demo.py:13: error: Incompatible types in assignment (expression has type "Series[Any]", variable has type "DataFrame") [assignment]
Please complete the following information:
- OS: [e.g. Windows, Linux, MacOS]
Linux - OS Version [e.g. 22]
CentOS Linux release 7.8.2003 (Core) - python version
Python 3.8 - version of type checker
mypy 1.4.1 (compiled: yes) - version of installed
pandas-stubs
pandas-stubs=2.0.1.230501=pyhd8ed1ab_0
Additional context
Add any other context about the problem here.
$ python demo.py
<class 'pandas.core.frame.DataFrame'>
a
alpha num
a 1 0
2 1
3 2
b 1 3
2 4
3 5
c 1 6
2 7
3 8
<class 'pandas.core.frame.DataFrame'>
a
num
1 0
2 1
3 2
Process finished with exit code 0