Skip to content

TimeFormatter broken for sub-second resolution #18478

Closed
@nmartensen

Description

@nmartensen

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:

fmt += '.%6f'

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).

fmt += '.%3f'

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions