@@ -114,6 +114,7 @@ def timedelta_to_str(td: pd.Timedelta) -> str:
114
114
-------
115
115
str:
116
116
The tight string bounds of format '$d-$h$m$s.$ms'.
117
+ If the timedelta is negative, the string starts with 'NEG'.
117
118
118
119
"""
119
120
out_str = ""
@@ -153,19 +154,48 @@ def timedelta_to_str(td: pd.Timedelta) -> str:
153
154
def round_td_str (td : pd .Timedelta ) -> str :
154
155
"""Round a timedelta to the nearest unit and convert to a string.
155
156
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
+
156
168
.. seealso::
157
169
:func:`timedelta_to_str`
170
+
158
171
"""
159
172
for t_s in ("D" , "H" , "min" , "s" , "ms" , "us" , "ns" ):
160
173
if td > 0.95 * pd .Timedelta (f"1{ t_s } " ):
161
174
return timedelta_to_str (td .round (t_s ))
162
175
163
176
164
177
def 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 )
165
194
if number > 0.95 :
166
195
for unit , scaling in (("M" , int (1e6 )), ("k" , int (1e3 ))):
167
196
if number / scaling > 0.95 :
168
197
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