Skip to content

Commit 5aadb12

Browse files
authored
CLN: remove unused out-of-bounds handling (#34006)
1 parent ebb727e commit 5aadb12

File tree

2 files changed

+30
-59
lines changed

2 files changed

+30
-59
lines changed

pandas/_libs/tslibs/conversion.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
265265
ts = <int64_t>ts
266266
except OverflowError:
267267
# GH#26651 re-raise as OutOfBoundsDatetime
268-
raise OutOfBoundsDatetime(ts)
268+
raise OutOfBoundsDatetime(f"Out of bounds nanosecond timestamp {ts}")
269269
if ts == NPY_NAT:
270270
obj.value = NPY_NAT
271271
else:

pandas/tseries/offsets.py

+29-58
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88

99
from pandas._libs.tslibs import (
1010
NaT,
11-
OutOfBoundsDatetime,
1211
Period,
1312
Timedelta,
1413
Timestamp,
1514
ccalendar,
1615
conversion,
1716
delta_to_nanoseconds,
1817
frequencies as libfrequencies,
19-
normalize_date,
2018
offsets as liboffsets,
21-
timezones,
2219
)
2320
from pandas._libs.tslibs.offsets import (
2421
ApplyTypeError,
@@ -76,73 +73,47 @@
7673
"DateOffset",
7774
]
7875

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-
9276

9377
def apply_wraps(func):
9478
@functools.wraps(func)
9579
def wrapper(self, other):
9680
if other is NaT:
9781
return NaT
98-
elif isinstance(other, (timedelta, Tick, DateOffset)):
82+
elif isinstance(other, (timedelta, DateOffset)):
9983
# timedelta path
10084
return func(self, other)
10185
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)
11589

116-
result = Timestamp(result)
117-
if self.normalize:
118-
result = result.normalize()
90+
tz = other.tzinfo
91+
nano = other.nanosecond
11992

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)
13195

132-
if tz is not None and result.tzinfo is None:
133-
result = conversion.localize_pydatetime(result, tz)
96+
result = func(self, other)
13497

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)
137101

138-
if self.normalize:
139-
# normalize_date returns normal datetime
140-
result = normalize_date(result)
102+
if self.normalize:
103+
result = result.normalize()
141104

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)
144114

145-
result = Timestamp(result)
115+
if tz is not None and result.tzinfo is None:
116+
result = result.tz_localize(tz)
146117

147118
return result
148119

@@ -290,7 +261,7 @@ def apply(self, other):
290261
# bring tz back from UTC calculation
291262
other = conversion.localize_pydatetime(other, tzinfo)
292263

293-
return as_timestamp(other)
264+
return Timestamp(other)
294265
else:
295266
return other + timedelta(self.n)
296267

@@ -394,7 +365,7 @@ def rollback(self, dt):
394365
TimeStamp
395366
Rolled timestamp if not on offset, otherwise unchanged timestamp.
396367
"""
397-
dt = as_timestamp(dt)
368+
dt = Timestamp(dt)
398369
if not self.is_on_offset(dt):
399370
dt = dt - type(self)(1, normalize=self.normalize, **self.kwds)
400371
return dt
@@ -408,7 +379,7 @@ def rollforward(self, dt):
408379
TimeStamp
409380
Rolled timestamp if not on offset, otherwise unchanged timestamp.
410381
"""
411-
dt = as_timestamp(dt)
382+
dt = Timestamp(dt)
412383
if not self.is_on_offset(dt):
413384
dt = dt + type(self)(1, normalize=self.normalize, **self.kwds)
414385
return dt
@@ -2505,7 +2476,7 @@ def apply(self, other):
25052476
raise OverflowError
25062477
return result
25072478
elif isinstance(other, (datetime, np.datetime64, date)):
2508-
return as_timestamp(other) + self
2479+
return Timestamp(other) + self
25092480

25102481
if isinstance(other, timedelta):
25112482
return other + self.delta

0 commit comments

Comments
 (0)