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)
But Pandas' native plot function plots the points at the start of the month:
s.plot()
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()
Is this a bug in Pandas or am I doing something wrong here?
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With