@@ -827,6 +827,14 @@ def _binary_op_method_timedeltalike(op, name):
827
827
# ----------------------------------------------------------------------
828
828
# Timedelta Construction
829
829
830
+ cpdef disallow_ambiguous_unit(unit):
831
+ if unit in {" Y" , " y" , " M" }:
832
+ raise ValueError (
833
+ " Units 'M', 'Y', and 'y' are no longer supported, as they do not "
834
+ " represent unambiguous timedelta values durations."
835
+ )
836
+
837
+
830
838
cdef int64_t parse_iso_format_string(str ts) except ? - 1 :
831
839
"""
832
840
Extracts and cleanses the appropriate values from a match object with
@@ -1815,11 +1823,7 @@ class Timedelta(_Timedelta):
1815
1823
)
1816
1824
raise OutOfBoundsTimedelta(msg) from err
1817
1825
1818
- if unit in {" Y" , " y" , " M" }:
1819
- raise ValueError (
1820
- " Units 'M', 'Y', and 'y' are no longer supported, as they do not "
1821
- " represent unambiguous timedelta values durations."
1822
- )
1826
+ disallow_ambiguous_unit(unit)
1823
1827
1824
1828
# GH 30543 if pd.Timedelta already passed, return it
1825
1829
# check that only value is passed
@@ -1932,10 +1936,7 @@ class Timedelta(_Timedelta):
1932
1936
int64_t result, unit
1933
1937
ndarray[int64_t] arr
1934
1938
1935
- from pandas._libs.tslibs.offsets import to_offset
1936
-
1937
- to_offset(freq).nanos # raises on non-fixed freq
1938
- unit = delta_to_nanoseconds(to_offset(freq), self ._creso)
1939
+ unit = get_unit_for_round(freq, self ._creso)
1939
1940
1940
1941
arr = np.array([self ._value], dtype = " i8" )
1941
1942
try :
@@ -2292,3 +2293,11 @@ cdef bint _should_cast_to_timedelta(object obj):
2292
2293
return (
2293
2294
is_any_td_scalar(obj) or obj is None or obj is NaT or isinstance (obj, str )
2294
2295
)
2296
+
2297
+
2298
+ cpdef int64_t get_unit_for_round(freq, NPY_DATETIMEUNIT creso) except ? - 1 :
2299
+ from pandas._libs.tslibs.offsets import to_offset
2300
+
2301
+ freq = to_offset(freq)
2302
+ freq.nanos # raises on non-fixed freq
2303
+ return delta_to_nanoseconds(freq, creso)
0 commit comments