-
-
Notifications
You must be signed in to change notification settings - Fork 150
Closed
Labels
Description
Describe the bug
Using pandas.DataFrame.loc
with a multi-index -- expressed as a tuple[str, str]
, per the syntax described in https://pandas.pydata.org/docs/user_guide/advanced.html#advanced-indexing-with-hierarchical-index -- seems to elicit mypy
errors.
To Reproduce
import itertools
import pandas
def main() -> None:
rows = pandas.Index(["project A", "project B", "project C"])
years: tuple[str, ...] = ("Year 1", "Year 2", "Year 3")
quarters: tuple[str, ...] = ("Q1", "Q2", "Q3", "Q4")
index_tuples: list[tuple[str, ...]] = list(itertools.product(years, quarters))
cols = pandas.MultiIndex.from_tuples(index_tuples)
budget = pandas.DataFrame(index=rows, columns=cols)
multi_index: tuple[str, str] = ("Year 1", "Q1")
budget.loc["project A", multi_index] = 4700
if __name__ == "__main__":
main()
Running mypy
version 0.991 on that source code yields the following error:
Invalid index type "Tuple[str, Tuple[str, str]]" for "_LocIndexerFrame"; expected type "Union[Series[bool], ndarray[Any, dtype[bool_]], List[bool], str, Tuple[Union[Index, Union[Series[bool], ndarray[Any, dtype[bool_]], List[bool]], Union[str, bytes, date, datetime, timedelta, bool, int, float, complex, Timestamp, Timedelta], List[Any], slice], ...], List[<nothing>]]" [index]
Please complete the following information:
- Linux
- Ubuntu 22.04.1
- 3.9.9
- mypy 0.991
- pandas-stubs 1.5.2.221124
Additional context
I tried changing
pandas-stubs/pandas-stubs/core/frame.pyi
Line 161 in 58416f2
list[HashableT] | slice | Series[bool] | Callable, |
_LocIndexerFrame.__getitem__
as well, so I may try that, and if it works, I'll submit a PR.