我可以帮您翻译。以下是翻译结果,保留html标签:
我可以像这样选择此系列中的元素:
我可以生成这样的图表:
我有一个非常类似的问题:Pandas图形化时间序列,并在选定日期处绘制垂直线,但该解决方案不能使用Timedelta。
考虑这个系列:
In:
avg_hr.head()
Out:
00:00:00 69.000000
00:00:01 93.750000
00:00:02 93.125000
00:00:03 92.900000
00:00:04 93.222222
00:00:05 93.222222
...
Name: bpm, Length: 253, dtype: float64
我可以像这样选择此系列中的元素:
In:
avg_hr[pd.Timedelta(seconds=3)]
Out:
92.9
我可以生成这样的图表:
In:
avg_hr.plot()
但是,我无法像这样使用TimeDelta绘制垂直线:
In:
plt.axvline(x=pd.Timedelta(seconds=110), color='r', linestyle='dashed', linewidth=2)
Out:
TypeError: Cannot compare type 'Timedelta' with type 'float64'
但是,如果我使用float或int类型,竖线会出现在位置0。
In:
plt.axvline(x=110, color='r', linestyle='dashed', linewidth=2)
如何使用timedelta索引绘制垂直线?
编辑:
即使我直接使用x轴上使用的键,我也会得到相同的错误:
In:
for key in avg_hr.keys():
ax.axvline(x=key, color='r', linestyle='dashed', linewidth=2)
Out:
TypeError: Cannot compare type 'Timedelta' with type 'float64'
avg_hr.index.dtype
返回的是什么? - Vinícius Figueiredo