Skip to content

Commit 5eee325

Browse files
GH1255 Initial commit
1 parent 494b3b0 commit 5eee325

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

pandas-stubs/_typing.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ from re import Pattern
1414
import sys
1515
from typing import (
1616
Any,
17+
KeysView,
1718
Literal,
1819
Protocol,
1920
SupportsIndex,
@@ -796,9 +797,6 @@ SliceType: TypeAlias = Hashable | None
796797

797798
num: TypeAlias = complex
798799

799-
# AxesData is used for data for Index
800-
AxesData: TypeAlias = Axes | dict
801-
802800
DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
803801
KeysArgType: TypeAlias = Any
804802
ListLikeT = TypeVar("ListLikeT", bound=ListLike)
@@ -853,6 +851,9 @@ IndexingInt: TypeAlias = (
853851
int | np.int_ | np.integer | np.unsignedinteger | np.signedinteger | np.int8
854852
)
855853

854+
# AxesData is used for data for Index
855+
AxesData: TypeAlias = Mapping[SeriesDType, Any] | Axes | KeysView
856+
856857
# Any plain Python or numpy function
857858
Function: TypeAlias = np.ufunc | Callable[..., Any]
858859
# Use a distinct HashableT in shared types to avoid conflicts with

pandas-stubs/core/series.pyi

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ from pandas._typing import (
111111
AnyArrayLike,
112112
ArrayLike,
113113
Axes,
114+
AxesData,
114115
Axis,
115116
AxisColumn,
116117
AxisIndex,
@@ -252,7 +253,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
252253
def __new__(
253254
cls,
254255
data: npt.NDArray[np.float64],
255-
index: Axes | None = ...,
256+
index: AxesData | None = ...,
256257
dtype: Dtype = ...,
257258
name: Hashable = ...,
258259
copy: bool = ...,
@@ -261,7 +262,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
261262
def __new__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
262263
cls,
263264
data: Sequence[Never],
264-
index: Axes | None = ...,
265+
index: AxesData | None = ...,
265266
dtype: Dtype = ...,
266267
name: Hashable = ...,
267268
copy: bool = ...,
@@ -270,7 +271,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
270271
def __new__(
271272
cls,
272273
data: Sequence[list[_str]],
273-
index: Axes | None = ...,
274+
index: AxesData | None = ...,
274275
dtype: Dtype = ...,
275276
name: Hashable = ...,
276277
copy: bool = ...,
@@ -279,7 +280,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
279280
def __new__(
280281
cls,
281282
data: Sequence[_str],
282-
index: Axes | None = ...,
283+
index: AxesData | None = ...,
283284
dtype: Dtype = ...,
284285
name: Hashable = ...,
285286
copy: bool = ...,
@@ -295,7 +296,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
295296
| datetime
296297
| date
297298
),
298-
index: Axes | None = ...,
299+
index: AxesData | None = ...,
299300
dtype: TimestampDtypeArg = ...,
300301
name: Hashable = ...,
301302
copy: bool = ...,
@@ -304,7 +305,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
304305
def __new__(
305306
cls,
306307
data: _ListLike,
307-
index: Axes | None = ...,
308+
index: AxesData | None = ...,
308309
*,
309310
dtype: TimestampDtypeArg,
310311
name: Hashable = ...,
@@ -314,7 +315,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
314315
def __new__(
315316
cls,
316317
data: PeriodIndex | Sequence[Period],
317-
index: Axes | None = ...,
318+
index: AxesData | None = ...,
318319
dtype: PeriodDtype = ...,
319320
name: Hashable = ...,
320321
copy: bool = ...,
@@ -329,7 +330,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
329330
| np.timedelta64
330331
| timedelta
331332
),
332-
index: Axes | None = ...,
333+
index: AxesData | None = ...,
333334
dtype: TimedeltaDtypeArg = ...,
334335
name: Hashable = ...,
335336
copy: bool = ...,
@@ -343,7 +344,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
343344
| Sequence[Interval[_OrderableT]]
344345
| dict[HashableT1, Interval[_OrderableT]]
345346
),
346-
index: Axes | None = ...,
347+
index: AxesData | None = ...,
347348
dtype: Literal["Interval"] = ...,
348349
name: Hashable = ...,
349350
copy: bool = ...,
@@ -352,7 +353,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
352353
def __new__( # type: ignore[overload-overlap]
353354
cls,
354355
data: Scalar | _ListLike | dict[HashableT1, Any] | None,
355-
index: Axes | None = ...,
356+
index: AxesData | None = ...,
356357
*,
357358
dtype: type[S1],
358359
name: Hashable = ...,
@@ -362,7 +363,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
362363
def __new__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
363364
cls,
364365
data: Sequence[bool],
365-
index: Axes | None = ...,
366+
index: AxesData | None = ...,
366367
dtype: Dtype = ...,
367368
name: Hashable = ...,
368369
copy: bool = ...,
@@ -371,7 +372,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
371372
def __new__( # type: ignore[overload-overlap]
372373
cls,
373374
data: Sequence[int],
374-
index: Axes | None = ...,
375+
index: AxesData | None = ...,
375376
dtype: Dtype = ...,
376377
name: Hashable = ...,
377378
copy: bool = ...,
@@ -380,7 +381,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
380381
def __new__(
381382
cls,
382383
data: Sequence[float],
383-
index: Axes | None = ...,
384+
index: AxesData | None = ...,
384385
dtype: Dtype = ...,
385386
name: Hashable = ...,
386387
copy: bool = ...,
@@ -389,7 +390,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
389390
def __new__( # type: ignore[overload-cannot-match] # pyright: ignore[reportOverlappingOverload]
390391
cls,
391392
data: Sequence[int | float],
392-
index: Axes | None = ...,
393+
index: AxesData | None = ...,
393394
dtype: Dtype = ...,
394395
name: Hashable = ...,
395396
copy: bool = ...,
@@ -398,7 +399,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
398399
def __new__(
399400
cls,
400401
data: S1 | _ListLike[S1] | dict[HashableT1, S1] | dict_keys[S1, Any],
401-
index: Axes | None = ...,
402+
index: AxesData | None = ...,
402403
dtype: Dtype = ...,
403404
name: Hashable = ...,
404405
copy: bool = ...,
@@ -415,7 +416,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
415416
| NAType
416417
| None
417418
) = ...,
418-
index: Axes | None = ...,
419+
index: AxesData | None = ...,
419420
dtype: Dtype = ...,
420421
name: Hashable = ...,
421422
copy: bool = ...,

tests/test_series.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Hashable,
55
Iterable,
66
Iterator,
7+
Mapping,
78
Sequence,
89
)
910
import datetime
@@ -44,6 +45,7 @@
4445
import xarray as xr
4546

4647
from pandas._typing import (
48+
AxesData,
4749
DtypeObj,
4850
Scalar,
4951
)
@@ -867,7 +869,6 @@ def test_types_scalar_arithmetic() -> None:
867869
res_pow3: pd.Series = s.pow(0.5)
868870

869871

870-
def test_types_complex_arithmetic() -> None:
871872
# GH 103
872873
c = 1 + 1j
873874
s = pd.Series([1.0, 2.0, 3.0])
@@ -3922,3 +3923,15 @@ def test_series_unstack() -> None:
39223923
),
39233924
pd.DataFrame,
39243925
)
3926+
3927+
3928+
def test_series_index_type() -> None:
3929+
index = {"a":3, "c": 4}
3930+
lst = [1, 2]
3931+
assert_type(index, Mapping[str, Any])
3932+
3933+
check(assert_type(pd.Series(lst, index=index), "pd.Series[int]"), pd.Series, np.integer)
3934+
check(assert_type(pd.Series([1, 2], index=index.keys()), "pd.Series[int]"), pd.Series, np.integer)
3935+
3936+
if TYPE_CHECKING_INVALID_USAGE:
3937+
t = pd.Series([1,2], index="ab") # type: ignore # pyright: ignore[reportCallIssue, reportArgumentType]

0 commit comments

Comments
 (0)