6161 from matplotlib .axis import Axis
6262
6363 from pandas ._libs .tslibs .offsets import BaseOffset
64+ from pandas ._typing import TimeUnit
6465
6566
6667_mpl_units : dict = {} # Cache for units overwritten by us
@@ -1099,18 +1100,22 @@ class TimeSeries_TimedeltaFormatter(mpl.ticker.Formatter): # pyright: ignore[re
10991100 Formats the ticks along an axis controlled by a :class:`TimedeltaIndex`.
11001101 """
11011102
1103+ def __init__ (self , unit : TimeUnit = "ns" ):
1104+ self .unit = unit
1105+ super ().__init__ ()
1106+
11021107 axis : Axis
11031108
11041109 @staticmethod
1105- def format_timedelta_ticks (x , pos , n_decimals : int ) -> str :
1110+ def format_timedelta_ticks (x , pos , n_decimals : int , exp : int ) -> str :
11061111 """
11071112 Convert seconds to 'D days HH:MM:SS.F'
11081113 """
1109- s , ns = divmod (x , 10 ** 9 ) # TODO(non-nano): this looks like it assumes ns
1114+ s , ns = divmod (x , 10 ** exp )
11101115 m , s = divmod (s , 60 )
11111116 h , m = divmod (m , 60 )
11121117 d , h = divmod (h , 24 )
1113- decimals = int (ns * 10 ** (n_decimals - 9 ))
1118+ decimals = int (ns * 10 ** (n_decimals - exp ))
11141119 s = f"{ int (h ):02d} :{ int (m ):02d} :{ int (s ):02d} "
11151120 if n_decimals > 0 :
11161121 s += f".{ decimals :0{n_decimals }d} "
@@ -1119,6 +1124,7 @@ def format_timedelta_ticks(x, pos, n_decimals: int) -> str:
11191124 return s
11201125
11211126 def __call__ (self , x , pos : int | None = 0 ) -> str :
1127+ exp = {"ns" : 9 , "us" : 6 , "ms" : 3 , "s" : 0 }[self .unit ]
11221128 (vmin , vmax ) = tuple (self .axis .get_view_interval ())
1123- n_decimals = min (int (np .ceil (np .log10 (100 * 10 ** 9 / abs (vmax - vmin )))), 9 )
1124- return self .format_timedelta_ticks (x , pos , n_decimals )
1129+ n_decimals = min (int (np .ceil (np .log10 (100 * 10 ** exp / abs (vmax - vmin )))), exp )
1130+ return self .format_timedelta_ticks (x , pos , n_decimals , exp )
0 commit comments