1
+ """
2
+ This class could be comprehensibly typed with the typeshed .pyi file but the
3
+ datatetime.datetime and pd.Timestamp API differ quite a bit and cannot be used interchangeably
4
+ """
1
5
import sys
2
- from typing import Any , Optional , Union , overload
6
+ from typing import Any , Optional , Union , overload , TypeVar , Type
7
+
8
+ from pandas ._libs .tslibs .timedeltas import Timedelta
3
9
4
10
if sys .version_info >= (3 , 8 ):
5
11
from typing import Literal
6
12
else :
7
13
from typing_extensions import Literal
8
14
9
15
from dateutil .tz import tzfile
10
- from datetime import tzinfo , date
16
+ from datetime import tzinfo , datetime , date as _date , time , timedelta
11
17
12
18
from pandas ._libs .tslibs .period import Period
13
19
from pandas ._typing import TimestampConvertible
@@ -16,35 +22,52 @@ OptInt = Optional[int]
16
22
17
23
Fold = Literal [0 , 1 ]
18
24
19
- class Timestamp :
25
+ _S = TypeVar ("_S" )
26
+
27
+ class Timestamp (datetime ):
20
28
@overload
21
- def __init__ (self , ts_input : TimestampConvertible , freq : Optional [str ] = ..., tz : Optional [Union [str , tzinfo , tzfile ]] = ..., unit : Optional [str ] = ..., tzinfo : Optional [tzinfo ] = ..., fold : Optional [Fold ] = ...): ...
29
+ def __init__ (self , ts_input : TimestampConvertible , freq : Optional [str ] = ..., tz : Optional [Union [str , tzinfo , tzfile ]] = ...,
30
+ unit : Optional [str ] = ..., tzinfo : Optional [tzinfo ] = ..., fold : Optional [Fold ] = ...): ...
22
31
@overload
23
- def __init__ (self , year : OptInt = ..., month : OptInt = ..., day : OptInt = ..., hour : OptInt = ..., minute : OptInt = ..., second : OptInt = ..., microsecond : OptInt = ..., nanosecond : OptInt = ..., tz : Optional [Union [str , tzinfo , tzfile ]] = ..., tzinfo : Optional [tzinfo ] = ..., fold : Optional [Fold ] = ..., freq : Optional [str ] = ...): ...
32
+ def __init__ (self , year : OptInt = ..., month : OptInt = ..., day : OptInt = ..., hour : OptInt = ..., minute : OptInt = ...,
33
+ second : OptInt = ..., microsecond : OptInt = ..., nanosecond : OptInt = ..., tz : Optional [Union [str , tzinfo , tzfile ]] = ...,
34
+ tzinfo : Optional [tzinfo ] = ..., fold : Optional [Fold ] = ..., freq : Optional [str ] = ...): ...
24
35
def to_period (self , freq : Optional [str ]) -> Period : ...
25
36
def to_julian_date (self ) -> float : ...
26
37
def tz_localize (self , tz : Any = ..., ambigious : Any = ..., nonexistent : Any = ...) -> Timestamp : ...
27
38
def tz_convert (self , tz : Any ) -> Timestamp : ...
28
- def astimezone (self , tz : Any ) -> Timestamp : ...
29
- def replace (self , year : OptInt = ..., month : OptInt = ..., day : OptInt = ..., hour : OptInt = ..., minute : OptInt = ..., second : OptInt = ..., microsecond : OptInt = ..., nanosecond : OptInt = ..., tzinfo : Optional [tzinfo ] = ..., fold : Fold = ...) -> Timestamp : ...
39
+ @overload # type: ignore[override]
40
+ def __sub__ (self , other : datetime ) -> Timedelta : ...
41
+ @overload
42
+ def __sub__ (self , other : timedelta ) -> Timestamp : ...
43
+ def __add__ (self , other : timedelta ) -> Timestamp : ...
44
+ if sys .version_info >= (3 , 8 ):
45
+ def astimezone (self : _S , tz : Optional [tzinfo ] = ...) -> _S : ...
46
+ else :
47
+ def astimezone (self , tz : Optional [tzinfo ] = ...) -> datetime : ...
48
+ # This is correct. Typeshed doesn't include Optionals, or the Literal correctly
49
+ def replace (self , year : OptInt = ..., month : OptInt = ..., # type: ignore[override]
50
+ day : OptInt = ..., hour : OptInt = ..., minute : OptInt = ...,
51
+ second : OptInt = ..., microsecond : OptInt = ..., nanosecond : OptInt = ...,
52
+ tzinfo : Optional [tzinfo ] = ..., * , fold : Fold = ...) -> datetime : ...
30
53
def round (self , freq : str , ambiguous : Any = ..., nonexistent : Any = ...) -> Timestamp : ...
31
- def ceil (self , freq : str , ambiguous : Any = ...,nonexistent : Any = ...) -> Timestamp : ...
32
- def floor (self , freq : str , ambiguous : Any = ...,nonexistent : Any = ...) -> Timestamp : ...
33
- def isoformat (self , sep : str = ...) -> str : ...
54
+ def ceil (self , freq : str , ambiguous : Any = ..., nonexistent : Any = ...) -> Timestamp : ...
55
+ def floor (self , freq : str , ambiguous : Any = ..., nonexistent : Any = ...) -> Timestamp : ...
56
+ def isoformat (self , sep : str = ..., timespec : str = ... ) -> str : ...
34
57
def day_name (self , locale : Optional [str ]) -> str : ...
35
58
def month_name (self , locale : Optional [str ]) -> str : ...
36
59
def normalize (self ) -> Timestamp : ...
37
60
def strftime (self , format : str ) -> str : ...
38
- def date (self ) -> date : ...
39
-
61
+ def date (self ) -> _date : ...
40
62
@classmethod
41
63
def utcnow (cls ) -> Timestamp : ...
42
64
@classmethod
43
- def utcfromtimestamp (cls , ts : Timestamp ) -> Timestamp : ...
65
+ def utcfromtimestamp (cls , ts : float ) -> Timestamp : ...
66
+ # Pandas doesn't accept timezone here, unlike datetime
44
67
@classmethod
45
- def fromtimestamp (cls , ts : Timestamp ) -> Timestamp : ...
68
+ def fromtimestamp (cls : Type [ _S ], t : float ) -> _S : ... # type: ignore[override]
46
69
@classmethod
47
- def combine (cls , date : Any , time : Any ) -> Timestamp : ...
70
+ def combine (cls , date : _date , time : time , tzinfo : Optional [ tzinfo ] = ... ) -> Timestamp : ...
48
71
@classmethod
49
72
def now (cls , tz : Optional [Any ] = ...) -> Timestamp : ...
50
73
@classmethod
0 commit comments