@@ -49,6 +49,7 @@ from pandas._libs.tslibs.np_datetime cimport (
49
49
cmp_scalar,
50
50
get_datetime64_unit,
51
51
get_timedelta64_value,
52
+ get_unit_from_dtype,
52
53
npy_datetimestruct,
53
54
pandas_datetime_to_datetimestruct,
54
55
pandas_timedelta_to_timedeltastruct,
@@ -141,14 +142,14 @@ _no_input = object()
141
142
142
143
@ cython.boundscheck (False )
143
144
@ cython.wraparound (False )
144
- def ints_to_pytimedelta (const int64_t[:] arr , box = False ):
145
+ def ints_to_pytimedelta (ndarray m8values , box = False ):
145
146
"""
146
147
convert an i8 repr to an ndarray of timedelta or Timedelta (if box ==
147
148
True)
148
149
149
150
Parameters
150
151
----------
151
- arr : ndarray[int64_t ]
152
+ arr : ndarray[timedelta64 ]
152
153
box : bool, default False
153
154
154
155
Returns
@@ -157,9 +158,12 @@ def ints_to_pytimedelta(const int64_t[:] arr, box=False):
157
158
array of Timedelta or timedeltas objects
158
159
"""
159
160
cdef:
160
- Py_ssize_t i, n = len (arr)
161
+ Py_ssize_t i, n = m8values.size
161
162
int64_t value
162
163
object [::1 ] result = np.empty(n, dtype = object )
164
+ NPY_DATETIMEUNIT reso = get_unit_from_dtype(m8values.dtype)
165
+
166
+ arr = m8values.view(" i8" )
163
167
164
168
for i in range (n):
165
169
@@ -168,9 +172,26 @@ def ints_to_pytimedelta(const int64_t[:] arr, box=False):
168
172
result[i] = < object > NaT
169
173
else :
170
174
if box:
171
- result[i] = Timedelta (value)
172
- else :
175
+ result[i] = _timedelta_from_value_and_reso (value, reso = reso )
176
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_ns :
173
177
result[i] = timedelta(microseconds = int (value) / 1000 )
178
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_us:
179
+ result[i] = timedelta(microseconds = value)
180
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_ms:
181
+ result[i] = timedelta(milliseconds = value)
182
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_s:
183
+ result[i] = timedelta(seconds = value)
184
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_m:
185
+ result[i] = timedelta(minutes = value)
186
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_h:
187
+ result[i] = timedelta(hours = value)
188
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_D:
189
+ result[i] = timedelta(days = value)
190
+ elif reso == NPY_DATETIMEUNIT.NPY_FR_W:
191
+ result[i] = timedelta(weeks = value)
192
+ else :
193
+ # Month, Year, NPY_FR_GENERIC, pico, fempto, atto
194
+ raise NotImplementedError (reso)
174
195
175
196
return result.base # .base to access underlying np.ndarray
176
197
@@ -1530,6 +1551,9 @@ class Timedelta(_Timedelta):
1530
1551
int64_t result, unit, remainder
1531
1552
ndarray[int64_t] arr
1532
1553
1554
+ if self ._reso != NPY_FR_ns:
1555
+ raise NotImplementedError
1556
+
1533
1557
from pandas._libs.tslibs.offsets import to_offset
1534
1558
unit = to_offset(freq).nanos
1535
1559
@@ -1620,6 +1644,8 @@ class Timedelta(_Timedelta):
1620
1644
1621
1645
elif is_integer_object(other) or is_float_object(other):
1622
1646
# integers or floats
1647
+ if self ._reso != NPY_FR_ns:
1648
+ raise NotImplementedError
1623
1649
return Timedelta(self .value / other, unit = ' ns' )
1624
1650
1625
1651
elif is_array(other):
@@ -1633,6 +1659,8 @@ class Timedelta(_Timedelta):
1633
1659
other = Timedelta(other)
1634
1660
if other is NaT:
1635
1661
return np.nan
1662
+ if self ._reso != NPY_FR_ns:
1663
+ raise NotImplementedError
1636
1664
return float (other.value) / self .value
1637
1665
1638
1666
elif is_array(other):
@@ -1651,17 +1679,25 @@ class Timedelta(_Timedelta):
1651
1679
other = Timedelta(other)
1652
1680
if other is NaT:
1653
1681
return np.nan
1682
+ if self ._reso != NPY_FR_ns:
1683
+ raise NotImplementedError
1654
1684
return self .value // other.value
1655
1685
1656
1686
elif is_integer_object(other) or is_float_object(other):
1687
+ if self ._reso != NPY_FR_ns:
1688
+ raise NotImplementedError
1657
1689
return Timedelta(self .value // other, unit = ' ns' )
1658
1690
1659
1691
elif is_array(other):
1660
1692
if other.dtype.kind == ' m' :
1661
1693
# also timedelta-like
1694
+ if self ._reso != NPY_FR_ns:
1695
+ raise NotImplementedError
1662
1696
return _broadcast_floordiv_td64(self .value, other, _floordiv)
1663
1697
elif other.dtype.kind in [' i' , ' u' , ' f' ]:
1664
1698
if other.ndim == 0 :
1699
+ if self ._reso != NPY_FR_ns:
1700
+ raise NotImplementedError
1665
1701
return Timedelta(self .value // other)
1666
1702
else :
1667
1703
return self .to_timedelta64() // other
@@ -1678,11 +1714,15 @@ class Timedelta(_Timedelta):
1678
1714
other = Timedelta(other)
1679
1715
if other is NaT:
1680
1716
return np.nan
1717
+ if self ._reso != NPY_FR_ns:
1718
+ raise NotImplementedError
1681
1719
return other.value // self .value
1682
1720
1683
1721
elif is_array(other):
1684
1722
if other.dtype.kind == ' m' :
1685
1723
# also timedelta-like
1724
+ if self ._reso != NPY_FR_ns:
1725
+ raise NotImplementedError
1686
1726
return _broadcast_floordiv_td64(self .value, other, _rfloordiv)
1687
1727
1688
1728
# Includes integer array // Timedelta, disallowed in GH#19761
0 commit comments