Skip to content

Commit

Permalink
TYP: Fix return type of Series.keys() fro list to Index (#1101) (#1106)
Browse files Browse the repository at this point in the history
* Update series.pyi, tackling issue #1101

Imported Index from pandas
and
Changed keys method:
def keys(self) -> Index: ...

* Update test_series.py

Add a test for issue # 1101

* Update tests/test_series.py

Following the pandas pattern.

Co-authored-by: Irv Lustig <[email protected]>

* Fix import issue in series.pyi to resolve Ruff error

* Delete test

---------

Co-authored-by: Irv Lustig <[email protected]>
  • Loading branch information
janlodewijk and Dr-Irv authored Feb 10, 2025
1 parent a516870 commit 4976e11
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ from matplotlib.axes import (
)
import numpy as np
from pandas import (
Index,
Period,
PeriodDtype,
Timedelta,
Expand Down Expand Up @@ -58,7 +59,6 @@ from pandas.core.indexes.accessors import (
TimedeltaProperties,
TimestampProperties,
)
from pandas.core.indexes.base import Index
from pandas.core.indexes.category import CategoricalIndex
from pandas.core.indexes.datetimes import DatetimeIndex
from pandas.core.indexes.interval import IntervalIndex
Expand Down Expand Up @@ -653,7 +653,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
) -> _str: ...
def to_xarray(self) -> xr.DataArray: ...
def items(self) -> Iterable[tuple[Hashable, S1]]: ...
def keys(self) -> list: ...
def keys(self) -> Index: ...
@overload
def to_dict(self, *, into: type[dict] = ...) -> dict[Any, S1]: ...
@overload
Expand Down
6 changes: 6 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3546,6 +3546,12 @@ def test_series_dict() -> None:
)


def test_series_keys_type() -> None:
# GH 1101
s = pd.Series([1, 2, 3])
check(assert_type(s.keys(), pd.Index), pd.Index)


def test_series_int_float() -> None:
# pyright infers mixtures of int and float in a list as list[int | float]
check(assert_type(pd.Series([1, 2, 3]), "pd.Series[int]"), pd.Series, np.integer)
Expand Down

0 comments on commit 4976e11

Please sign in to comment.