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).