@@ -8,38 +8,53 @@ from datetime import (
8
8
from time import struct_time
9
9
from typing import (
10
10
ClassVar ,
11
+ Literal ,
11
12
TypeVar ,
13
+ Union ,
12
14
overload ,
13
15
)
14
16
15
17
import numpy as np
16
- from pandas import Index
18
+ from pandas import (
19
+ DatetimeIndex ,
20
+ Index ,
21
+ TimedeltaIndex ,
22
+ )
17
23
from pandas .core .series import (
18
24
Series ,
25
+ TimedeltaSeries ,
19
26
TimestampSeries ,
20
27
)
28
+ from typing_extensions import TypeAlias
21
29
22
30
from pandas ._libs .tslibs import (
23
31
BaseOffset ,
24
32
Period ,
25
33
Tick ,
26
34
Timedelta ,
27
35
)
28
- from pandas ._typing import np_ndarray_bool
36
+ from pandas ._typing import (
37
+ np_ndarray_bool ,
38
+ npt ,
39
+ )
29
40
30
41
_DatetimeT = TypeVar ("_DatetimeT" , bound = datetime )
31
-
32
- def integer_op_not_supported (obj : object ) -> TypeError : ...
42
+ _Ambiguous : TypeAlias = Union [bool , Literal ["raise" , "NaT" ]]
43
+ _Nonexistent : TypeAlias = Union [
44
+ Literal ["raise" , "NaT" , "shift_backward" , "shift_forward" ], Timedelta , timedelta
45
+ ]
33
46
34
47
class Timestamp (datetime ):
35
48
min : ClassVar [Timestamp ]
36
49
max : ClassVar [Timestamp ]
37
50
38
51
resolution : ClassVar [Timedelta ]
39
- value : int # np.int64
52
+ value : int
40
53
def __new__ (
41
54
cls : type [_DatetimeT ],
42
55
ts_input : np .integer | float | str | _date | datetime | np .datetime64 = ...,
56
+ # Freq is deprecated but is left in to allow code like Timestamp(2000,1,1)
57
+ # Removing it would make the other arguments position only
43
58
freq : int | str | BaseOffset | None = ...,
44
59
tz : str | _tzinfo | int | None = ...,
45
60
unit : str | int | None = ...,
@@ -53,7 +68,7 @@ class Timestamp(datetime):
53
68
nanosecond : int | None = ...,
54
69
tzinfo : _tzinfo | None = ...,
55
70
* ,
56
- fold : int | None = ...,
71
+ fold : Literal [ 0 , 1 ] | None = ...,
57
72
) -> _DatetimeT : ...
58
73
# GH 46171
59
74
# While Timestamp can return pd.NaT, having the constructor return
@@ -73,14 +88,16 @@ class Timestamp(datetime):
73
88
@property
74
89
def microsecond (self ) -> int : ...
75
90
@property
91
+ def nanosecond (self ) -> int : ...
92
+ @property
76
93
def tzinfo (self ) -> _tzinfo | None : ...
77
94
@property
78
95
def tz (self ) -> _tzinfo | None : ...
79
96
@property
80
97
def fold (self ) -> int : ...
81
98
@classmethod
82
99
def fromtimestamp (
83
- cls : type [_DatetimeT ], t : float , tz : _tzinfo | None = ...
100
+ cls : type [_DatetimeT ], t : float , tz : _tzinfo | str | None = ...
84
101
) -> _DatetimeT : ...
85
102
@classmethod
86
103
def utcfromtimestamp (cls : type [_DatetimeT ], ts : float ) -> _DatetimeT : ...
@@ -90,7 +107,8 @@ class Timestamp(datetime):
90
107
def fromordinal (
91
108
cls : type [_DatetimeT ],
92
109
ordinal : int ,
93
- freq : str | BaseOffset | None = ...,
110
+ # freq produces a FutureWarning about being deprecated in a future version
111
+ freq : None = ...,
94
112
tz : _tzinfo | str | None = ...,
95
113
) -> _DatetimeT : ...
96
114
@classmethod
@@ -111,17 +129,21 @@ class Timestamp(datetime):
111
129
def date (self ) -> _date : ...
112
130
def time (self ) -> _time : ...
113
131
def timetz (self ) -> _time : ...
114
- def replace (
132
+ # Override since fold is more precise than datetime.replace(fold:int)
133
+ # Here it is restricted to be 0 or 1 using a Literal
134
+ # Violation of Liskov substitution principle
135
+ def replace ( # type:ignore[override]
115
136
self ,
116
- year : int = ...,
117
- month : int = ...,
118
- day : int = ...,
119
- hour : int = ...,
120
- minute : int = ...,
121
- second : int = ...,
122
- microsecond : int = ...,
137
+ year : int | None = ...,
138
+ month : int | None = ...,
139
+ day : int | None = ...,
140
+ hour : int | None = ...,
141
+ minute : int | None = ...,
142
+ second : int | None = ...,
143
+ microsecond : int | None = ...,
123
144
tzinfo : _tzinfo | None = ...,
124
- fold : int = ...,
145
+ * ,
146
+ fold : Literal [0 , 1 ] | None = ...,
125
147
) -> Timestamp : ...
126
148
def astimezone (self : _DatetimeT , tz : _tzinfo | None = ...) -> _DatetimeT : ...
127
149
def ctime (self ) -> str : ...
@@ -131,45 +153,86 @@ class Timestamp(datetime):
131
153
def utcoffset (self ) -> timedelta | None : ...
132
154
def tzname (self ) -> str | None : ...
133
155
def dst (self ) -> timedelta | None : ...
156
+ # Mypy complains Forward operator "<inequality op>" is not callable, so ignore misc
157
+ # for le, lt ge and gt
134
158
@overload # type: ignore[override]
135
- def __le__ (self , other : datetime ) -> bool : ...
159
+ def __le__ (self , other : Timestamp | datetime | np . datetime64 ) -> bool : ... # type: ignore[misc]
136
160
@overload
137
- def __le__ (self , other : Index ) -> np_ndarray_bool : ...
161
+ def __le__ (self , other : Index | npt . NDArray [ np . datetime64 ] ) -> np_ndarray_bool : ...
138
162
@overload
139
- def __le__ (self , other : TimestampSeries ) -> Series [bool ]: ...
163
+ def __le__ (self , other : TimestampSeries | Series [ Timestamp ] ) -> Series [bool ]: ...
140
164
@overload # type: ignore[override]
141
- def __lt__ (self , other : datetime ) -> bool : ...
165
+ def __lt__ (self , other : Timestamp | datetime | np . datetime64 ) -> bool : ... # type: ignore[misc]
142
166
@overload
143
- def __lt__ (self , other : Index ) -> np_ndarray_bool : ...
167
+ def __lt__ (self , other : Index | npt . NDArray [ np . datetime64 ] ) -> np_ndarray_bool : ...
144
168
@overload
145
- def __lt__ (self , other : TimestampSeries ) -> Series [bool ]: ...
169
+ def __lt__ (self , other : TimestampSeries | Series [ Timestamp ] ) -> Series [bool ]: ...
146
170
@overload # type: ignore[override]
147
- def __ge__ (self , other : datetime ) -> bool : ...
171
+ def __ge__ (self , other : Timestamp | datetime | np . datetime64 ) -> bool : ... # type: ignore[misc]
148
172
@overload
149
- def __ge__ (self , other : Index ) -> np_ndarray_bool : ...
173
+ def __ge__ (self , other : Index | npt . NDArray [ np . datetime64 ] ) -> np_ndarray_bool : ...
150
174
@overload
151
- def __ge__ (self , other : TimestampSeries ) -> Series [bool ]: ...
175
+ def __ge__ (self , other : TimestampSeries | Series [ Timestamp ] ) -> Series [bool ]: ...
152
176
@overload # type: ignore[override]
153
- def __gt__ (self , other : datetime ) -> bool : ...
177
+ def __gt__ (self , other : Timestamp | datetime | np . datetime64 ) -> bool : ... # type: ignore[misc]
154
178
@overload
155
- def __gt__ (self , other : Index ) -> np_ndarray_bool : ...
179
+ def __gt__ (self , other : Index | npt . NDArray [ np . datetime64 ] ) -> np_ndarray_bool : ...
156
180
@overload
157
- def __gt__ (self , other : TimestampSeries ) -> Series [bool ]: ...
181
+ def __gt__ (self , other : TimestampSeries | Series [ Timestamp ] ) -> Series [bool ]: ...
158
182
# error: Signature of "__add__" incompatible with supertype "date"/"datetime"
159
183
@overload # type: ignore[override]
160
- def __add__ (self , other : np .ndarray ) -> np .ndarray : ...
184
+ def __add__ (
185
+ self , other : npt .NDArray [np .timedelta64 ]
186
+ ) -> npt .NDArray [np .datetime64 ]: ...
161
187
@overload
162
188
def __add__ (
163
189
self : _DatetimeT , other : timedelta | np .timedelta64 | Tick
164
190
) -> _DatetimeT : ...
191
+ @overload
192
+ def __add__ (
193
+ self , other : TimedeltaSeries | Series [Timedelta ]
194
+ ) -> TimestampSeries : ...
195
+ @overload
196
+ def __add__ (self , other : TimedeltaIndex ) -> DatetimeIndex : ...
197
+ @overload
165
198
def __radd__ (self : _DatetimeT , other : timedelta ) -> _DatetimeT : ...
199
+ @overload
200
+ def __radd__ (self , other : TimedeltaIndex ) -> DatetimeIndex : ...
201
+ @overload
202
+ def __radd__ (
203
+ self , other : npt .NDArray [np .timedelta64 ]
204
+ ) -> npt .NDArray [np .datetime64 ]: ...
205
+ # TODO: test dt64
166
206
@overload # type: ignore[override]
167
- def __sub__ (self , other : datetime ) -> Timedelta : ...
207
+ def __sub__ (self , other : Timestamp | datetime | np . datetime64 ) -> Timedelta : ...
168
208
@overload
169
209
def __sub__ (
170
210
self : _DatetimeT , other : timedelta | np .timedelta64 | Tick
171
211
) -> _DatetimeT : ...
172
- def __hash__ (self ) -> int : ...
212
+ @overload
213
+ def __sub__ (self , other : TimedeltaIndex ) -> DatetimeIndex : ...
214
+ @overload
215
+ def __sub__ (self , other : TimedeltaSeries ) -> TimestampSeries : ...
216
+ @overload
217
+ def __sub__ (
218
+ self , other : npt .NDArray [np .timedelta64 ]
219
+ ) -> npt .NDArray [np .datetime64 ]: ...
220
+ @overload
221
+ def __eq__ (self , other : Timestamp | datetime | np .datetime64 ) -> bool : ... # type: ignore[misc]
222
+ @overload
223
+ def __eq__ (self , other : TimestampSeries | Series [Timestamp ]) -> Series [bool ]: ... # type: ignore[misc]
224
+ @overload
225
+ def __eq__ (self , other : npt .NDArray [np .datetime64 ] | Index ) -> np_ndarray_bool : ... # type: ignore[misc]
226
+ @overload
227
+ def __eq__ (self , other : object ) -> Literal [False ]: ...
228
+ @overload
229
+ def __ne__ (self , other : Timestamp | datetime | np .datetime64 ) -> bool : ... # type: ignore[misc]
230
+ @overload
231
+ def __ne__ (self , other : TimestampSeries | Series [Timestamp ]) -> Series [bool ]: ... # type: ignore[misc]
232
+ @overload
233
+ def __ne__ (self , other : npt .NDArray [np .datetime64 ] | Index ) -> np_ndarray_bool : ... # type: ignore[misc]
234
+ @overload
235
+ def __ne__ (self , other : object ) -> Literal [True ]: ...
173
236
def weekday (self ) -> int : ...
174
237
def isoweekday (self ) -> int : ...
175
238
def isocalendar (self ) -> tuple [int , int , int ]: ...
@@ -198,19 +261,28 @@ class Timestamp(datetime):
198
261
def tz_localize (
199
262
self : _DatetimeT ,
200
263
tz : _tzinfo | str | None ,
201
- ambiguous : str = ...,
202
- nonexistent : str = ...,
264
+ ambiguous : _Ambiguous = ...,
265
+ nonexistent : _Nonexistent = ...,
203
266
) -> _DatetimeT : ...
204
267
def normalize (self : _DatetimeT ) -> _DatetimeT : ...
205
268
# TODO: round/floor/ceil could return NaT?
206
269
def round (
207
- self : _DatetimeT , freq : str , ambiguous : bool | str = ..., nonexistent : str = ...
270
+ self : _DatetimeT ,
271
+ freq : str ,
272
+ ambiguous : _Ambiguous = ...,
273
+ nonexistent : _Nonexistent = ...,
208
274
) -> _DatetimeT : ...
209
275
def floor (
210
- self : _DatetimeT , freq : str , ambiguous : bool | str = ..., nonexistent : str = ...
276
+ self : _DatetimeT ,
277
+ freq : str ,
278
+ ambiguous : _Ambiguous = ...,
279
+ nonexistent : _Nonexistent = ...,
211
280
) -> _DatetimeT : ...
212
281
def ceil (
213
- self : _DatetimeT , freq : str , ambiguous : bool | str = ..., nonexistent : str = ...
282
+ self : _DatetimeT ,
283
+ freq : str ,
284
+ ambiguous : _Ambiguous = ...,
285
+ nonexistent : _Nonexistent = ...,
214
286
) -> _DatetimeT : ...
215
287
def day_name (self , locale : str | None = ...) -> str : ...
216
288
def month_name (self , locale : str | None = ...) -> str : ...
@@ -223,12 +295,12 @@ class Timestamp(datetime):
223
295
@property
224
296
def dayofyear (self ) -> int : ...
225
297
@property
298
+ def weekofyear (self ) -> int : ...
299
+ @property
226
300
def quarter (self ) -> int : ...
227
301
@property
228
302
def week (self ) -> int : ...
229
- def to_numpy (
230
- self , dtype : np .dtype | None = ..., copy : bool = ...
231
- ) -> np .datetime64 : ...
303
+ def to_numpy (self ) -> np .datetime64 : ...
232
304
@property
233
305
def days_in_month (self ) -> int : ...
234
306
@property
0 commit comments