Skip to content

GH1255 Improve index typing for Series #1261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from collections.abc import (
Callable,
Hashable,
Iterator,
KeysView,
Mapping,
MutableSequence,
Sequence,
Expand Down Expand Up @@ -796,9 +797,6 @@ SliceType: TypeAlias = Hashable | None

num: TypeAlias = complex

# AxesData is used for data for Index
AxesData: TypeAlias = Axes | dict

DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
KeysArgType: TypeAlias = Any
ListLikeT = TypeVar("ListLikeT", bound=ListLike)
Expand Down Expand Up @@ -853,6 +851,9 @@ IndexingInt: TypeAlias = (
int | np.int_ | np.integer | np.unsignedinteger | np.signedinteger | np.int8
)

# AxesData is used for data for Index
AxesData: TypeAlias = Mapping[S3, Any] | Axes | KeysView

# Any plain Python or numpy function
Function: TypeAlias = np.ufunc | Callable[..., Any]
# Use a distinct HashableT in shared types to avoid conflicts with
Expand Down
33 changes: 17 additions & 16 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ from pandas._typing import (
AnyArrayLike,
ArrayLike,
Axes,
AxesData,
Axis,
AxisColumn,
AxisIndex,
Expand Down Expand Up @@ -252,7 +253,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__(
cls,
data: npt.NDArray[np.float64],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -261,7 +262,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
cls,
data: Sequence[Never],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -270,7 +271,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__(
cls,
data: Sequence[list[_str]],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -279,7 +280,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__(
cls,
data: Sequence[_str],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -295,7 +296,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
| datetime
| date
),
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: TimestampDtypeArg = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -304,7 +305,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__(
cls,
data: _ListLike,
index: Axes | None = ...,
index: AxesData | None = ...,
*,
dtype: TimestampDtypeArg,
name: Hashable = ...,
Expand All @@ -314,7 +315,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__(
cls,
data: PeriodIndex | Sequence[Period],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: PeriodDtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -329,7 +330,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
| np.timedelta64
| timedelta
),
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: TimedeltaDtypeArg = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -343,7 +344,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
| Sequence[Interval[_OrderableT]]
| dict[HashableT1, Interval[_OrderableT]]
),
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Literal["Interval"] = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -352,7 +353,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__( # type: ignore[overload-overlap]
cls,
data: Scalar | _ListLike | dict[HashableT1, Any] | None,
index: Axes | None = ...,
index: AxesData | None = ...,
*,
dtype: type[S1],
name: Hashable = ...,
Expand All @@ -362,7 +363,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
cls,
data: Sequence[bool],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -371,7 +372,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__( # type: ignore[overload-overlap]
cls,
data: Sequence[int],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -380,7 +381,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__(
cls,
data: Sequence[float],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -389,7 +390,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__( # type: ignore[overload-cannot-match] # pyright: ignore[reportOverlappingOverload]
cls,
data: Sequence[int | float],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -398,7 +399,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
def __new__(
cls,
data: S1 | _ListLike[S1] | dict[HashableT1, S1] | dict_keys[S1, Any],
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand All @@ -415,7 +416,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
| NAType
| None
) = ...,
index: Axes | None = ...,
index: AxesData | None = ...,
dtype: Dtype = ...,
name: Hashable = ...,
copy: bool = ...,
Expand Down
21 changes: 20 additions & 1 deletion tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ def test_types_scalar_arithmetic() -> None:


def test_types_complex_arithmetic() -> None:
# GH 103
"""Test adding complex number to pd.Series[float] GH 103."""
c = 1 + 1j
s = pd.Series([1.0, 2.0, 3.0])
x = s + c
Expand Down Expand Up @@ -3922,3 +3922,22 @@ def test_series_unstack() -> None:
),
pd.DataFrame,
)


def test_series_index_type() -> None:
index = {"a": 3, "c": 4}
lst = [1, 2]

check(
assert_type(pd.Series(lst, index=index), "pd.Series[int]"),
pd.Series,
np.integer,
)
check(
assert_type(pd.Series([1, 2], index=index.keys()), "pd.Series[int]"),
pd.Series,
np.integer,
)

if TYPE_CHECKING_INVALID_USAGE:
t = pd.Series([1, 2], index="ab") # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType]