Skip to content

Commit

Permalink
BUG: to_timedelta not properly converting some units (GH6855)
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Apr 10, 2014
1 parent 6387f38 commit 6ac4d74
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pandas/tseries/tests/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ def conv(v):
expected = Series([ np.timedelta64(1,'D') ]*5)
tm.assert_series_equal(result, expected)

# validate all units
# GH 6855
for unit in ['Y','M','W','D','y','w','d']:
result = to_timedelta(np.arange(5),unit=unit)
expected = Series([ np.timedelta64(i,unit.upper()) for i in np.arange(5).tolist() ])
tm.assert_series_equal(result, expected)
for unit in ['h','m','s','ms','us','ns','H','S','MS','US','NS']:
result = to_timedelta(np.arange(5),unit=unit)
expected = Series([ np.timedelta64(i,unit.lower()) for i in np.arange(5).tolist() ])
tm.assert_series_equal(result, expected)

# these will error
self.assertRaises(ValueError, lambda : to_timedelta(['1h']))
self.assertRaises(ValueError, lambda : to_timedelta(['1m']))
Expand Down
11 changes: 11 additions & 0 deletions pandas/tseries/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def _convert_listlike(arg, box, unit):
if is_timedelta64_dtype(arg):
value = arg.astype('timedelta64[ns]')
elif is_integer_dtype(arg):
unit = _validate_timedelta_unit(unit)

# these are shortcutable
value = arg.astype('timedelta64[{0}]'.format(unit)).astype('timedelta64[ns]')
else:
Expand All @@ -65,6 +67,15 @@ def _convert_listlike(arg, box, unit):
# ...so it must be a scalar value. Return scalar.
return _coerce_scalar_to_timedelta_type(arg, unit=unit)

def _validate_timedelta_unit(arg):
""" provide validation / translation for timedelta short units """

if re.search("Y|W|D",arg,re.IGNORECASE) or arg == 'M':
return arg.upper()
elif re.search("h|m|s|ms|us|ns",arg,re.IGNORECASE):
return arg.lower()
raise ValueError("invalid timedelta unit {0} provided".format(arg))

_short_search = re.compile(
"^\s*(?P<neg>-?)\s*(?P<value>\d*\.?\d*)\s*(?P<unit>d|s|ms|us|ns)?\s*$",re.IGNORECASE)
_full_search = re.compile(
Expand Down

0 comments on commit 6ac4d74

Please sign in to comment.