@@ -220,6 +220,19 @@ def format_array_from_datetime(
220220 return result
221221
222222
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+
223236def array_with_unit_to_datetime (
224237 ndarray[object] values ,
225238 str unit ,
@@ -261,13 +274,10 @@ def array_with_unit_to_datetime(
261274 bint is_raise = errors== " raise"
262275 ndarray[int64_t] iresult
263276 object tz = None
264- bint is_ym
265277 float fval
266278
267279 assert is_ignore or is_coerce or is_raise
268280
269- is_ym = unit in " YM"
270-
271281 if unit == " ns" :
272282 result, tz = array_to_datetime(
273283 values.astype(object , copy = False ),
@@ -292,19 +302,7 @@ def array_with_unit_to_datetime(
292302 if val != val or val == NPY_NAT:
293303 iresult[i] = NPY_NAT
294304 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)
308306
309307 elif isinstance (val, str ):
310308 if len (val) == 0 or val in nat_strings:
@@ -319,23 +317,7 @@ def array_with_unit_to_datetime(
319317 f" non convertible value {val} with the unit '{unit}'"
320318 )
321319
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)
339321
340322 else :
341323 # TODO: makes more sense as TypeError, but that would be an
0 commit comments