Skip to content

BUG: Timedeltas with no specified units (and frac) should raise, #10426 #10429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Added vbench benchmarks for alternative ExcelWriter engines and reading Excel files (:issue:`7171`)

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

.. _whatsnew_0170.bug_fixes:
Expand Down
25 changes: 14 additions & 11 deletions pandas/tseries/tests/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ def test_construction(self):
# only leading neg signs are allowed
self.assertRaises(ValueError, lambda : Timedelta('10 days -1 h 1.5m 1s 3us'))

# no units specified
self.assertRaises(ValueError, lambda : Timedelta('3.1415'))

# invalid construction
tm.assertRaisesRegexp(ValueError,
"cannot construct a TimeDelta",
lambda : Timedelta())
tm.assertRaisesRegexp(ValueError,
"unit abbreviation w/o a number",
lambda : Timedelta('foo'))
tm.assertRaisesRegexp(ValueError,
"cannot construct a TimeDelta from the passed arguments, allowed keywords are ",
lambda : Timedelta(day=10))

# roundtripping both for string and value
for v in ['1s',
'-1s',
Expand Down Expand Up @@ -149,17 +163,6 @@ def test_construction(self):
self.assertEqual(Timedelta(pd.offsets.Hour(2)),Timedelta('0 days, 02:00:00'))
self.assertEqual(Timedelta(pd.offsets.Second(2)),Timedelta('0 days, 00:00:02'))

# invalid
tm.assertRaisesRegexp(ValueError,
"cannot construct a TimeDelta",
lambda : Timedelta())
tm.assertRaisesRegexp(ValueError,
"unit abbreviation w/o a number",
lambda : Timedelta('foo'))
tm.assertRaisesRegexp(ValueError,
"cannot construct a TimeDelta from the passed arguments, allowed keywords are ",
lambda : Timedelta(day=10))

def test_repr(self):

self.assertEqual(repr(Timedelta(10,unit='d')),"Timedelta('10 days 00:00:00')")
Expand Down
5 changes: 4 additions & 1 deletion pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,6 @@ cdef inline parse_timedelta_string(object ts, coerce=False):
have_value = 1
have_dot = 0


# we had a dot, but we have a fractional
# value since we have an unit
if have_dot and len(unit):
Expand All @@ -2415,6 +2414,10 @@ cdef inline parse_timedelta_string(object ts, coerce=False):
# we have a dot as part of a regular format
# e.g. hh:mm:ss.fffffff
elif have_dot:

if (len(number) or len(frac)) and not len(unit) and current_unit is None:
raise ValueError("no units specified")

if len(frac) > 0 and len(frac) <= 3:
m = 10**(3-len(frac)) * 1000L * 1000L
elif len(frac) > 3 and len(frac) <= 6:
Expand Down