@@ -1537,12 +1537,7 @@ cdef class _Timedelta(timedelta):
1537
1537
def _as_unit (self , str unit , bint round_ok = True ):
1538
1538
dtype = np.dtype(f" m8[{unit}]" )
1539
1539
reso = get_unit_from_dtype(dtype)
1540
- try :
1541
- return self ._as_reso(reso, round_ok = round_ok)
1542
- except OverflowError as err:
1543
- raise OutOfBoundsTimedelta(
1544
- f" Cannot cast {self} to unit='{unit}' without overflow."
1545
- ) from err
1540
+ return self ._as_reso(reso, round_ok = round_ok)
1546
1541
1547
1542
@ cython.cdivision (False )
1548
1543
cdef _Timedelta _as_reso(self , NPY_DATETIMEUNIT reso, bint round_ok = True ):
@@ -1552,9 +1547,26 @@ cdef class _Timedelta(timedelta):
1552
1547
if reso == self ._reso:
1553
1548
return self
1554
1549
1555
- value = convert_reso(self .value, self ._reso, reso, round_ok = round_ok)
1550
+ try :
1551
+ value = convert_reso(self .value, self ._reso, reso, round_ok = round_ok)
1552
+ except OverflowError as err:
1553
+ unit = npy_unit_to_abbrev(reso)
1554
+ raise OutOfBoundsTimedelta(
1555
+ f" Cannot cast {self} to unit='{unit}' without overflow."
1556
+ ) from err
1557
+
1556
1558
return type (self )._from_value_and_reso(value, reso = reso)
1557
1559
1560
+ cpdef _maybe_cast_to_matching_resos(self , _Timedelta other):
1561
+ """
1562
+ If _resos do not match, cast to the higher resolution, raising on overflow.
1563
+ """
1564
+ if self ._reso > other._reso:
1565
+ other = other._as_reso(self ._reso)
1566
+ elif self ._reso < other._reso:
1567
+ self = self ._as_reso(other._reso)
1568
+ return self , other
1569
+
1558
1570
1559
1571
# Python front end to C extension type _Timedelta
1560
1572
# This serves as the box for timedelta64
@@ -1834,11 +1846,7 @@ class Timedelta(_Timedelta):
1834
1846
if other is NaT:
1835
1847
return np.nan
1836
1848
if other._reso != self ._reso:
1837
- raise ValueError (
1838
- " division between Timedeltas with mismatched resolutions "
1839
- " are not supported. Explicitly cast to matching resolutions "
1840
- " before dividing."
1841
- )
1849
+ self , other = self ._maybe_cast_to_matching_resos(other)
1842
1850
return self .value / float (other.value)
1843
1851
1844
1852
elif is_integer_object(other) or is_float_object(other):
@@ -1865,11 +1873,7 @@ class Timedelta(_Timedelta):
1865
1873
if other is NaT:
1866
1874
return np.nan
1867
1875
if self ._reso != other._reso:
1868
- raise ValueError (
1869
- " division between Timedeltas with mismatched resolutions "
1870
- " are not supported. Explicitly cast to matching resolutions "
1871
- " before dividing."
1872
- )
1876
+ self , other = self ._maybe_cast_to_matching_resos(other)
1873
1877
return float (other.value) / self .value
1874
1878
1875
1879
elif is_array(other):
@@ -1897,11 +1901,7 @@ class Timedelta(_Timedelta):
1897
1901
if other is NaT:
1898
1902
return np.nan
1899
1903
if self ._reso != other._reso:
1900
- raise ValueError (
1901
- " floordivision between Timedeltas with mismatched resolutions "
1902
- " are not supported. Explicitly cast to matching resolutions "
1903
- " before dividing."
1904
- )
1904
+ self , other = self ._maybe_cast_to_matching_resos(other)
1905
1905
return self .value // other.value
1906
1906
1907
1907
elif is_integer_object(other) or is_float_object(other):
@@ -1920,6 +1920,7 @@ class Timedelta(_Timedelta):
1920
1920
if self ._reso != NPY_FR_ns:
1921
1921
raise NotImplementedError
1922
1922
return _broadcast_floordiv_td64(self .value, other, _floordiv)
1923
+
1923
1924
elif other.dtype.kind in [' i' , ' u' , ' f' ]:
1924
1925
if other.ndim == 0 :
1925
1926
return self // other.item()
@@ -1939,11 +1940,7 @@ class Timedelta(_Timedelta):
1939
1940
if other is NaT:
1940
1941
return np.nan
1941
1942
if self ._reso != other._reso:
1942
- raise ValueError (
1943
- " floordivision between Timedeltas with mismatched resolutions "
1944
- " are not supported. Explicitly cast to matching resolutions "
1945
- " before dividing."
1946
- )
1943
+ self , other = self ._maybe_cast_to_matching_resos(other)
1947
1944
return other.value // self .value
1948
1945
1949
1946
elif is_array(other):
@@ -2029,7 +2026,7 @@ cdef _broadcast_floordiv_td64(
2029
2026
Parameters
2030
2027
----------
2031
2028
value : int64_t; `self.value` from a Timedelta object
2032
- other : object
2029
+ other : ndarray[timedelta64[ns]]
2033
2030
operation : function, either _floordiv or _rfloordiv
2034
2031
2035
2032
Returns
0 commit comments