gpt4 book ai didi

python - 如何让 Pandas 系列只绘制索引中的数据?

转载 作者:太空宇宙 更新时间:2023-11-04 08:08:35 25 4
gpt4 key购买 nike

编辑 预计会错过日期 20140103 和 20140104,我不希望它们自动修补。而且我不想使用 xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts)) 而不是 xs = pd.Series (data=range(len(ts)), index=pd.to_datetime(ts)) 因为我想使用一些操作作为

xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))

xs['2014']
Out[24]:
2014-01-01 0
2014-01-02 1
2014-01-05 2
2014-01-06 3
2014-01-07 4
dtype: int64

不适用于:

In [26]: xs = pd.Series(data=range(len(ts)), index=ts)

In [27]: xs['2014']
---------------------------------------------------------------------------
KeyError: '2014'

例如:

In [1]: import pandas as pd

In [2]: ts = ['20140101', '20140102', '20140105', '20140106', '20140107']

In [3]: xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))

In [4]: xs.plot()

将绘制此图像,其中添加了关于 20140103、20140104 的额外数据。 enter image description here

虽然我想要这样的图像:

import matplotlib.pyplot as plt

plt.plot(xs.values)

enter image description here

最佳答案

感谢 euri10 use_index = False

ts = ['20140101', '20140102', '20140105', '20140106', '20140107']
xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))
fig, ax = plt.subplots()
xs.plot(use_index=False)
ax.set_xticklabels(pd.to_datetime(ts))
ax.set_xticks(range(len(ts)))
fig.autofmt_xdate()
plt.show()

enter image description here

关于python - 如何让 Pandas 系列只绘制索引中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27181108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com