Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 541
Samizdis Avatar asked Apr 13 '18 12:04

Samizdis


People also ask

How to extract the month from a given date in pandas?

Pandas has many inbuilt methods that can be used to extract the month from a given date that are being generated randomly using the random function or by using Timestamp function or that are transformed to date format using the to_datetime function. Let’s see few examples for better understanding.

How do I get date and time values in pandas?

Let’s get started. The pandas library provides a DateTime object with nanosecond precision called Timestamp to work with date and time values. The Timestamp object derives from the NumPy’s datetime64 data type, making it more accurate and significantly faster than Python’s DateTime object.

Where can I learn more about time series data in pandas?

If you’d like to learn more about working with time-series data in pandas, you can check out the Time Series Analysis with Pandas tutorial on the Dataquest blog, and of course, the Pandas documentation on Time series / date functionality. Mehdi is a Senior Data Engineer and Team Lead at ADA.

How do I get the month from a date in Python?

Get Month from date in Pandas – Python. Pandas has many inbuilt methods that can be used to extract the month from a given date that are being generated randomly using the random function or by using Timestamp function or that are transformed to date format using the to_datetime function. Let’s see few examples for better understanding.


1 Answers

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)
like image 124
ImportanceOfBeingErnest Avatar answered Oct 20 '22 00:10

ImportanceOfBeingErnest