@@ -114,6 +114,7 @@ def timedelta_to_str(td: pd.Timedelta) -> str:
114114 -------
115115 str:
116116 The tight string bounds of format '$d-$h$m$s.$ms'.
117+ If the timedelta is negative, the string starts with 'NEG'.
117118
118119 """
119120 out_str = ""
@@ -153,19 +154,48 @@ def timedelta_to_str(td: pd.Timedelta) -> str:
153154def round_td_str (td : pd .Timedelta ) -> str :
154155 """Round a timedelta to the nearest unit and convert to a string.
155156
157+ Parameters
158+ ----------
159+ td : pd.Timedelta
160+ The timedelta to round.
161+
162+ Returns
163+ -------
164+ str
165+ The rounded timedelta as a string.
166+ If the timedelta is == 0, None is returned.
167+
156168 .. seealso::
157169 :func:`timedelta_to_str`
170+
158171 """
159172 for t_s in ("D" , "H" , "min" , "s" , "ms" , "us" , "ns" ):
160173 if td > 0.95 * pd .Timedelta (f"1{ t_s } " ):
161174 return timedelta_to_str (td .round (t_s ))
162175
163176
164177def round_number_str (number : float ) -> str :
178+ """Round a number to the nearest unit and convert to a string.
179+
180+ Parameters
181+ ----------
182+ number : float
183+ The number to round.
184+
185+ Returns
186+ -------
187+ str
188+ The rounded number as a string.
189+ If the number is == 0, None is returned.
190+
191+ """
192+ sign = "-" if number < 0 else ""
193+ number = abs (number )
165194 if number > 0.95 :
166195 for unit , scaling in (("M" , int (1e6 )), ("k" , int (1e3 ))):
167196 if number / scaling > 0.95 :
168197 return f"{ round (number / scaling )} { unit } "
169- return str (round (number ))
170- # we have a number < 1 --> round till nearest non-zero digit
171- return str (round (number , 1 + abs (int (math .log10 (number )))))
198+ return sign + str (round (number ))
199+ if number > 0 : # avoid log10(0)
200+ # we have a number between 0-0.95 -> round till nearest non-zero digit
201+ return sign + str (round (number , 1 + abs (int (math .log10 (number )))))
0 commit comments