Skip to content

Commit 708fd66

Browse files
author
victor
committed
Checking float with units in Timedelta class.
1 parent 82cdf2d commit 708fd66

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

pandas/_libs/tslibs/timedeltas.pyx

+10-3
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ class Timedelta(_Timedelta):
10811081
----------
10821082
value : Timedelta, timedelta, np.timedelta64, string, or integer
10831083
unit : string, {'ns', 'us', 'ms', 's', 'm', 'h', 'D'}, optional
1084-
Denote the unit of the input, if input is an integer. Default 'ns'.
1084+
Denote the unit of the input, if input is an integer/float. Default 'ns'.
10851085
days, seconds, microseconds,
10861086
milliseconds, minutes, hours, weeks : numeric, optional
10871087
Values for construction in compat with datetime.timedelta.
@@ -1119,8 +1119,15 @@ class Timedelta(_Timedelta):
11191119
if len(value) > 0 and value[0] == 'P':
11201120
value = parse_iso_format_string(value)
11211121
else:
1122-
value = parse_timedelta_string(value)
1123-
value = np.timedelta64(value)
1122+
try:
1123+
value = float(value)
1124+
except ValueError:
1125+
value = parse_timedelta_string(value)
1126+
value = np.timedelta64(value)
1127+
else:
1128+
if unit is None:
1129+
raise ValueError("Cannot convert float string without unit.")
1130+
value = convert_to_timedelta64(value, unit)
11241131
elif PyDelta_Check(value):
11251132
value = convert_to_timedelta64(value, 'ns')
11261133
elif is_timedelta64_object(value):

0 commit comments

Comments
 (0)