Skip to content

Commit 9012a9f

Browse files
authored
REF: array_with_unit_to_datetime (#50737)
1 parent c426dc0 commit 9012a9f

File tree

1 file changed

+45
-64
lines changed

1 file changed

+45
-64
lines changed

pandas/_libs/tslib.pyx

+45-64
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ def array_with_unit_to_datetime(
262262
bint is_coerce = errors=="coerce"
263263
bint is_raise = errors=="raise"
264264
ndarray[int64_t] iresult
265-
ndarray[object] oresult
266265
object tz = None
267266
bint is_ym
268267
float fval
@@ -283,10 +282,10 @@ def array_with_unit_to_datetime(
283282
result = np.empty(n, dtype="M8[ns]")
284283
iresult = result.view("i8")
285284

286-
try:
287-
for i in range(n):
288-
val = values[i]
285+
for i in range(n):
286+
val = values[i]
289287

288+
try:
290289
if checknull_with_nat_and_na(val):
291290
iresult[i] = NPY_NAT
292291

@@ -297,26 +296,17 @@ def array_with_unit_to_datetime(
297296
else:
298297
if is_ym and is_float_object(val) and not val.is_integer():
299298
# 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+
)
309303

310304
try:
311305
iresult[i] = cast_from_unit(val, unit)
312306
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+
)
320310

321311
elif isinstance(val, str):
322312
if len(val) == 0 or val in nat_strings:
@@ -327,65 +317,56 @@ def array_with_unit_to_datetime(
327317
try:
328318
fval = float(val)
329319
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+
)
338323

339324
if is_ym and not fval.is_integer():
340325
# 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+
)
350330

351331
try:
352332
iresult[i] = cast_from_unit(fval, unit)
353333
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+
)
361337
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+
)
369341

370342
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+
)
371348

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
379360
iresult[i] = NPY_NAT
380361

381-
return result, tz
362+
return result, tz
382363

383-
except AssertionError:
384-
pass
385364

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
389370

390371
# TODO: fix subtle differences between this and no-unit code
391372
oresult = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0)

0 commit comments

Comments
 (0)