Skip to content

Commit ad37b5d

Browse files
committed
Merge pull request #10429 from jreback/td2
BUG: Timedeltas with no specified units (and frac) should raise, #10426
2 parents 499eb21 + 09b73d5 commit ad37b5d

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

doc/source/whatsnew/v0.17.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Performance Improvements
6161
~~~~~~~~~~~~~~~~~~~~~~~~
6262
- Added vbench benchmarks for alternative ExcelWriter engines and reading Excel files (:issue:`7171`)
6363

64-
- 4x improvement in ``timedelta`` string parsing (:issue:`6755`)
64+
- 4x improvement in ``timedelta`` string parsing (:issue:`6755`, :issue:`10426`)
6565
- 8x improvement in ``timedelta64`` and ``datetime64`` ops (:issue:`6755`)
6666

6767
.. _whatsnew_0170.bug_fixes:

pandas/tseries/tests/test_timedeltas.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ def test_construction(self):
112112
# only leading neg signs are allowed
113113
self.assertRaises(ValueError, lambda : Timedelta('10 days -1 h 1.5m 1s 3us'))
114114

115+
# no units specified
116+
self.assertRaises(ValueError, lambda : Timedelta('3.1415'))
117+
118+
# invalid construction
119+
tm.assertRaisesRegexp(ValueError,
120+
"cannot construct a TimeDelta",
121+
lambda : Timedelta())
122+
tm.assertRaisesRegexp(ValueError,
123+
"unit abbreviation w/o a number",
124+
lambda : Timedelta('foo'))
125+
tm.assertRaisesRegexp(ValueError,
126+
"cannot construct a TimeDelta from the passed arguments, allowed keywords are ",
127+
lambda : Timedelta(day=10))
128+
115129
# roundtripping both for string and value
116130
for v in ['1s',
117131
'-1s',
@@ -149,17 +163,6 @@ def test_construction(self):
149163
self.assertEqual(Timedelta(pd.offsets.Hour(2)),Timedelta('0 days, 02:00:00'))
150164
self.assertEqual(Timedelta(pd.offsets.Second(2)),Timedelta('0 days, 00:00:02'))
151165

152-
# invalid
153-
tm.assertRaisesRegexp(ValueError,
154-
"cannot construct a TimeDelta",
155-
lambda : Timedelta())
156-
tm.assertRaisesRegexp(ValueError,
157-
"unit abbreviation w/o a number",
158-
lambda : Timedelta('foo'))
159-
tm.assertRaisesRegexp(ValueError,
160-
"cannot construct a TimeDelta from the passed arguments, allowed keywords are ",
161-
lambda : Timedelta(day=10))
162-
163166
def test_repr(self):
164167

165168
self.assertEqual(repr(Timedelta(10,unit='d')),"Timedelta('10 days 00:00:00')")

pandas/tslib.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -2400,7 +2400,6 @@ cdef inline parse_timedelta_string(object ts, coerce=False):
24002400
have_value = 1
24012401
have_dot = 0
24022402

2403-
24042403
# we had a dot, but we have a fractional
24052404
# value since we have an unit
24062405
if have_dot and len(unit):
@@ -2415,6 +2414,10 @@ cdef inline parse_timedelta_string(object ts, coerce=False):
24152414
# we have a dot as part of a regular format
24162415
# e.g. hh:mm:ss.fffffff
24172416
elif have_dot:
2417+
2418+
if (len(number) or len(frac)) and not len(unit) and current_unit is None:
2419+
raise ValueError("no units specified")
2420+
24182421
if len(frac) > 0 and len(frac) <= 3:
24192422
m = 10**(3-len(frac)) * 1000L * 1000L
24202423
elif len(frac) > 3 and len(frac) <= 6:

0 commit comments

Comments
 (0)