问题描述
当使用内置的pandas绘图功能绘制时间序列时,似乎会忽略索引的时区:它始终将UTC时间用于x轴.一个例子:
When plotting a timeseries with the built-in plot function of pandas, it seems to ignore the timezone of my index: it always uses the UTC time for the x-axis. An 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()
尽管索引具有:
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 file a bug, or do I miss something?
推荐答案
这绝对是一个错误.我已经在 github 上创建了一个报告.原因是因为在内部,pandas将常规频率DatetimeIndex转换为PeriodIndex以挂接到pandas中的格式化程序/定位器,并且当前PeriodIndex不保留时区信息.请继续关注以解决问题.
This is definitely a bug. I've created a report on github. The reason is because internally, pandas converts a regular frequency DatetimeIndex to PeriodIndex to hook into formatters/locators in pandas, and currently PeriodIndex does NOT retain timezone information.Please stay tuned for a fix.
这篇关于 pandas 图函数忽略时间序列的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!