@@ -1537,12 +1537,7 @@ cdef class _Timedelta(timedelta):
15371537 def _as_unit (self , str unit , bint round_ok = True ):
15381538 dtype = np.dtype(f" m8[{unit}]" )
15391539 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)
15461541
15471542 @ cython.cdivision (False )
15481543 cdef _Timedelta _as_reso(self , NPY_DATETIMEUNIT reso, bint round_ok = True ):
@@ -1552,9 +1547,26 @@ cdef class _Timedelta(timedelta):
15521547 if reso == self ._reso:
15531548 return self
15541549
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+
15561558 return type (self )._from_value_and_reso(value, reso = reso)
15571559
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+
15581570
15591571# Python front end to C extension type _Timedelta
15601572# This serves as the box for timedelta64
@@ -1834,11 +1846,7 @@ class Timedelta(_Timedelta):
18341846 if other is NaT:
18351847 return np.nan
18361848 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)
18421850 return self .value / float (other.value)
18431851
18441852 elif is_integer_object(other) or is_float_object(other):
@@ -1865,11 +1873,7 @@ class Timedelta(_Timedelta):
18651873 if other is NaT:
18661874 return np.nan
18671875 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)
18731877 return float (other.value) / self .value
18741878
18751879 elif is_array(other):
@@ -1897,11 +1901,7 @@ class Timedelta(_Timedelta):
18971901 if other is NaT:
18981902 return np.nan
18991903 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)
19051905 return self .value // other.value
19061906
19071907 elif is_integer_object(other) or is_float_object(other):
@@ -1920,6 +1920,7 @@ class Timedelta(_Timedelta):
19201920 if self ._reso != NPY_FR_ns:
19211921 raise NotImplementedError
19221922 return _broadcast_floordiv_td64(self .value, other, _floordiv)
1923+
19231924 elif other.dtype.kind in [' i' , ' u' , ' f' ]:
19241925 if other.ndim == 0 :
19251926 return self // other.item()
@@ -1939,11 +1940,7 @@ class Timedelta(_Timedelta):
19391940 if other is NaT:
19401941 return np.nan
19411942 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)
19471944 return other.value // self .value
19481945
19491946 elif is_array(other):
@@ -2029,7 +2026,7 @@ cdef _broadcast_floordiv_td64(
20292026 Parameters
20302027 ----------
20312028 value : int64_t; `self.value` from a Timedelta object
2032- other : object
2029+ other : ndarray[timedelta64[ns]]
20332030 operation : function, either _floordiv or _rfloordiv
20342031
20352032 Returns
0 commit comments