8
8
9
9
from pandas ._libs .tslibs import (
10
10
NaT ,
11
- OutOfBoundsDatetime ,
12
11
Period ,
13
12
Timedelta ,
14
13
Timestamp ,
15
14
ccalendar ,
16
15
conversion ,
17
16
delta_to_nanoseconds ,
18
17
frequencies as libfrequencies ,
19
- normalize_date ,
20
18
offsets as liboffsets ,
21
- timezones ,
22
19
)
23
20
from pandas ._libs .tslibs .offsets import (
24
21
ApplyTypeError ,
76
73
"DateOffset" ,
77
74
]
78
75
79
- # convert to/from datetime/timestamp to allow invalid Timestamp ranges to
80
- # pass thru
81
-
82
-
83
- def as_timestamp (obj ):
84
- if isinstance (obj , Timestamp ):
85
- return obj
86
- try :
87
- return Timestamp (obj )
88
- except (OutOfBoundsDatetime ):
89
- pass
90
- return obj
91
-
92
76
93
77
def apply_wraps (func ):
94
78
@functools .wraps (func )
95
79
def wrapper (self , other ):
96
80
if other is NaT :
97
81
return NaT
98
- elif isinstance (other , (timedelta , Tick , DateOffset )):
82
+ elif isinstance (other , (timedelta , DateOffset )):
99
83
# timedelta path
100
84
return func (self , other )
101
85
elif isinstance (other , (np .datetime64 , datetime , date )):
102
- other = as_timestamp (other )
103
-
104
- tz = getattr (other , "tzinfo" , None )
105
- nano = getattr (other , "nanosecond" , 0 )
106
-
107
- try :
108
- if self ._adjust_dst and isinstance (other , Timestamp ):
109
- other = other .tz_localize (None )
110
-
111
- result = func (self , other )
112
-
113
- if self ._adjust_dst :
114
- result = conversion .localize_pydatetime (result , tz )
86
+ other = Timestamp (other )
87
+ else :
88
+ raise TypeError (other )
115
89
116
- result = Timestamp (result )
117
- if self .normalize :
118
- result = result .normalize ()
90
+ tz = other .tzinfo
91
+ nano = other .nanosecond
119
92
120
- # nanosecond may be deleted depending on offset process
121
- if not self .normalize and nano != 0 :
122
- if not isinstance (self , Nano ) and result .nanosecond != nano :
123
- if result .tz is not None :
124
- # convert to UTC
125
- value = conversion .tz_convert_single (
126
- result .value , timezones .UTC , result .tz
127
- )
128
- else :
129
- value = result .value
130
- result = Timestamp (value + nano )
93
+ if self ._adjust_dst :
94
+ other = other .tz_localize (None )
131
95
132
- if tz is not None and result .tzinfo is None :
133
- result = conversion .localize_pydatetime (result , tz )
96
+ result = func (self , other )
134
97
135
- except OutOfBoundsDatetime :
136
- result = func (self , as_datetime (other ))
98
+ result = Timestamp (result )
99
+ if self ._adjust_dst :
100
+ result = result .tz_localize (tz )
137
101
138
- if self .normalize :
139
- # normalize_date returns normal datetime
140
- result = normalize_date (result )
102
+ if self .normalize :
103
+ result = result .normalize ()
141
104
142
- if tz is not None and result .tzinfo is None :
143
- result = conversion .localize_pydatetime (result , tz )
105
+ # nanosecond may be deleted depending on offset process
106
+ if not self .normalize and nano != 0 :
107
+ if not isinstance (self , Nano ) and result .nanosecond != nano :
108
+ if result .tz is not None :
109
+ # convert to UTC
110
+ value = result .tz_localize (None ).value
111
+ else :
112
+ value = result .value
113
+ result = Timestamp (value + nano )
144
114
145
- result = Timestamp (result )
115
+ if tz is not None and result .tzinfo is None :
116
+ result = result .tz_localize (tz )
146
117
147
118
return result
148
119
@@ -290,7 +261,7 @@ def apply(self, other):
290
261
# bring tz back from UTC calculation
291
262
other = conversion .localize_pydatetime (other , tzinfo )
292
263
293
- return as_timestamp (other )
264
+ return Timestamp (other )
294
265
else :
295
266
return other + timedelta (self .n )
296
267
@@ -394,7 +365,7 @@ def rollback(self, dt):
394
365
TimeStamp
395
366
Rolled timestamp if not on offset, otherwise unchanged timestamp.
396
367
"""
397
- dt = as_timestamp (dt )
368
+ dt = Timestamp (dt )
398
369
if not self .is_on_offset (dt ):
399
370
dt = dt - type (self )(1 , normalize = self .normalize , ** self .kwds )
400
371
return dt
@@ -408,7 +379,7 @@ def rollforward(self, dt):
408
379
TimeStamp
409
380
Rolled timestamp if not on offset, otherwise unchanged timestamp.
410
381
"""
411
- dt = as_timestamp (dt )
382
+ dt = Timestamp (dt )
412
383
if not self .is_on_offset (dt ):
413
384
dt = dt + type (self )(1 , normalize = self .normalize , ** self .kwds )
414
385
return dt
@@ -2505,7 +2476,7 @@ def apply(self, other):
2505
2476
raise OverflowError
2506
2477
return result
2507
2478
elif isinstance (other , (datetime , np .datetime64 , date )):
2508
- return as_timestamp (other ) + self
2479
+ return Timestamp (other ) + self
2509
2480
2510
2481
if isinstance (other , timedelta ):
2511
2482
return other + self .delta
0 commit comments