Skip to content

Commit 2332161

Browse files
BUG: Timedelta input string with only symbols and no digits raises an error (GH39710) (#39737)
1 parent a92501e commit 2332161

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ Timedelta
292292
^^^^^^^^^
293293
- Bug in constructing :class:`Timedelta` from ``np.timedelta64`` objects with non-nanosecond units that are out of bounds for ``timedelta64[ns]`` (:issue:`38965`)
294294
- Bug in constructing a :class:`TimedeltaIndex` incorrectly accepting ``np.datetime64("NaT")`` objects (:issue:`39462`)
295-
-
295+
- Bug in constructing :class:`Timedelta` from input string with only symbols and no digits failed to raise an error (:issue:`39710`)
296296

297297
Timezones
298298
^^^^^^^^^

pandas/_libs/tslibs/timedeltas.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ cdef inline int64_t parse_timedelta_string(str ts) except? -1:
496496
else:
497497
raise ValueError("unit abbreviation w/o a number")
498498

499+
# we only have symbols and no numbers
500+
elif len(number) == 0:
501+
raise ValueError("symbols w/o a number")
502+
499503
# treat as nanoseconds
500504
# but only if we don't have anything else
501505
else:

pandas/tests/scalar/timedelta/test_constructors.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import timedelta
2+
from itertools import product
23

34
import numpy as np
45
import pytest
@@ -342,3 +343,22 @@ def test_string_with_unit(constructor, value, unit, expectation):
342343
exp, match = expectation
343344
with pytest.raises(exp, match=match):
344345
_ = constructor(value, unit=unit)
346+
347+
348+
@pytest.mark.parametrize(
349+
"value",
350+
[
351+
"".join(elements)
352+
for repetition in (1, 2)
353+
for elements in product("+-, ", repeat=repetition)
354+
],
355+
)
356+
def test_string_without_numbers(value):
357+
# GH39710 Timedelta input string with only symbols and no digits raises an error
358+
msg = (
359+
"symbols w/o a number"
360+
if value != "--"
361+
else "only leading negative signs are allowed"
362+
)
363+
with pytest.raises(ValueError, match=msg):
364+
Timedelta(value)

0 commit comments

Comments
 (0)