我正在从文件中读取一些 csv 日志数据,并使用日期、时间字段作为帧的索引。当我绘制时间序列时,绝对时间显示在 X 轴中。我想在 x 轴上显示相对于开始时间的时间。如何做到这一点?
例如:这是一个示例 x 轴:
23:59:57--------+23:59:58----------23:59:59--------+00:00:00--------------+
我想要这样:
0---------00:00:01----------00:00:02--------+00:00:03--------------+
最佳答案
一个简单的解决方案是从索引中减去第一个索引项。这可以通过使用列表理解来完成,如果您的 Dataframe 非常大,这可能不是最好(最快)的选择。
begin = pd.datetime(2013,1,5,5,53)
end = pd.datetime(2013,1,7,7,16)
rng = pd.DatetimeIndex(start=begin, end=end, freq=pd.datetools.Minute(15))
df = pd.DataFrame(np.random.randn(rng.size), index=rng)
fig, axs = plt.subplots(2,1, figsize=(15,6))
fig.subplots_adjust(hspace=.5)
df.plot(ax=axs[0])
axs[0].set_title('Original')
df.index = [idx - df.index[0] for idx in df.index]
df.plot(ax=axs[1])
axs[1].set_title('Normalized')
关于python - Pandas 时间序列使用相对于开始的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14867512/