Skip to content

Commit ed2d0a6

Browse files
Clean timedelta format and add test
1 parent 8bcb313 commit ed2d0a6

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

pandas/plotting/_matplotlib/timeseries.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,23 +304,6 @@ def _maybe_convert_index(ax, data):
304304
# Do we need the rest for convenience?
305305

306306

307-
def format_timedelta_ticks(x, pos, n_decimals):
308-
"""
309-
Convert seconds to 'D days HH:MM:SS.F'
310-
"""
311-
s, ns = divmod(x, 1e9)
312-
m, s = divmod(s, 60)
313-
h, m = divmod(m, 60)
314-
d, h = divmod(h, 24)
315-
decimals = int(ns * 10 ** (n_decimals - 9))
316-
s = r"{:02d}:{:02d}:{:02d}".format(int(h), int(m), int(s))
317-
if n_decimals > 0:
318-
s += ".{{:0{:0d}d}}".format(n_decimals).format(decimals)
319-
if d != 0:
320-
s = "{:d} days ".format(int(d)) + s
321-
return s
322-
323-
324307
def _format_coord(freq, t, y):
325308
return "t = {0} y = {1:8f}".format(Period(ordinal=int(t), freq=freq), y)
326309

pandas/tests/plotting/test_converter.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,22 @@ def test_convert_nested(self):
373373
r1 = self.pc.convert([data, data], None, self.axis)
374374
r2 = [self.pc.convert(data, None, self.axis) for _ in range(2)]
375375
assert r1 == r2
376+
377+
378+
class TestTimeDeltaConverter:
379+
def setup_method(self, method):
380+
self.tdc = converter.TimeSeries_TimedeltaFormatter
381+
382+
@pytest.mark.parametrize(
383+
"x, decimal, format_expected",
384+
[
385+
(0.0, 0, "00:00:00"),
386+
(3972320000000, 1, "01:06:12.3"),
387+
(713233432000000, 2, "8 days 06:07:13.43"),
388+
(32423432000000, 4, "09:00:23.4320"),
389+
],
390+
)
391+
def test_format_timedelta_ticks(self, x, decimal, format_expected):
392+
393+
result = self.tdc.format_timedelta_ticks(x, pos=None, n_decimals=decimal)
394+
assert result == format_expected

0 commit comments

Comments
 (0)