Skip to content

Fix pandas time series plotting at 1970 dates - #2

Open
robertoffmoura wants to merge 2 commits into
rm/port-plotly-testsfrom
rm/fix-pandas-dates
Open

robertoffmoura wants to merge 2 commits into
rm/port-plotly-testsfrom
rm/fix-pandas-dates

Conversation

@robertoffmoura

Copy link
Copy Markdown
Collaborator

pandas plots a regular time series at its period ordinals: periods of the series' frequency counted since 1970, with its own TimeSeries_DateFormatter. matplotlylib wasn't converting these, so:

  • x values and axis range stayed numbers. The conversion used matplotlib.dates.epoch2num, which matplotlib has since removed. The AttributeError was caught and the numbers went to plotly as they were. Plotly reads them as milliseconds since 1970, so a daily series from 2001 showed up in the first seconds of 1970. Even with epoch2num, hourly, monthly and yearly series came out wrong, because their ordinals aren't days.
  • The axis domain was wrong, and 15-minute series crashed. mplexporter looked for pandas' PeriodConverter in pandas.tseries.converter, a module pandas no longer has. The axis therefore fell through to num2date. For a 15-minute series the ordinals are past year 9999, so mpl_to_plotly raised a ValueError.

Changes

  • Add mpltools.pandas_period_ordinals_to_datetimes, which converts ordinals with pd.Period and the formatter's frequency. Fractional ordinals, such as bar edges, are interpolated within their period.
  • Pass the formatter object to mpl_dates_to_datestrings instead of its class name, so the frequency can be read from it. Warn if the conversion fails instead of silently returning the numbers.
  • In mplexporter, recognise PeriodConverter by class name, because pandas has moved it between private modules.

pandas plots a regular time series at the ordinals of its periods,
counted in the series' frequency since 1970, and formats the axis with
its TimeSeries_DateFormatter. mpl_dates_to_datestrings treated these
values as days since 1970 and converted them with
matplotlib.dates.epoch2num, which matplotlib has since removed. The
resulting AttributeError was caught and the values returned unchanged,
so the x values and axis range stayed numbers on a date axis, which
plotly reads as milliseconds since 1970: a daily series from 2001
ended up in the first seconds of 1970.

Convert the ordinals with pandas' Period and the formatter's frequency
instead, interpolating fractional ordinals such as bar edges within
their period. This also fixes hourly, monthly and yearly series, which
were wrong even with epoch2num because their ordinals are not days. To
reach the frequency, pass the formatter itself rather than its class
name to mpl_dates_to_datestrings. Warn if the conversion fails rather
than returning the values silently.

Remove the xfail mark from test_pandas_time_series_date_formatter and
add tests for other frequencies and for fractional ordinals.
mplexporter recognised pandas' PeriodConverter by importing it from
pandas.tseries.converter, a module pandas no longer has. The
ImportError was caught, so pandas axes fell through to num2date, which
read the period ordinals as days since 1970. For an hourly series that
put the domain in the year 2714, and for a 15-minute series the ordinals
are past year 9999, so mpl_to_plotly raised a ValueError.

Recognise the converter by its class name, as pandas has moved it
between private modules, and add a 15-minute series to the frequency
tests.
@robertoffmoura
robertoffmoura added this pull request to stack #3 December 4, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant