8000 Pull in PR #196 from MS project · pandas-dev/pandas-stubs@12250f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12250f0

Browse files
committed
Pull in PR #196 from MS project
1 parent 5a49c82 commit 12250f0

File tree

9 files changed

+58
-75
lines changed

9 files changed

+58
-75
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
This will be the home for the typing stubs supported by the pandas core team.
66

7-
The current version of these stubs was copied/forked from the project https://github.com/microsoft/python-type-stubs as of commit `3ea7eb74b1143fc19d3cde337a4ca04dfaa81f66`
7+
The current version of these stubs was copied/forked from the project https://github.com/microsoft/python-type-stubs as of commit `3ea7eb74b1143fc19d3cde337a4ca04dfaa81f66` plus https://github.com/microsoft/python-type-stubs/pull/196
88

99
As both projects move forward, this page will track what the differences are (if any).
1010

1111
## Thanks
1212

1313
We are indebted to Microsoft and that project for the initial set of public type stubs. We are also grateful for the original pandas-stubs project at https://github.com/VirtusLab/pandas-stubs that created the framework for testing the stubs.
1414

15-
Last update to README: 4/27/2022: 5:17 EDT
15+
Last update to README: 5/3/2022: 12:31 EDT

tests/pandas/test_series.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tempfile
44
from pathlib import Path
55
from typing import List, Union, TYPE_CHECKING
6+
from typing_extensions import assert_type
67

78
from pandas._typing import Scalar
89
from pandas.api.extensions import ExtensionArray
@@ -588,7 +589,14 @@ def test_series_min_max_sub_axis() -> None:
588589
df = pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [5, 4, 3, 2, 1]})
589590
s1 = df.min(axis=1)
590591
s2 = df.max(axis=1)
591-
s3 = s2 - s1
592+
sa = s1 + s2
593+
ss = s1 - s2
594+
sm = s1 * s2
595+
sd = s1 / s2
596+
assert_type(sa, "pd.Series")
597+
assert_type(ss, "pd.Series")
598+
assert_type(sm, "pd.Series")
599+
assert_type(sd, "pd.Series")
592600

593601

594602
def test_series_index_isin() -> None:
@@ -606,7 +614,7 @@ def test_series_index_isin() -> None:
606614
def test_series_invert() -> None:
607615
s1 = pd.Series([True, False, True])
608616
s2 = ~s1
609-
check_series_result(s2, bool)
617+
assert_type(s2, "pd.Series[bool]")
610618
s3 = pd.Series([1, 2, 3])
611619
check_series_result(s3[s2])
612620
check_series_result(s3.loc[s2])
@@ -650,4 +658,4 @@ def test_series_add_str() -> None:
650658
def test_series_dtype() -> None:
651659
s = pd.Series(["abc", "def"], dtype=str)
652660
check_series_result(s, object)
653-
661+

tests/pandas/test_timefuncs.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# flake8: noqa: F841
22

3+
from typing import TYPE_CHECKING
34
import pandas as pd
45
import datetime as dt
6+
from typing_extensions import assert_type
57

68
from pandas.testing import assert_series_equal
79

810
from . import check_datetimeindex_result, check_series_result
911

12+
if TYPE_CHECKING:
13+
from pandas.core.series import TimedeltaSeries, TimestampSeries
14+
1015

1116
def test_types_init() -> None:
1217
ts: pd.Timestamp = pd.Timestamp("2021-03-01T12")
@@ -79,6 +84,7 @@ def test_timestamp_timedelta_series_arithmetic() -> None:
7984
td1 = pd.to_timedelta([2, 3], "seconds")
8085
ts2 = pd.to_datetime(pd.Series(["2022-03-08", "2022-03-10"]))
8186
r1 = ts1 - ts2
87+
assert_type(r1, "TimedeltaSeries")
8288
check_series_result(r1, td1.dtype) # type: ignore
8389
r2 = r1 / td1
8490
check_series_result(r2, float)
@@ -123,14 +129,15 @@ def test_timestamp_plus_timedelta_series() -> None:
123129
ts = pd.Timestamp("2022-03-05")
124130
td = pd.to_timedelta(pd.Series([10, 20]), "minutes")
125131
r3 = td + ts
132+
assert_type(r3, "TimestampSeries")
126133
# ignore type on next, because `tscheck` has Unknown dtype
127134
check_series_result(r3, tscheck.dtype) # type: ignore
128135

129136

130137
def test_timedelta_series_mult() -> None:
131138
df = pd.DataFrame({"x": [1, 3, 5], "y": [2, 2, 6]})
132139
std = (df["x"] < df["y"]) * pd.Timedelta(10, "minutes")
133-
check_series_result(std, pd.to_timedelta(pd.Series([10]), "minutes").dtype)
140+
assert_type(std, "TimedeltaSeries")
134141

135142
def test_timedelta_series_sum() -> None:
136143
s = pd.Series(pd.to_datetime(["04/05/2022 11:00", "04/03/2022 10:00"])) - pd.Series(pd.to_datetime(["04/05/2022 08:00", "04/03/2022 09:00"]))
@@ -140,4 +147,4 @@ def test_timedelta_series_sum() -> None:
140147
sf = pd.Series([1.0, 2.2, 3.3])
141148
sfsum: float = sf.sum()
142149

143-
150+

tests/requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
pandas==1.4.1
1+
pandas==1.4.2
22
pyright
33
pytest
4-
mypy
4+
mypy==0.950
5+
typing_extensions==4.2.0

typings/pandas/_libs/tslibs/timestamps.pyi

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@ class Timestamp(datetime):
3434
value: int # np.int64
3535
def __new__(
3636
cls: type[_DatetimeT],
37-
ts_input: int
38-
| np.integer
39-
| float
40-
| str
41-
| _date
42-
| datetime
43-
| np.datetime64 = ...,
37+
ts_input: int | np.integer | float | str | _date | datetime | np.datetime64 = ...,
4438
freq: int | None | str | BaseOffset = ...,
4539
tz: str | _tzinfo | None | int = ...,
4640
unit: str | int | None = ...,
@@ -81,9 +75,7 @@ class Timestamp(datetime):
8175
@property
8276
def fold(self) -> int: ...
8377
@classmethod
84-
def fromtimestamp(
85-
cls: type[_DatetimeT], t: float, tz: _tzinfo | None = ...
86-
) -> _DatetimeT: ...
78+
def fromtimestamp(cls: type[_DatetimeT], t: float, tz: _tzinfo | None = ...) -> _DatetimeT: ...
8779
@classmethod
8880
def utcfromtimestamp(cls: type[_DatetimeT], t: float) -> _DatetimeT: ...
8981
@classmethod
@@ -124,7 +116,7 @@ class Timestamp(datetime):
124116
microsecond: int = ...,
125117
tzinfo: _tzinfo | None = ...,
126118
fold: int = ...,
127-
) -> datetime: ...
119+
) -> Timestamp: ...
128120
def astimezone(self: _DatetimeT, tz: _tzinfo | None = ...) -> _DatetimeT: ...
129121
def ctime(self) -> str: ...
130122
def isoformat(self, sep: str = ..., timespec: str = ...) -> str: ...
@@ -141,16 +133,12 @@ class Timestamp(datetime):
141133
@overload # type: ignore[override]
142134
def __add__(self, other: np.ndarray) -> np.ndarray: ...
143135
@overload
144-
def __add__(
145-
self: _DatetimeT, other: timedelta | np.timedelta64 | Tick
146-
) -> _DatetimeT: ...
136+
def __add__(self: _DatetimeT, other: timedelta | np.timedelta64 | Tick) -> _DatetimeT: ...
147137
def __radd__(self: _DatetimeT, other: timedelta) -> _DatetimeT: ...
148138
@overload # type: ignore[override]
149139
def __sub__(self, other: datetime) -> Timedelta: ...
150140
@overload
151-
def __sub__(
152-
self: _DatetimeT, other: timedelta | np.timedelta64 | Tick
153-
) -> _DatetimeT: ...
141+
def __sub__(self: _DatetimeT, other: timedelta | np.timedelta64 | Tick) -> _DatetimeT: ...
154142
def __hash__(self) -> int: ...
155143
def weekday(self) -> int: ...
156144
def isoweekday(self) -> int: ...
@@ -185,15 +173,9 @@ class Timestamp(datetime):
185173
) -> _DatetimeT: ...
186174
def normalize(self: _DatetimeT) -> _DatetimeT: ...
187175
# TODO: round/floor/ceil could return NaT?
188-
def round(
189-
self: _DatetimeT, freq: str, ambiguous: bool | str = ..., nonexistent: str = ...
190-
) -> _DatetimeT: ...
191-
def floor(
192-
self: _DatetimeT, freq: str, ambiguous: bool | str = ..., nonexistent: str = ...
193-
) -> _DatetimeT: ...
194-
def ceil(
195-
self: _DatetimeT, freq: str, ambiguous: bool | str = ..., nonexistent: str = ...
196-
) -> _DatetimeT: ...
176+
def round(self: _DatetimeT, freq: str, ambiguous: bool | str = ..., nonexistent: str = ...) -> _DatetimeT: ...
177+
def floor(self: _DatetimeT, freq: str, ambiguous: bool | str = ..., nonexistent: str = ...) -> _DatetimeT: ...
178+
def ceil(self: _DatetimeT, freq: str, ambiguous: bool | str = ..., nonexistent: str = ...) -> _DatetimeT: ...
197179
def day_name(self, locale: str | None = ...) -> str: ...
198180
def month_name(self, locale: str | None = ...) -> str: ...
199181
@property
@@ -210,9 +192,7 @@ class Timestamp(datetime):
210192
def quarter(self) -> int: ...
211193
@property
212194
def week(self) -> int: ...
213-
def to_numpy(
214-
self, dtype: np.dtype | None = ..., copy: bool = ...
215-
) -> np.datetime64: ...
195+
def to_numpy(self, dtype: np.dtype | None = ..., copy: bool = ...) -> np.datetime64: ...
216196
@property
217197
def _date_repr(self) -> str: ...
218198
@property

typings/pandas/core/indexes/datetimes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from pandas.core.indexes.datetimelike import (
55
DatetimelikeDelegateMixin as DatetimelikeDelegateMixin,
66
)
77
from pandas.core.indexes.timedeltas import TimedeltaIndex as TimedeltaIndex
8-
from pandas.core.series import Series as Series
8+
from pandas.core.series import Series as Series, TimedeltaSeries, TimestampSeries
99
from pandas._typing import Timestamp as Timestamp, Timedelta as Timedelta
1010
from typing import Optional, Union, overload
1111

@@ -30,7 +30,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeDelegateMixin):
3030
def __array__(self, dtype=...) -> np.ndarray: ...
3131
def __reduce__(self): ...
3232
@overload
33-
def __add__(self, other: Series[Timedelta]) -> Series[Timestamp]: ...
33+
def __add__(self, other: TimedeltaSeries) -> TimestampSeries: ...
3434
@overload
3535
def __add__(self, other: Union[Timedelta, TimedeltaIndex]) -> DatetimeIndex: ...
3636
def union_many(self, others): ...

typings/pandas/core/series.pyi

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ from typing import (
4444
Hashable,
4545
Iterable,
4646
List,
47+
Literal,
4748
Mapping,
4849
Optional,
4950
Sequence,
@@ -53,11 +54,6 @@ from typing import (
5354
overload,
5455
)
5556

56-
if sys.version_info >= (3, 8):
57-
from typing import Literal
58-
else:
59-
from typing_extensions import Literal
60-
6157
_bool = bool
6258
_str = str
6359

@@ -122,7 +118,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
122118
name: Optional[Hashable] = ...,
123119
copy: bool = ...,
124120
fastpath: bool = ...,
125-
) -> Series[Timestamp]: ...
121+
) -> TimestampSeries: ...
126122
@overload
127123
def __new__(
128124
cls,
@@ -991,11 +987,11 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
991987
# just failed to generate these so I couldn't match
992988
# them up.
993989
@overload
994-
def __add__(self: Series[Timedelta], other: Series[Timestamp]) -> Series[Timestamp]: ...
990+
def __add__(self, other: TimestampSeries) -> TimestampSeries: ...
995991
@overload
996-
def __add__(self: Series[Timedelta], other: DatetimeIndex) -> Series[Timestamp]: ...
992+
def __add__(self, other: DatetimeIndex) -> TimestampSeries: ...
997993
@overload
998-
def __add__(self: Series[Timedelta], other: Timestamp) -> Series[Timestamp]: ...
994+
def __add__(self, other: Timestamp) -> TimestampSeries: ...
999995
@overload
1000996
def __add__(self, other: Union[num, _str, Timedelta, _ListLike, Series[S1]]) -> Series: ...
1001997
def __and__(self, other: Union[_ListLike, Series[S1]]) -> Series[_bool]: ...
@@ -1020,11 +1016,9 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
10201016
def __le__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[_bool]: ...
10211017
def __lt__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[_bool]: ...
10221018
@overload
1023-
def __mul__(self, other: Union[num, _ListLike, Series[S1]]) -> Series: ...
1024-
@overload
1025-
def __mul__(self, other: Union[Timedelta, Series[Timedelta]]) -> Series[Timedelta]: ...
1019+
def __mul__(self, other: Union[Timedelta, TimedeltaSeries]) -> TimedeltaSeries: ...
10261020
@overload
1027-
def __mul__(self: Series[Timedelta], other: Union[num, Series[bool], Series[int], Series[float]]) -> Series[Timedelta]: ...
1021+
def __mul__(self, other: Union[num, _ListLike, Series]) -> Series: ...
10281022
def __mod__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[S1]: ...
10291023
def __ne__(self, other: object) -> Series[_bool]: ... # type: ignore
10301024
def __pow__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[S1]: ...
@@ -1035,36 +1029,26 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
10351029
def __rdivmod__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[S1]: ...
10361030
def __rfloordiv__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[S1]: ...
10371031
def __rmod__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[S1]: ...
1038-
@overload
1039-
def __rmul__(self, other: Union[num, _ListLike, Series[S1]]) -> Series: ...
1040-
@overload
1041-
def __rmul__(self, other: Union[Timedelta, Series[Timedelta]]) -> Series[Timedelta]: ...
1042-
@overload
1043-
def __rmul__(self: Series[Timedelta], other: Union[num, Series[bool], Series[int], Series[float]]) -> Series[Timedelta]: ...
1032+
def __rmul__(self, other: Union[num, _ListLike, Series]) -> Series: ...
10441033
def __rnatmul__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[S1]: ...
10451034
def __rpow__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[S1]: ...
10461035
def __ror__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[_bool]: ...
1036+
def __rsub__(self, other: Union[num, _ListLike, Series[S1]]) -> Series: ...
10471037
@overload
1048-
def __rsub__(self: Series[Timestamp], other: Union[Timestamp, Series[Timestamp]]) -> Series[Timedelta]: ...
1049-
@overload
1050-
def __rsub__(self: Series[Timestamp], other: Union[Timedelta, Series[Timedelta]]) -> Series[Timestamp]: ...
1051-
@overload
1052-
def __rsub__(self, other: Union[num, Timedelta, _ListLike, Series[S1]]) -> Series: ...
1053-
@overload
1054-
def __rtruediv__(self: Series[Timedelta], other: Union[Timedelta, Series[Timedelta]]) -> Series[float]: ...
1038+
def __rtruediv__(self, other: Union[Timedelta, TimedeltaSeries]) -> Series[float]: ...
10551039
@overload
10561040
def __rtruediv__(self, other: Union[num, _ListLike, Series[S1]]) -> Series: ...
10571041
def __rxor__(self, other: Union[num, _ListLike, Series[S1]]) -> Series[_bool]: ...
10581042
@overload
1059-
def __sub__(self: Series[Timestamp], other: Union[Timestamp, Series[Timestamp]]) -> Series[Timedelta]: ...
1060-
@overload
1061-
def __sub__(self: Series[Timestamp], other: Union[Timedelta, Series[Timedelta]]) -> Series[Timestamp]: ...
1043+
def __sub__(self, other: Union[Timestamp, TimestampSeries]) -> TimedeltaSeries: ...
10621044
@overload
1063-
def __sub__(self, other: Union[num, Timedelta, _ListLike, Series[S1]]) -> Series: ...
1045+
def __sub__(self, other: Union[Timedelta, TimedeltaSeries]) -> TimestampSeries: ...
10641046
@overload
1065-
def __truediv__(self: Series[Timedelta], other: Union[Timedelta, Series[Timedelta]]) -> Series[float]: ...
1047+
def __sub__(self, other: Union[num, _ListLike, Series]) -> Series: ...
10661048
@overload
10671049
def __truediv__(self, other: Union[num, _ListLike, Series[S1]]) -> Series: ...
1050+
@overload
1051+
def __truediv__(self, other: Union[Timedelta, TimedeltaSeries]) -> Series[float]: ...
10681052
def __xor__(self, other: Union[_ListLike, Series[S1]]) -> Series: ...
10691053
def __invert__(self) -> Series[bool]: ...
10701054
# properties
@@ -1603,3 +1587,6 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
16031587
def set_axis(self, labels, *, inplace: Literal[True]) -> None: ...
16041588
@overload
16051589
def set_axis(self, labels, axis: Axis = ..., inplace: bool = ...) -> Optional[Series[S1]]: ...
1590+
1591+
class TimestampSeries(Series): ...
1592+
class TimedeltaSeries(Series): ...

typings/pandas/core/tools/datetimes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ from pandas.core.dtypes.generic import ABCSeries as ABCSeries
1414
from pandas.core.generic import NDFrame as NDFrame
1515
from pandas.core.indexes.datetimes import DatetimeIndex as DatetimeIndex
1616
from pandas.core.frame import DataFrame as DataFrame
17-
from pandas.core.series import Series as Series
17+
from pandas.core.series import Series as Series, TimestampSeries
1818
from typing import List, Optional, Tuple, TypedDict, Union, overload
1919

2020
ArrayConvertible = Union[List, Tuple, AnyArrayLike]
@@ -71,7 +71,7 @@ def to_datetime(
7171
infer_datetime_format: bool = ...,
7272
origin=...,
7373
cache: bool = ...,
74-
) -> Series[Timestamp]: ...
74+
) -> TimestampSeries: ...
7575
@overload
7676
def to_datetime(
7777
arg: list | tuple | np.ndarray | Index | ExtensionArray,

typings/pandas/core/tools/timedeltas.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from typing import Literal, Optional, Union, overload
44
from pandas._libs.tslibs import Timedelta
55
from pandas._libs.tslibs.timedeltas import UnitChoices
66
from pandas._typing import DateTimeErrorChoices, ArrayLike, Index as Index
7-
from pandas.core.frame import Series as Series
7+
from pandas.core.series import Series as Series, TimedeltaSeries
88
from pandas.core.indexes.timedeltas import TimedeltaIndex
99

1010
# Copied from pandas/_libs/tslibs/timedeltas.pyx
@@ -20,7 +20,7 @@ def to_timedelta(
2020
arg: Series,
2121
unit: Optional[UnitChoices] = ...,
2222
errors: DateTimeErrorChoices = ...,
23-
) -> Series[Timedelta]: ...
23+
) -> TimedeltaSeries: ...
2424
@overload
2525
def to_timedelta(
2626
arg: Union[list, tuple, range, ArrayLike, Index],

0 commit comments

Comments
 (0)
0