Skip to content

Commit 187b02f

Browse files
committed
Fix by casting ts to an integer instead
1 parent a087d40 commit 187b02f

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

pandas/_libs/tslibs/conversion.pyx

+10-8
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,17 @@ cdef int64_t cast_from_unit(
220220
f"cannot convert input {ts} with the unit '{unit}'"
221221
) from err
222222

223+
# In the event that ts itself is already an integer type, cast it to a
224+
# 64-bit integer to ensure that frac will also be a 64-bit integer after
225+
# type promotion changes in NEP 50. If this is not done, for example,
226+
# np.int32 values for ts can lead to np.int32 values for frac, which can
227+
# raise unexpected overflow errors downstream (GH 56996).
223228
if isinstance(ts, np.integer):
224-
# If ts is an integer then the fractional component will always be
225-
# zero. It helps to set this explicitly following changes to type
226-
# promotion behavior in NEP 50 (GH 56996).
227-
frac = 0
228-
else:
229-
frac = ts - base
230-
if p:
231-
frac = round(frac, p)
229+
ts = <int64_t>ts
230+
231+
frac = ts - base
232+
if p:
233+
frac = round(frac, p)
232234

233235
try:
234236
return <int64_t>(base * m) + <int64_t>(frac * m)

0 commit comments

Comments
 (0)