diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 2920b8252..85c5de1b8 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -48,6 +48,7 @@ from pandas.core.indexes.timedeltas import TimedeltaIndex from pandas.core.indexing import ( _AtIndexer, _iAtIndexer, + _IndexSliceTuple, ) from pandas.core.resample import Resampler from pandas.core.strings import StringMethods @@ -131,21 +132,21 @@ class _iLocIndexerSeries(_iLocIndexer, Generic[S1]): ) -> None: ... class _LocIndexerSeries(_LocIndexer, Generic[S1]): + # ignore needed because of mypy. Overlapping, but we want to distinguish + # having a tuple of just scalars, versus tuples that include slices or Index @overload - def __getitem__( + def __getitem__( # type: ignore[misc] self, - idx: MaskType - | Index - | Sequence[float] - | list[str] - | slice - | tuple[str | float | slice | Index, ...], - ) -> Series[S1]: ... + idx: Scalar | tuple[Scalar, ...], + # tuple case is for getting a specific element when using a MultiIndex + ) -> S1: ... @overload def __getitem__( self, - idx: str | float, - ) -> S1: ... + idx: MaskType | Index | Sequence[float] | list[str] | slice | _IndexSliceTuple, + # _IndexSliceTuple is when having a tuple that includes a slice. Could just + # be s.loc[1, :], or s.loc[pd.IndexSlice[1, :]] + ) -> Series[S1]: ... @overload def __setitem__( self, diff --git a/tests/test_series.py b/tests/test_series.py index 75061d886..c5c1fc556 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -120,9 +120,12 @@ def test_types_loc_at() -> None: def test_multiindex_loc() -> None: - s = pd.Series([1, 2, 3, 4], index=pd.MultiIndex.from_product([[1, 2], ["a", "b"]])) - check(assert_type(s.loc[1, :], pd.Series), pd.Series) - check(assert_type(s.loc[pd.Index([1]), :], pd.Series), pd.Series) + s = pd.Series( + [1, 2, 3, 4], index=pd.MultiIndex.from_product([[1, 2], ["a", "b"]]), dtype=int + ) + check(assert_type(s.loc[1, :], "pd.Series[int]"), pd.Series, int) + check(assert_type(s.loc[pd.Index([1]), :], "pd.Series[int]"), pd.Series, int) + check(assert_type(s.loc[1, "a"], int), np.int_) def test_types_boolean_indexing() -> None: