Pandas chart function ignores time zone timers

When plotting time series with the built-in pandas plotting function, it seems to ignore the time zone of my index: it always uses UTC for the x axis. Example:

import numpy as np import matplotlib.pyplot as plt from pandas import rolling_mean, DataFrame, date_range rng = date_range('1/1/2011', periods=200, freq='S', tz="UTC") data = DataFrame(np.random.randn(len(rng), 3), index=rng, columns=['A', 'B', 'C']) data_cet = data.tz_convert("CET") # plot with data in UTC timezone fig, ax = plt.subplots() data[["A", "B"]].plot(ax=ax, grid=True) plt.show() # plot with data in CET timezone, but the x-axis remains the same as above fig, ax = plt.subplots() data_cet[["A", "B"]].plot(ax=ax, grid=True) plt.show() 

The graph does not change, although the index has:

 In [11]: data.index[0] Out[11]: <Timestamp: 2011-01-01 00:00:00+0000 UTC, tz=UTC> In [12]: data_cet.index[0] Out[12]: <Timestamp: 2011-01-01 01:00:00+0100 CET, tz=CET> 

Should I write a mistake or am I missing something?

+6
source share
2 answers

This is definitely a mistake. I created a github report. The reason is that internally pandas converts the regular DatetimeIndex frequency to PeriodIndex to connect to formatters / locators in pandas, and currently PeriodIndex does NOT save time zone information. Please stay tuned.

+5
source

What to do with UTC in local time

 import time import matplotlib.dates … tz = pytz.timezone(time.tzname[0]) … ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval=1, tz=tz)) ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%H', tz=tz)) 
0
source

All Articles