@@ -185,6 +185,8 @@ cdef int64_t cast_from_unit(
185
185
int64_t m
186
186
int p
187
187
NPY_DATETIMEUNIT in_reso
188
+ int64_t frac_int64
189
+ float64_t frac_float64
188
190
189
191
if unit in [" Y" , " M" ]:
190
192
if is_float_object(ts) and not ts.is_integer():
@@ -220,24 +222,29 @@ cdef int64_t cast_from_unit(
220
222
f" cannot convert input {ts} with the unit '{unit}'"
221
223
) from err
222
224
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).
225
+ # To maintain consistency across changes in NEP 50, handle the integer and
226
+ # float cases separately, using frac variables with explicitly declared
227
+ # types. See GH 56996 and 57984 for more discussion.
228
228
if isinstance (ts, np.integer):
229
- ts = < int64_t> ts
230
-
231
- frac = ts - base
232
- if p:
233
- frac = round (frac, p)
234
-
235
- try :
236
- return < int64_t> (base * m) + < int64_t> (frac * m)
237
- except OverflowError as err:
238
- raise OutOfBoundsDatetime(
239
- f" cannot convert input {ts} with the unit '{unit}'"
240
- ) from err
229
+ frac_int64 = ts - base
230
+ if p:
231
+ frac_int64 = round (frac_int64, p)
232
+ try :
233
+ return < int64_t> (base * m) + < int64_t> (frac_int64 * m)
234
+ except OverflowError as err:
235
+ raise OutOfBoundsDatetime(
236
+ f" cannot convert input {ts} with the unit '{unit}'"
237
+ ) from err
238
+ else :
239
+ frac_float64 = ts - base
240
+ if p:
241
+ frac_float64 = round (frac_float64, p)
242
+ try :
243
+ return < int64_t> (base * m) + < int64_t> (frac_float64 * m)
244
+ except OverflowError as err:
245
+ raise OutOfBoundsDatetime(
246
+ f" cannot convert input {ts} with the unit '{unit}'"
247
+ ) from err
241
248
242
249
243
250
cdef (int64_t, int ) precision_from_unit(
0 commit comments