@@ -262,7 +262,6 @@ def array_with_unit_to_datetime(
262
262
bint is_coerce = errors== " coerce"
263
263
bint is_raise = errors== " raise"
264
264
ndarray[int64_t] iresult
265
- ndarray[object ] oresult
266
265
object tz = None
267
266
bint is_ym
268
267
float fval
@@ -283,10 +282,10 @@ def array_with_unit_to_datetime(
283
282
result = np.empty(n, dtype = " M8[ns]" )
284
283
iresult = result.view(" i8" )
285
284
286
- try :
287
- for i in range (n):
288
- val = values[i]
285
+ for i in range (n):
286
+ val = values[i]
289
287
288
+ try :
290
289
if checknull_with_nat_and_na(val):
291
290
iresult[i] = NPY_NAT
292
291
@@ -297,26 +296,17 @@ def array_with_unit_to_datetime(
297
296
else :
298
297
if is_ym and is_float_object(val) and not val.is_integer():
299
298
# Analogous to GH#47266 for Timestamp
300
- if is_raise:
301
- raise ValueError (
302
- f" Conversion of non-round float with unit={unit} "
303
- " is ambiguous"
304
- )
305
- elif is_ignore:
306
- raise AssertionError
307
- iresult[i] = NPY_NAT
308
- continue
299
+ raise ValueError (
300
+ f" Conversion of non-round float with unit={unit} "
301
+ " is ambiguous"
302
+ )
309
303
310
304
try :
311
305
iresult[i] = cast_from_unit(val, unit)
312
306
except OverflowError :
313
- if is_raise:
314
- raise OutOfBoundsDatetime(
315
- f" cannot convert input {val} with the unit '{unit}'"
316
- )
317
- elif is_ignore:
318
- raise AssertionError
319
- iresult[i] = NPY_NAT
307
+ raise OutOfBoundsDatetime(
308
+ f" cannot convert input {val} with the unit '{unit}'"
309
+ )
320
310
321
311
elif isinstance (val, str ):
322
312
if len (val) == 0 or val in nat_strings:
@@ -327,65 +317,56 @@ def array_with_unit_to_datetime(
327
317
try :
328
318
fval = float (val)
329
319
except ValueError :
330
- if is_raise:
331
- raise ValueError (
332
- f" non convertible value {val} with the unit '{unit}'"
333
- )
334
- elif is_ignore:
335
- raise AssertionError
336
- iresult[i] = NPY_NAT
337
- continue
320
+ raise ValueError (
321
+ f" non convertible value {val} with the unit '{unit}'"
322
+ )
338
323
339
324
if is_ym and not fval.is_integer():
340
325
# Analogous to GH#47266 for Timestamp
341
- if is_raise:
342
- raise ValueError (
343
- f" Conversion of non-round float with unit={unit} "
344
- " is ambiguous"
345
- )
346
- elif is_ignore:
347
- raise AssertionError
348
- iresult[i] = NPY_NAT
349
- continue
326
+ raise ValueError (
327
+ f" Conversion of non-round float with unit={unit} "
328
+ " is ambiguous"
329
+ )
350
330
351
331
try :
352
332
iresult[i] = cast_from_unit(fval, unit)
353
333
except ValueError :
354
- if is_raise:
355
- raise ValueError (
356
- f" non convertible value {val} with the unit '{unit}'"
357
- )
358
- elif is_ignore:
359
- raise AssertionError
360
- iresult[i] = NPY_NAT
334
+ raise ValueError (
335
+ f" non convertible value {val} with the unit '{unit}'"
336
+ )
361
337
except OverflowError :
362
- if is_raise:
363
- raise OutOfBoundsDatetime(
364
- f" cannot convert input {val} with the unit '{unit}'"
365
- )
366
- elif is_ignore:
367
- raise AssertionError
368
- iresult[i] = NPY_NAT
338
+ raise OutOfBoundsDatetime(
339
+ f" cannot convert input {val} with the unit '{unit}'"
340
+ )
369
341
370
342
else :
343
+ # TODO: makes more sense as TypeError, but that would be an
344
+ # API change.
345
+ raise ValueError (
346
+ f" unit='{unit}' not valid with non-numerical val='{val}'"
347
+ )
371
348
372
- if is_raise:
373
- raise ValueError (
374
- f" unit='{unit}' not valid with non-numerical val='{val}'"
375
- )
376
- if is_ignore:
377
- raise AssertionError
378
-
349
+ except (ValueError , OutOfBoundsDatetime, TypeError ) as err:
350
+ if is_raise:
351
+ err.args = (f" {err}, at position {i}" ,)
352
+ raise
353
+ elif is_ignore:
354
+ # we have hit an exception
355
+ # and are in ignore mode
356
+ # redo as object
357
+ return _array_with_unit_to_datetime_object_fallback(values, unit)
358
+ else :
359
+ # is_coerce
379
360
iresult[i] = NPY_NAT
380
361
381
- return result, tz
362
+ return result, tz
382
363
383
- except AssertionError :
384
- pass
385
364
386
- # we have hit an exception
387
- # and are in ignore mode
388
- # redo as object
365
+ cdef _array_with_unit_to_datetime_object_fallback(ndarray[object ] values, str unit):
366
+ cdef:
367
+ Py_ssize_t i, n = len (values)
368
+ ndarray[object ] oresult
369
+ object tz = None
389
370
390
371
# TODO: fix subtle differences between this and no-unit code
391
372
oresult = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0 )
0 commit comments