Skip to content

Commit 3c5b319

Browse files
author
Victor
committed
Passing tests.
1 parent 10a212e commit 3c5b319

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

pandas/_libs/tslibs/timedeltas.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from cpython.datetime cimport timedelta
66
from numpy cimport int64_t, ndarray
77

88
# Exposed for tslib, not intended for outside use.
9-
cdef parse_timedelta_string(object ts, object specified_unit=None)
9+
cdef parse_timedelta_string(object ts, specified_unit=*)
1010
cpdef int64_t cast_from_unit(object ts, object unit) except? -1
1111
cpdef int64_t delta_to_nanoseconds(delta) except? -1
1212
cpdef convert_to_timedelta64(object ts, object unit)

pandas/_libs/tslibs/timedeltas.pyx

+16-7
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ cdef inline _decode_if_necessary(object ts):
245245
return ts
246246

247247

248-
cdef inline parse_timedelta_string(object ts, object specified_unit=None):
248+
cdef inline parse_timedelta_string(object ts, specified_unit=None):
249249
"""
250250
Parse a regular format timedelta string. Return an int64_t (in ns)
251251
or raise a ValueError on an invalid parse.
@@ -407,10 +407,7 @@ cdef inline parse_timedelta_string(object ts, object specified_unit=None):
407407
if have_value:
408408
raise ValueError("have leftover units")
409409
if len(number):
410-
if specified_unit is None:
411-
fallback_unit = 'ns'
412-
else:
413-
fallback_unit = specified_unit
410+
fallback_unit = 'ns' if specified_unit is None else specified_unit
414411
r = timedelta_from_spec(number, frac, fallback_unit)
415412
result += timedelta_as_neg(r, neg)
416413

@@ -1004,13 +1001,25 @@ class Timedelta(_Timedelta):
10041001
"[weeks, days, hours, minutes, seconds, "
10051002
"milliseconds, microseconds, nanoseconds]")
10061003

1004+
10071005
if isinstance(value, Timedelta):
10081006
value = value.value
10091007
elif is_string_object(value):
1010-
if len(value) > 0 and value[0] == 'P':
1008+
# Check if it is just a number in a string
1009+
try:
1010+
value = int(value)
1011+
except (ValueError, TypeError):
1012+
try:
1013+
value = float(value)
1014+
except (ValueError, TypeError):
1015+
pass
1016+
1017+
if is_integer_object(value) or is_float_object(value):
1018+
value = convert_to_timedelta64(value, unit)
1019+
elif len(value) > 0 and value[0] == 'P':
10111020
value = parse_iso_format_string(value)
10121021
else:
1013-
value = parse_timedelta_string(value)
1022+
value = parse_timedelta_string(value, unit)
10141023
value = np.timedelta64(value)
10151024
elif PyDelta_Check(value):
10161025
value = convert_to_timedelta64(value, 'ns')

pandas/tests/scalar/timedelta/test_construction.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ def test_construction():
8585
with pytest.raises(ValueError):
8686
Timedelta('10 days -1 h 1.5m 1s 3us')
8787

88-
# no units specified
89-
with pytest.raises(ValueError):
90-
Timedelta('3.1415')
91-
9288
# invalid construction
9389
tm.assert_raises_regex(ValueError, "cannot construct a Timedelta",
9490
lambda: Timedelta())
@@ -225,12 +221,12 @@ def __exit__(self, exc_type, exc_val, exc_tb):
225221
("d", pytest.raises(ValueError)),
226222
("us", pytest.raises(ValueError))])
227223
@pytest.mark.parametrize("unit", [
228-
"d", "m", "s", "us"])
224+
"d", "m", "s", "us"])
229225
@pytest.mark.parametrize("sign", [
230-
+1, -1])
226+
+1, -1])
231227
@pytest.mark.parametrize("num", [
232228
0.001, 1, 10])
233229
def test_string_with_unit(num, sign, unit, redundant_unit, expectation):
234230
with expectation:
235231
assert Timedelta(str(sign * num) + redundant_unit, unit=unit) \
236-
== Timedelta(sign * num, unit=unit)
232+
== Timedelta(sign * num, unit=unit)

0 commit comments

Comments
 (0)