Skip to content

Commit 7db61ec

Browse files
committed
Create TimeSeries_TimedeltaFormatter.
1 parent 232efe6 commit 7db61ec

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

pandas/tseries/converter.py

+30
Original file line numberDiff line numberDiff line change
@@ -1000,3 +1000,33 @@ def __call__(self, x, pos=0):
10001000
else:
10011001
fmt = self.formatdict.pop(x, '')
10021002
return Period(ordinal=int(x), freq=self.freq).strftime(fmt)
1003+
1004+
1005+
class TimeSeries_TimedeltaFormatter(Formatter):
1006+
"""
1007+
Formats the ticks along an axis controlled by a :class:`TimedeltaIndex`.
1008+
"""
1009+
1010+
@staticmethod
1011+
def format_timedelta_ticks(x, pos, n_decimals):
1012+
"""
1013+
Convert seconds to 'D days HH:MM:SS.F'
1014+
"""
1015+
s, ns = divmod(x, 1e9)
1016+
m, s = divmod(s, 60)
1017+
h, m = divmod(m, 60)
1018+
d, h = divmod(h, 24)
1019+
decimals = int(ns * 10**(n_decimals - 9))
1020+
s = r'{:02d}:{:02d}:{:02d}'.format(int(h), int(m), int(s))
1021+
if n_decimals > 0:
1022+
s += '.{{:0{:0d}d}}'.format(n_decimals).format(decimals)
1023+
if d != 0:
1024+
s = '{:d} days '.format(int(d)) + s
1025+
return s
1026+
1027+
def __call__(self, x, pos=0):
1028+
(vmin, vmax) = tuple(self.axis.get_view_interval())
1029+
n_decimals = int(np.ceil(np.log10(100 * 1e9 / (vmax - vmin))))
1030+
if n_decimals > 9:
1031+
n_decimals = 9
1032+
return self.format_timedelta_ticks(x, pos, n_decimals)

pandas/tseries/plotting.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import pandas.compat as compat
1919

2020
from pandas.tseries.converter import (TimeSeries_DateLocator,
21-
TimeSeries_DateFormatter)
21+
TimeSeries_DateFormatter,
22+
TimeSeries_TimedeltaFormatter)
2223

2324
# ---------------------------------------------------------------------
2425
# Plotting functions and monkey patches
@@ -335,14 +336,8 @@ def format_dateaxis(subplot, freq, index):
335336
"t = {0} y = {1:8f}".format(Period(ordinal=int(t), freq=freq), y))
336337

337338
elif isinstance(index, TimedeltaIndex):
338-
from matplotlib import ticker
339-
(vmin, vmax) = tuple(subplot.xaxis.get_view_interval())
340-
n_decimals = int(np.ceil(np.log10(100 * 1e9 / (vmax - vmin))))
341-
if n_decimals > 9:
342-
n_decimals = 9
343-
formatter = ticker.FuncFormatter(
344-
lambda x, pos: format_timedelta_ticks(x, pos, n_decimals))
345-
subplot.xaxis.set_major_formatter(formatter)
339+
subplot.xaxis.set_major_formatter(
340+
TimeSeries_TimedeltaFormatter())
346341
else:
347342
raise TypeError('index type not supported')
348343

0 commit comments

Comments
 (0)