@@ -1619,6 +1619,7 @@ class Timedelta(_Timedelta):
1619
1619
Denote the unit of the input, if input is an integer. Default 'ns'.
1620
1620
days, seconds, microseconds, milliseconds, minutes, hours, weeks : numeric, optional
1621
1621
Values for construction in compat with datetime.timedelta.
1622
+ np ints and floats will be coereced to python ints and floats.
1622
1623
1623
1624
Notes
1624
1625
-----
@@ -1632,11 +1633,23 @@ class Timedelta(_Timedelta):
1632
1633
if value is None :
1633
1634
if not len (kwargs):
1634
1635
raise ValueError (" cannot construct a TimeDelta without a value/unit or descriptive keywords (days,seconds....)" )
1636
+ # sanitize timedelta input.
1637
+ # needed to avoid np to python-native int/float typecasting issues
1638
+ for argname, argvalue in kwargs.items():
1639
+ if isinstance (argvalue, (int , float )):
1640
+ continue
1641
+ elif np.issubdtype(argvalue, int ):
1642
+ kwargs[argname] = int (argvalue)
1643
+ elif np.issubdtype(argvalue, float ):
1644
+ kwargs[argname] = float (argvalue)
1635
1645
try :
1636
1646
value = timedelta(** kwargs)
1637
- except (TypeError ):
1638
- raise ValueError (" cannot construct a TimeDelta from the passed arguments, allowed keywords are "
1639
- " [days, seconds, microseconds, milliseconds, minutes, hours, weeks]" )
1647
+ except TypeError as e:
1648
+ if ' unsupported type' in str (e):
1649
+ raise e
1650
+ else :
1651
+ raise ValueError (" cannot construct a TimeDelta from the passed arguments, allowed keywords are "
1652
+ " [days, seconds, microseconds, milliseconds, minutes, hours, weeks]" )
1640
1653
1641
1654
if isinstance (value, Timedelta):
1642
1655
value = value.value
0 commit comments