我试图绘制一个熊猫时间序列,但不是y轴上的实际时间,我想要从时间序列开始以来的时间作为X轴。
有没有方便的格式化方法或将我的时间序列转换为时间增量系列?
通过索引将第一个值减去DatetimeIndex
:
s = pd.Series([1,8,9], index= pd.date_range('2015-01-01', periods=3, freq='H'))
print (s)
2015-01-01 00:00:00 1
2015-01-01 01:00:00 8
2015-01-01 02:00:00 9
Freq: H, dtype: int64
s.index = (s.index - s.index[0])
print (s)
00:00:00 1
01:00:00 8
02:00:00 9
dtype: int64
如果有必要,将TimedeltaIndex
转换为秒,如果在total_seconds
中没有时间则使用days
或DatetimeIndex
:
s.index = (s.index - s.index[0]).total_seconds()
print (s)
0.0 1
3600.0 8
7200.0 9
dtype: int64