Skip to content

Commit 1c0defa

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

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,8 @@ cdef convert_to_timedelta64(object ts, str unit):
333333
334334
Handle these types of objects:
335335
- timedelta/Timedelta
336-
- timedelta64
337-
- an offset
338-
- np.int64 (with unit providing a possible modifier)
339-
- None/NaT
340336
341-
Return an ns based int64
337+
Return an timedelta64[ns] object
342338
"""
343339
# Caller is responsible for checking unit not in ["Y", "y", "M"]
344340
if isinstance(ts, _Timedelta):
@@ -347,42 +343,29 @@ cdef convert_to_timedelta64(object ts, str unit):
347343
ts = ts.as_unit("ns").asm8
348344
else:
349345
ts = np.timedelta64(ts._value, "ns")
350-
elif cnp.is_timedelta64_object(ts):
351-
ts = ensure_td64ns(ts)
352-
elif is_integer_object(ts):
353-
if ts == NPY_NAT:
354-
return np.timedelta64(NPY_NAT, "ns")
355-
else:
356-
ts = _maybe_cast_from_unit(ts, unit)
357-
elif is_float_object(ts):
358-
ts = _maybe_cast_from_unit(ts, unit)
359-
elif isinstance(ts, str):
360-
if (len(ts) > 0 and ts[0] == "P") or (len(ts) > 1 and ts[:2] == "-P"):
361-
ts = parse_iso_format_string(ts)
362-
else:
363-
ts = parse_timedelta_string(ts)
364-
ts = np.timedelta64(ts, "ns")
365-
elif is_tick_object(ts):
366-
ts = np.timedelta64(ts.nanos, "ns")
367346

368-
if PyDelta_Check(ts):
347+
elif PyDelta_Check(ts):
369348
ts = np.timedelta64(delta_to_nanoseconds(ts), "ns")
370349
elif not cnp.is_timedelta64_object(ts):
371350
raise TypeError(f"Invalid type for timedelta scalar: {type(ts)}")
372351
return ts.astype("timedelta64[ns]")
373352

374353

375-
cdef _maybe_cast_from_unit(ts, str unit):
354+
cdef _numeric_to_td64ns(object item, str unit):
376355
# caller is responsible for checking
377356
# assert unit not in ["Y", "y", "M"]
357+
# assert is_integer_object(item) or is_float_object(item)
358+
if is_integer_object(item) and item == NPY_NAT:
359+
return np.timedelta64(NPY_NAT, "ns")
360+
378361
try:
379-
ts = cast_from_unit(ts, unit)
362+
item = cast_from_unit(item, unit)
380363
except OutOfBoundsDatetime as err:
381364
raise OutOfBoundsTimedelta(
382-
f"Cannot cast {ts} from {unit} to 'ns' without overflow."
365+
f"Cannot cast {item} from {unit} to 'ns' without overflow."
383366
) from err
384367

385-
ts = np.timedelta64(ts, "ns")
368+
ts = np.timedelta64(item, "ns")
386369
return ts
387370

388371

@@ -436,9 +419,28 @@ def array_to_timedelta64(
436419
try:
437420
if checknull_with_nat_and_na(item):
438421
ival = NPY_NAT
422+
elif cnp.is_timedelta64_object(item):
423+
td64ns_obj = ensure_td64ns(item)
424+
ival = cnp.get_timedelta64_value(td64ns_obj)
425+
elif isinstance(item, str):
426+
if (
427+
(len(item) > 0 and item[0] == "P")
428+
or (len(item) > 1 and item[:2] == "-P")
429+
):
430+
item = parse_iso_format_string(item)
431+
else:
432+
item = parse_timedelta_string(item)
433+
td64ns_obj = np.timedelta64(item, "ns")
434+
ival = cnp.get_timedelta64_value(td64ns_obj)
435+
elif is_tick_object(item):
436+
ival = item.nanos
437+
elif is_integer_object(item) or is_float_object(item):
438+
td64ns_obj = _numeric_to_td64ns(item, unit)
439+
ival = cnp.get_timedelta64_value(td64ns_obj)
439440
else:
440441
td64ns_obj = convert_to_timedelta64(item, parsed_unit)
441442
ival = cnp.get_timedelta64_value(td64ns_obj)
443+
442444
except ValueError as err:
443445
if errors == "coerce":
444446
ival = NPY_NAT
@@ -2121,7 +2123,7 @@ class Timedelta(_Timedelta):
21212123
elif is_integer_object(value) or is_float_object(value):
21222124
# unit=None is de-facto 'ns'
21232125
unit = parse_timedelta_unit(unit)
2124-
value = convert_to_timedelta64(value, unit)
2126+
value = _numeric_to_td64ns(value, unit)
21252127

21262128
else:
21272129
raise ValueError(

0 commit comments

Comments
 (0)