Skip to content

Commit 54e237b

Browse files
committed
Merge pull request #8787 from wholmgren/master
BUG: pd.Timedelta np.int, np.float. fixes #8757
2 parents b3df1ff + babf6ff commit 54e237b

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

doc/source/whatsnew/v0.15.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ Bug Fixes
6161
- ``io.data.Options`` now raises ``RemoteDataError`` when no expiry dates are available from Yahoo and when it receives no data from Yahoo (:issue:`8761`), (:issue:`8783`).
6262
- Bug in slicing a multi-index with an empty list and at least one boolean indexer (:issue:`8781`)
6363
- ``io.data.Options`` now raises ``RemoteDataError`` when no expiry dates are available from Yahoo (:issue:`8761`).
64+
- ``Timedelta`` kwargs may now be numpy ints and floats (:issue:`8757`).

pandas/tseries/tests/test_timedeltas.py

+12
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,24 @@ def test_construction(self):
3838
self.assertEqual(Timedelta(10.0,unit='d').value, expected)
3939
self.assertEqual(Timedelta('10 days').value, expected)
4040
self.assertEqual(Timedelta(days=10).value, expected)
41+
self.assertEqual(Timedelta(days=10.0).value, expected)
4142

4243
expected += np.timedelta64(10,'s').astype('m8[ns]').view('i8')
4344
self.assertEqual(Timedelta('10 days 00:00:10').value, expected)
4445
self.assertEqual(Timedelta(days=10,seconds=10).value, expected)
4546
self.assertEqual(Timedelta(days=10,milliseconds=10*1000).value, expected)
4647
self.assertEqual(Timedelta(days=10,microseconds=10*1000*1000).value, expected)
48+
49+
# test construction with np dtypes
50+
# GH 8757
51+
timedelta_kwargs = {'days':'D', 'seconds':'s', 'microseconds':'us',
52+
'milliseconds':'ms', 'minutes':'m', 'hours':'h', 'weeks':'W'}
53+
npdtypes = [np.int64, np.int32, np.int16,
54+
np.float64, np.float32, np.float16]
55+
for npdtype in npdtypes:
56+
for pykwarg, npkwarg in timedelta_kwargs.items():
57+
expected = np.timedelta64(1, npkwarg).astype('m8[ns]').view('i8')
58+
self.assertEqual(Timedelta(**{pykwarg:npdtype(1)}).value, expected)
4759

4860
# rounding cases
4961
self.assertEqual(Timedelta(82739999850000).value, 82739999850000)

pandas/tslib.pyx

+13-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ from datetime import time as datetime_time
3737
from dateutil.tz import (tzoffset, tzlocal as _dateutil_tzlocal, tzfile as _dateutil_tzfile,
3838
tzutc as _dateutil_tzutc, gettz as _dateutil_gettz)
3939
from pytz.tzinfo import BaseTzInfo as _pytz_BaseTzInfo
40-
from pandas.compat import parse_date, string_types, PY3
40+
from pandas.compat import parse_date, string_types, PY3, iteritems
4141

4242
from sys import version_info
4343
import operator
@@ -1619,6 +1619,7 @@ class Timedelta(_Timedelta):
16191619
Denote the unit of the input, if input is an integer. Default 'ns'.
16201620
days, seconds, microseconds, milliseconds, minutes, hours, weeks : numeric, optional
16211621
Values for construction in compat with datetime.timedelta.
1622+
np ints and floats will be coereced to python ints and floats.
16221623
16231624
Notes
16241625
-----
@@ -1632,9 +1633,19 @@ class Timedelta(_Timedelta):
16321633
if value is None:
16331634
if not len(kwargs):
16341635
raise ValueError("cannot construct a TimeDelta without a value/unit or descriptive keywords (days,seconds....)")
1636+
1637+
def _to_py_int_float(v):
1638+
if is_integer_object(v):
1639+
return int(v)
1640+
elif is_float_object(v):
1641+
return float(v)
1642+
raise TypeError("Invalid type {0}. Must be int or float.".format(type(v)))
1643+
1644+
kwargs = dict([ (k, _to_py_int_float(v)) for k, v in iteritems(kwargs) ])
1645+
16351646
try:
16361647
value = timedelta(**kwargs)
1637-
except (TypeError):
1648+
except TypeError as e:
16381649
raise ValueError("cannot construct a TimeDelta from the passed arguments, allowed keywords are "
16391650
"[days, seconds, microseconds, milliseconds, minutes, hours, weeks]")
16401651

0 commit comments

Comments
 (0)