Samizdis
Samizdis

Reputation: 1661

Pandas dates being wrongly plotted at start of month

I have a timeseries in Pandas where the dates are at the end of the month:

import pandas as pd
s = pd.Series({
    '2018-04-30':     0,
    '2018-05-31':     1,
    '2018-06-30':     0,
    '2018-07-31':    1,
    '2018-08-31':    0,
    '2018-09-30':    1,
})
s.index = pd.to_datetime(s.index)

When I plot this with matplotlib I get the result I'd expect, with points at the end-of-month and the line starting just before May 2018:

import matplotlib.pyplot as plt
plt.plot(s)

Matplotlib graph

But Pandas' native plot function plots the points at the start of the month:

s.plot()

Pandas graph

I thought perhaps this was just Pandas labelling '30 April' as 'April', but that doesn't seem to be the case:

s2 = pd.Series([0.2, 0.7], index=pd.date_range('2018-05-01', '2018-05-02'))
s.plot()
s2.plot()

Pandas plot with first-of-month also plotted

Is this a bug in Pandas or am I doing something wrong here?

Upvotes: 9

Views: 271

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

This seems to be a bug in pandas. To get the same as with matplotlib you may always use x_compat=True. This then would also allow to use matplotlib.dates formatters and locators.

s = pandas.Series(...)
s.plot(x_compat=True)

Upvotes: 2

Related Questions