Python大 pandas 绘制带有时间间隔的时间序列

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

我正在尝试绘制一个带有TimeStamp indizes的pandas DataFrame,它在其indizes中有一个时间间隔.使用pandas.plot()会在前一个分段的最后一个时间戳与下一个分段的第一个时间戳之间进行线性插值.我既不想线性插值,也不要两个日期段之间的空白.有办法吗?

I am trying to plot a pandas DataFrame with TimeStamp indizes that has a time gap in its indizes. Using pandas.plot() results in linear interpolation between the last TimeStamp of the former segment and the first TimeStamp of the next. I do not want linear interpolation, nor do I want empty space between the two date segments. Is there a way to do that?

假设我们有一个带有TimeStamp的DataFrame:

Suppose we have a DataFrame with TimeStamp indizes:

>>> import numpy as np >>> import pandas as pd >>> import matplotlib.pyplot as plt >>> df = pd.DataFrame(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)) >>> df = df.cumsum()

现在让我们取其中的两个时间块并将其绘制:

Now lets take two time chunks of it and plot it:

>>> df = pd.concat([df['Jan 2000':'Aug 2000'], df['Jan 2001':'Aug 2001']]) >>> df.plot() >>> plt.show()

生成的图具有一条插值线,该插值线连接围绕间隙的时间戳记.我无法弄清楚如何在这台机器上上传图片,但是这些图片来自 Google网上论坛显示了我的问题(interpolated.jpg,no-interpolation.jpg和no gaps.jpg).我可以重新创建第一个,如上所示.第二个可以通过用NaN替换所有间隙值来实现(另请参见此问题).如何获得省略时间间隔的第三个版本?

The resulting plot has an interpolation line connecting the TimeStamps enclosing the gap. I cannot figure out how to upload pictures on this machine, but these pictures from Google Groups show my problem (interpolated.jpg, no-interpolation.jpg and no gaps.jpg). I can recreate the first as shown above. The second is achievable by replacing all gap values with NaN (see also this question). How can I achieve the third version, where the time gap is omitted?

推荐答案

尝试:

df.plot(x=df.index.astype(str))

您可能需要自定义刻度线和刻度线标签.

You may want to customize ticks and tick labels.

编辑

使用pandas 0.17.1和numpy 1.10.4对我有效.

That works for me using pandas 0.17.1 and numpy 1.10.4.

您真正需要的是将DatetimeIndex转换为与日期时间不一样的另一种类型的方法.为了获得有意义的标签,我选择了str.如果x=df.index.astype(str)不适用于您的pandas/numpy/任何组合,您可以尝试其他选择:

All you really need is a way to convert the DatetimeIndex to another type which is not datetime-like. In order to get meaningful labels I chose str. If x=df.index.astype(str) does not work with your combination of pandas/numpy/whatever you can try other options:

df.index.to_series().dt.strftime('%Y-%m-%d') df.index.to_series().apply(lambda x: x.strftime('%Y-%m-%d')) ...

我意识到没有必要重置索引,因此我删除了该部分.

I realized that resetting the index is not necessary so I removed that part.

更多推荐

Python大 pandas 绘制带有时间间隔的时间序列

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

发布评论

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

>www.elefans.com

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