TimeFormatter broken for sub-second resolution · Issue #18478 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

%matplotlib inline import pandas import datetime as pydt

pandas.DataFrame({'A': 1}, index=[pydt.time(1, 1, 1, 1), pydt.time(1, 1, 1, 100)]).plot()

Problem description

The TimeFormatter code has several issues. The first is here:

us = int((x - s) * 1e6 - ms)

ms needs to be multiplied by 1e3, otherwise we'd be subtracting milliseconds from microseconds.

The next problem is here:

The strftime format does not support the %6f notation. This needs to be %f, otherwise the result would be either a literal '%6f' in the output or a ValueError (invalid format string).

Same problems, plus: '%f' means microseconds for strftime(), %3f cannot turn this into milliseconds.

And the last one:

return pydt.time(h, m, s, us).strftime(fmt)

This timestamp is missing the milliseconds entirely. (Assuming the fix to the microsecond calculation mentioned above.)

Expected Output

Correctly formatted time stamp strings as tick labels.

Solution Approaches

  1. Drop the TimeFormatter entirely (after checking if MPL already copes with pydt.time())
  2. Drop any sub-second resolution support from the TimeFormatter, as it was broken for a long time and nobody complained.
  3. Make it work correctly in the way it was most likely intended to work.

What do you think? I can set up a pull request for option 3 if that's the preferred solution.