@@ -220,6 +220,19 @@ def format_array_from_datetime(
220
220
return result
221
221
222
222
223
+ cdef int64_t _wrapped_cast_from_unit(object val, str unit) except ? - 1 :
224
+ """
225
+ Call cast_from_unit and re-raise OverflowError as OutOfBoundsDatetime
226
+ """
227
+ # See also timedeltas._maybe_cast_from_unit
228
+ try :
229
+ return cast_from_unit(val, unit)
230
+ except OverflowError as err:
231
+ raise OutOfBoundsDatetime(
232
+ f" cannot convert input {val} with the unit '{unit}'"
233
+ ) from err
234
+
235
+
223
236
def array_with_unit_to_datetime (
224
237
ndarray[object] values ,
225
238
str unit ,
@@ -261,13 +274,10 @@ def array_with_unit_to_datetime(
261
274
bint is_raise = errors== " raise"
262
275
ndarray[int64_t] iresult
263
276
object tz = None
264
- bint is_ym
265
277
float fval
266
278
267
279
assert is_ignore or is_coerce or is_raise
268
280
269
- is_ym = unit in " YM"
270
-
271
281
if unit == " ns" :
272
282
result, tz = array_to_datetime(
273
283
values.astype(object , copy = False ),
@@ -292,19 +302,7 @@ def array_with_unit_to_datetime(
292
302
if val != val or val == NPY_NAT:
293
303
iresult[i] = NPY_NAT
294
304
else :
295
- if is_ym and is_float_object(val) and not val.is_integer():
296
- # Analogous to GH#47266 for Timestamp
297
- raise ValueError (
298
- f" Conversion of non-round float with unit={unit} "
299
- " is ambiguous"
300
- )
301
-
302
- try :
303
- iresult[i] = cast_from_unit(val, unit)
304
- except OverflowError :
305
- raise OutOfBoundsDatetime(
306
- f" cannot convert input {val} with the unit '{unit}'"
307
- )
305
+ iresult[i] = _wrapped_cast_from_unit(val, unit)
308
306
309
307
elif isinstance (val, str ):
310
308
if len (val) == 0 or val in nat_strings:
@@ -319,23 +317,7 @@ def array_with_unit_to_datetime(
319
317
f" non convertible value {val} with the unit '{unit}'"
320
318
)
321
319
322
- if is_ym and not fval.is_integer():
323
- # Analogous to GH#47266 for Timestamp
324
- raise ValueError (
325
- f" Conversion of non-round float with unit={unit} "
326
- " is ambiguous"
327
- )
328
-
329
- try :
330
- iresult[i] = cast_from_unit(fval, unit)
331
- except ValueError :
332
- raise ValueError (
333
- f" non convertible value {val} with the unit '{unit}'"
334
- )
335
- except OverflowError :
336
- raise OutOfBoundsDatetime(
337
- f" cannot convert input {val} with the unit '{unit}'"
338
- )
320
+ iresult[i] = _wrapped_cast_from_unit(fval, unit)
339
321
340
322
else :
341
323
# TODO: makes more sense as TypeError, but that would be an
0 commit comments