pandas 图函数忽略时间序列的时区

编程入门 行业动态 更新时间:2024-10-22 23:05:27
本文介绍了 pandas 图函数忽略时间序列的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当使用内置的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 图函数忽略时间序列的时区

本文发布于:2023-07-18 04:47:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   函数   时区   时间   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!