Skip to content

Commit 3991c5e

Browse files
author
victor
committed
Checking float with units in Timedelta class.
1 parent bc0c897 commit 3991c5e

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

pandas/_libs/tslibs/timedeltas.pyx

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

0 commit comments

Comments
 (0)