Skip to content

Commit ba0cd30

Browse files
committed
REF: move cases out of convert_to_timedelta64
1 parent d7621cd commit ba0cd30

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,7 @@ cdef convert_to_timedelta64(object ts, str unit):
341341
Return an ns based int64
342342
"""
343343
# Caller is responsible for checking unit not in ["Y", "y", "M"]
344-
if checknull_with_nat_and_na(ts):
345-
return np.timedelta64(NPY_NAT, "ns")
346-
elif isinstance(ts, _Timedelta):
344+
if isinstance(ts, _Timedelta):
347345
# already in the proper format
348346
if ts._creso != NPY_FR_ns:
349347
ts = ts.as_unit("ns").asm8
@@ -436,8 +434,11 @@ def array_to_timedelta64(
436434
item = <object>(<PyObject**>cnp.PyArray_MultiIter_DATA(mi, 1))[0]
437435

438436
try:
439-
td64ns_obj = convert_to_timedelta64(item, parsed_unit)
440-
ival = cnp.get_timedelta64_value(td64ns_obj)
437+
if checknull_with_nat_and_na(item):
438+
ival = NPY_NAT
439+
else:
440+
td64ns_obj = convert_to_timedelta64(item, parsed_unit)
441+
ival = cnp.get_timedelta64_value(td64ns_obj)
441442
except ValueError as err:
442443
if errors == "coerce":
443444
ival = NPY_NAT
@@ -2114,12 +2115,14 @@ class Timedelta(_Timedelta):
21142115
new_value = delta_to_nanoseconds(value, reso=new_reso)
21152116
return cls._from_value_and_reso(new_value, reso=new_reso)
21162117

2118+
elif checknull_with_nat_and_na(value):
2119+
return NaT
2120+
21172121
elif is_integer_object(value) or is_float_object(value):
21182122
# unit=None is de-facto 'ns'
21192123
unit = parse_timedelta_unit(unit)
21202124
value = convert_to_timedelta64(value, unit)
2121-
elif checknull_with_nat_and_na(value):
2122-
return NaT
2125+
21232126
else:
21242127
raise ValueError(
21252128
"Value must be Timedelta, string, integer, "

0 commit comments

Comments
 (0)