@@ -107,18 +107,24 @@ def to_timedelta(x):
107107 """
108108 Convert x to pandas.Timedelta if and only if it represents
109109 a fixed-length duration.
110+
111+ Notes
112+ -----
113+ Numeric values are rejected because they are unit-ambiguous.
110114 """
111115 if isinstance (x , (int , np .integer , float )):
112- return pd .Timedelta (x , unit = "ns" )
116+ raise TypeError (
117+ "Numeric values are ambiguous as time intervals; "
118+ "use a Timedelta, offset (hours(1)), or a string like '1h'."
119+ )
113120
114121 if isinstance (x , pd .Timedelta ):
115122 return x
116123
117- # FIRST: try Timedelta parsing (handles "1H", "1D" , etc.)
124+ # FIRST: try Timedelta parsing (handles '1h', '1D' , etc.)
118125 try :
119126 if isinstance (x , str ):
120- x = x .replace ("H" , "h" ).replace ("d" , "D" ).replace ("T" , "t" ) # standardize case
121- print (x )
127+ x = x .replace ("H" , "h" ).replace ("d" , "D" ).replace ("T" , "t" )
122128 return pd .Timedelta (x )
123129 except Exception :
124130 pass
@@ -138,8 +144,34 @@ def to_timedelta(x):
138144 return pd .Timedelta (off .nanos , unit = "ns" )
139145
140146
141-
142147def safe_divide_interval (a , b , * , tol = 1e-12 , require_int = True ):
148+ """
149+ Divide two intervals (or two scalars) safely.
150+
151+ - interval / interval -> float ratio (or int if require_int)
152+ - scalar / scalar -> numeric ratio
153+ - mixed scalar/interval -> TypeError
154+ """
155+ a_is_num = isinstance (a , (int , np .integer , float ))
156+ b_is_num = isinstance (b , (int , np .integer , float ))
157+
158+ if a_is_num and b_is_num :
159+ if b == 0 :
160+ raise ZeroDivisionError ("Division by zero" )
161+ r = a / b
162+ if require_int :
163+ r_int = int (round (r ))
164+ if abs (r - r_int ) > tol :
165+ raise ValueError (f"Scalars are not evenly divisible: { a !r} / { b !r} = { r } " )
166+ return r_int
167+ return float (r )
168+
169+ if a_is_num or b_is_num :
170+ raise TypeError (
171+ "safe_divide_interval does not support scalar/interval division; "
172+ "use explicit Timedelta scaling instead."
173+ )
174+
143175 td_a = to_timedelta (a )
144176 td_b = to_timedelta (b )
145177
@@ -157,4 +189,3 @@ def safe_divide_interval(a, b, *, tol=1e-12, require_int=True):
157189 return r_int
158190
159191 return float (ratio )
160-
0 commit comments