From 5eee3259620bd1e2795d81bc54b640c1d23a40b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Diridollou?= Date: Tue, 24 Jun 2025 16:16:26 -0400 Subject: [PATCH 1/5] GH1255 Initial commit --- pandas-stubs/_typing.pyi | 7 ++++--- pandas-stubs/core/series.pyi | 33 +++++++++++++++++---------------- tests/test_series.py | 15 ++++++++++++++- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 5cef91fd..162ce3e5 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -14,6 +14,7 @@ from re import Pattern import sys from typing import ( Any, + KeysView, Literal, Protocol, SupportsIndex, @@ -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) @@ -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[SeriesDType, 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 diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 1982d54b..ffe9338b 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -111,6 +111,7 @@ from pandas._typing import ( AnyArrayLike, ArrayLike, Axes, + AxesData, Axis, AxisColumn, AxisIndex, @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -295,7 +296,7 @@ class Series(IndexOpsMixin[S1], NDFrame): | datetime | date ), - index: Axes | None = ..., + index: AxesData | None = ..., dtype: TimestampDtypeArg = ..., name: Hashable = ..., copy: bool = ..., @@ -304,7 +305,7 @@ class Series(IndexOpsMixin[S1], NDFrame): def __new__( cls, data: _ListLike, - index: Axes | None = ..., + index: AxesData | None = ..., *, dtype: TimestampDtypeArg, name: Hashable = ..., @@ -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 = ..., @@ -329,7 +330,7 @@ class Series(IndexOpsMixin[S1], NDFrame): | np.timedelta64 | timedelta ), - index: Axes | None = ..., + index: AxesData | None = ..., dtype: TimedeltaDtypeArg = ..., name: Hashable = ..., copy: bool = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -415,7 +416,7 @@ class Series(IndexOpsMixin[S1], NDFrame): | NAType | None ) = ..., - index: Axes | None = ..., + index: AxesData | None = ..., dtype: Dtype = ..., name: Hashable = ..., copy: bool = ..., diff --git a/tests/test_series.py b/tests/test_series.py index 3f25d981..7f1ae6a6 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -4,6 +4,7 @@ Hashable, Iterable, Iterator, + Mapping, Sequence, ) import datetime @@ -44,6 +45,7 @@ import xarray as xr from pandas._typing import ( + AxesData, DtypeObj, Scalar, ) @@ -867,7 +869,6 @@ def test_types_scalar_arithmetic() -> None: res_pow3: pd.Series = s.pow(0.5) -def test_types_complex_arithmetic() -> None: # GH 103 c = 1 + 1j s = pd.Series([1.0, 2.0, 3.0]) @@ -3922,3 +3923,15 @@ def test_series_unstack() -> None: ), pd.DataFrame, ) + + +def test_series_index_type() -> None: + index = {"a":3, "c": 4} + lst = [1, 2] + assert_type(index, Mapping[str, Any]) + + 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 # pyright: ignore[reportCallIssue, reportArgumentType] From fe0c6388b0a063f06e8cf59ca4e350f18141d4bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Diridollou?= Date: Tue, 24 Jun 2025 16:23:52 -0400 Subject: [PATCH 2/5] GH1255 Initial commit --- pandas-stubs/_typing.pyi | 5 ++++- tests/test_series.py | 3 --- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 162ce3e5..8e1d920b 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -1,4 +1,7 @@ -from builtins import type as type_t +from builtins import ( + str, + type as type_t, +) from collections.abc import ( Callable, Hashable, diff --git a/tests/test_series.py b/tests/test_series.py index 7f1ae6a6..f5bea74f 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -4,7 +4,6 @@ Hashable, Iterable, Iterator, - Mapping, Sequence, ) import datetime @@ -45,7 +44,6 @@ import xarray as xr from pandas._typing import ( - AxesData, DtypeObj, Scalar, ) @@ -3928,7 +3926,6 @@ def test_series_unstack() -> None: def test_series_index_type() -> None: index = {"a":3, "c": 4} lst = [1, 2] - assert_type(index, Mapping[str, Any]) 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) From a8209bd9c9a93c5b6b1d1483b86c73653d1904d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Diridollou?= Date: Tue, 24 Jun 2025 16:29:40 -0400 Subject: [PATCH 3/5] GH1255 Initial commit --- pandas-stubs/_typing.pyi | 9 +++------ tests/test_series.py | 23 +++++++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 8e1d920b..7b2b0081 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -1,11 +1,9 @@ -from builtins import ( - str, - type as type_t, -) +from builtins import type as type_t from collections.abc import ( Callable, Hashable, Iterator, + KeysView, Mapping, MutableSequence, Sequence, @@ -17,7 +15,6 @@ from re import Pattern import sys from typing import ( Any, - KeysView, Literal, Protocol, SupportsIndex, @@ -855,7 +852,7 @@ IndexingInt: TypeAlias = ( ) # AxesData is used for data for Index -AxesData: TypeAlias = Mapping[SeriesDType, Any] | Axes | KeysView +AxesData: TypeAlias = Mapping[S3, Any] | Axes | KeysView # Any plain Python or numpy function Function: TypeAlias = np.ufunc | Callable[..., Any] diff --git a/tests/test_series.py b/tests/test_series.py index f5bea74f..94a425a3 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -866,12 +866,11 @@ def test_types_scalar_arithmetic() -> None: res_pow2: pd.Series = s**0.213 res_pow3: pd.Series = s.pow(0.5) - # GH 103 c = 1 + 1j - s = pd.Series([1.0, 2.0, 3.0]) - x = s + c - y = s - c + s_flt = pd.Series([1.0, 2.0, 3.0]) + x = s_flt + c + y = s_flt - c def test_types_groupby() -> None: @@ -3924,11 +3923,19 @@ def test_series_unstack() -> None: def test_series_index_type() -> None: - index = {"a":3, "c": 4} + 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) + 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 # pyright: ignore[reportCallIssue, reportArgumentType] + t = pd.Series([1, 2], index="ab") # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType] From b4ed638aaca64be0f85811f511e53e2402268f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Diridollou?= Date: Tue, 24 Jun 2025 16:46:18 -0400 Subject: [PATCH 4/5] GH1255 Initial commit --- tests/test_series.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_series.py b/tests/test_series.py index 94a425a3..9d73069a 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -866,11 +866,12 @@ def test_types_scalar_arithmetic() -> None: res_pow2: pd.Series = s**0.213 res_pow3: pd.Series = s.pow(0.5) - # GH 103 +def test_types_complex_arithmetic() -> None: + """ Test adding complex number to pd.Series[float] GH 103.""" c = 1 + 1j - s_flt = pd.Series([1.0, 2.0, 3.0]) - x = s_flt + c - y = s_flt - c + s = pd.Series([1.0, 2.0, 3.0]) + x = s + c + y = s - c def test_types_groupby() -> None: From a3e490ccb39dcc880930bb142e53538d00d0eb47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Diridollou?= Date: Tue, 24 Jun 2025 16:54:51 -0400 Subject: [PATCH 5/5] GH1255 Initial commit --- tests/test_series.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_series.py b/tests/test_series.py index 9d73069a..b71f6498 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -866,8 +866,9 @@ def test_types_scalar_arithmetic() -> None: res_pow2: pd.Series = s**0.213 res_pow3: pd.Series = s.pow(0.5) + def test_types_complex_arithmetic() -> None: - """ Test adding complex number to pd.Series[float] 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