Skip to content

Commit d52fbf8

Browse files
authored
Update timestamps.pyx
1 parent d81882b commit d52fbf8

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ cdef class _Timestamp(ABCTimestamp):
12371237
12381238
# -----------------------------------------------------------------
12391239
# Transformation Methods
1240-
1240+
12411241
def normalize(self) -> "Timestamp":
12421242
"""
12431243
Normalize Timestamp to midnight, preserving tz information.
@@ -1264,14 +1264,27 @@ cdef class _Timestamp(ABCTimestamp):
12641264
Timestamp('2020-03-14 00:00:00')
12651265
"""
12661266
cdef:
1267-
local_val = self._maybe_convert_value_to_local()
1268-
int64_t normalized
1269-
int64_t ppd = periods_per_day(self._creso)
1270-
_Timestamp ts
1271-
1272-
normalized = normalize_i8_stamp(local_val, ppd)
1273-
ts = type(self)._from_value_and_reso(normalized, reso=self._creso, tz=None)
1274-
return ts.tz_localize(self.tzinfo)
1267+
local_val = self._maybe_convert_value_to_local()
1268+
int64_t normalized
1269+
int64_t ppd = periods_per_day(self._creso)
1270+
_Timestamp ts
1271+
1272+
# Check for potential overflow before normalization
1273+
if local_val < INT64_MIN or local_val > INT64_MAX:
1274+
raise OutOfBoundsDatetime(
1275+
f"Cannot normalize Timestamp {self} without overflow"
1276+
)
1277+
1278+
normalized = normalize_i8_stamp(local_val, ppd)
1279+
1280+
# Additional overflow check after normalization
1281+
if normalized < INT64_MIN or normalized > INT64_MAX:
1282+
raise OutOfBoundsDatetime(
1283+
f"Normalization of {self} would cause an overflow"
1284+
)
1285+
1286+
ts = type(self)._from_value_and_reso(normalized, reso=self._creso, tz=None)
1287+
return ts.tz_localize(self.tzinfo)
12751288
12761289
# -----------------------------------------------------------------
12771290
# Pickle Methods

0 commit comments

Comments
 (0)