Repository navigation
Fix pandas time series plotting at 1970 dates - #2
Open
robertoffmoura wants to merge 2 commits into
Open
robertoffmoura wants to merge 2 commits into
robertoffmoura wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:matplotlib.dates.epoch2num, which matplotlib has since removed. TheAttributeErrorwas 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 withepoch2num, hourly, monthly and yearly series came out wrong, because their ordinals aren't days.PeriodConverterinpandas.tseries.converter, a module pandas no longer has. The axis therefore fell through tonum2date. For a 15-minute series the ordinals are past year 9999, sompl_to_plotlyraised aValueError.Changes
mpltools.pandas_period_ordinals_to_datetimes, which converts ordinals withpd.Periodand the formatter's frequency. Fractional ordinals, such as bar edges, are interpolated within their period.mpl_dates_to_datestringsinstead of its class name, so the frequency can be read from it. Warn if the conversion fails instead of silently returning the numbers.PeriodConverterby class name, because pandas has moved it between private modules.