Can pandas plot a time-series without trying to convert the index to Periods?

2024/10/1 5:31:11

When plotting a time-series, I observe an unusual behavior, which eventually results in not being able to format the xticks of the plot. It seems that pandas internally tries to convert the index into a PeriodIndex, but obviously only succeeds if the timestamp values are equally spaced. If they are unevenly spaced (or - strangely - if they are evenly spaced but timezone-aware) the index remains a DatetimeIndex. The latter case works as expected. I can set DateFormatter and Locators. If however the index is interally converted to a PeriodIndex before plotting, the x-axis of the resulting plott seems to be messed up.

Here is an Example to reproduce the problem.

from pandas import Series, DataFrame
import pandas as pd
from datetime import datetime
import pytz
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as npidx1 = np.array([datetime(2014, 1, 16, 0),datetime(2014, 1, 16, 5),datetime(2014, 1, 16, 10),datetime(2014, 1, 16, 15), datetime(2014, 1, 16, 20), datetime(2014, 1, 17, 1)])
idx2 = np.array([datetime(2014, 1, 16, 0),datetime(2014, 1, 16, 5),datetime(2014, 1, 16, 10),datetime(2014, 1, 16, 15),datetime(2014, 1, 16, 20),datetime(2014, 1, 16, 23)])
y = [0, 2, np.nan, 5, 2, 1]
tz = pytz.timezone('Europe/Berlin')fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(15,4))# index convertible to period index
s1 = Series(y, index=idx1)
s1.plot(ax=ax1)
print ax1.get_xticks()
print ax1.xaxis.get_major_locator()
print ax1.xaxis.get_major_formatter()
#ax1.xaxis.set_major_formatter(mpl.dates.DateFormatter('%H'))
#ax1.xaxis.set_major_locator(mpl.ticker.MultipleLocator(0.25))# index not convertible to period index
s2 = Series(y, index=idx2)
s2.plot(ax=ax2)
print ax2.get_xticks()
#ax2.xaxis.set_major_formatter(mpl.dates.DateFormatter('%H'))
#ax2.xaxis.set_major_locator(mpl.ticker.MultipleLocator(0.25))# index convertible to period index but tz-aware
s3 = Series(y, index=idx1)
s3 = s3.tz_localize(tz)
s3.plot(ax=ax3)
print ax3.get_xticks()
#ax2.xaxis.set_major_formatter(mpl.dates.DateFormatter('%H'))
#ax2.xaxis.set_major_locator(mpl.ticker.MultipleLocator(0.25))fig.autofmt_xdate()  # just temporarilyplt.tight_layout()
plt.show(block=False)

Is there a way to tell pandas to keep the index in its original format and not to convert it to Periods? Any ideas how to deal with this are greatly appreciated!

I use pandas 0.13 and matplotlib 1.3.1

As a sidenote:
It would of course be great if the timezones were not converted all to UTC. However I realize this problem may still persist for a while. But if anyone has a hint for a workaround I'd be glad to hear (I tried passing a tz directly to the DateFormatter. That works, but the Locators don't seem to like it much).

Answer

One way around this is not to use the pandas plot method, but to directly the matplotlib's plot function. s1.plot(ax=ax1) would then become:

ax1.plot(s1.index, s1)

If you then print the ax1.get_xticks() you get the same as with the irregular time series, as the datetime values are not converted to Periods. One disadvantage of this is that you loose the smarter date axis formatting of pandas (but as you want to adapt this, not a problem I suppose).

As far as I know you cannot specify this in the pandas public api (apart form ugly hacks as deliberately making your time series irregular or adding a time zone)

https://en.xdnf.cn/q/70997.html

Related Q&A

pip install syntax for allowing insecure

I tried to run$pip install --upgrade --allow-insecure setuptoolsbut it doesnt seem to work? is my syntax wrong?this is on ubuntu 13.10 I need --allow-insecure as I havent been able to the get the co…

how do I determine the locations of the points after perspective transform, in the new image plane?

Im using OpenCV+Python+Numpy and I have three points in the image, I know the exact locations of those points.(P1, P2);N1I am going to transform the image to another view, (for example I am transformin…

How to do a simple Gaussian mixture sampling and PDF plotting with NumPy/SciPy?

I add three normal distributions to obtain a new distribution as shown below, how can I do sampling according to this distribution in python?import matplotlib.pyplot as plt import scipy.stats as ss im…

Python dict.get() or None scenario [duplicate]

This question already has answers here:Truth value of a string in python(4 answers)Closed 7 years ago.I am attempting to access a dictionarys values based on a list of keys I have. If the key is not pr…

p-values from ridge regression in python

Im using ridge regression (ridgeCV). And Ive imported it from: from sklearn.linear_model import LinearRegression, RidgeCV, LarsCV, Ridge, Lasso, LassoCVHow do I extract the p-values? I checked but rid…

AutoTokenizer.from_pretrained fails to load locally saved pretrained tokenizer (PyTorch)

I am new to PyTorch and recently, I have been trying to work with Transformers. I am using pretrained tokenizers provided by HuggingFace.I am successful in downloading and running them. But if I try to…

How to scroll down in an instagram pop-up frame with Selenium

I have a python script using selenium to go to a given Instagram profile and iterate over the users followers. On the instagram website when one clicks to see the list of followers, a pop-up opens with…

Get starred messages from GMail using IMAP4 and python

I found many dummy info about working with IMAP, but I didnt understand how to use it for my purposes. I found how I can get ALL messages from mailbox and ALL SEEN messages, but how should I work with …

python and php bcrypt

I was using Laravel to register the users. It uses bcrypt like so:$2y$10$kb9T4WXdz5aKLSZX1OkpMOx.3ogUn9QX8GRZ93rd99i7VLKmeoXXXI am currently making another script that will authenticate users from anot…

Python socket library thinks socket is open when its not

Im working with a bit of Python that looks like this:HOST = 127.0.0.1 PORT = 43434 single = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try:single.bind((HOST, PORT)) except socket.error as e:# Pr…