将Pandas时间序列转换为时间增量

问题描述 投票:2回答:1

我试图绘制一个熊猫时间序列,但不是y轴上的实际时间,我想要从时间序列开始以来的时间作为X轴。

有没有方便的格式化方法或将我的时间序列转换为时间增量系列?

python pandas timedelta
1个回答
1
投票

通过索引将第一个值减去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中没有时间则使用daysDatetimeIndex

s.index = (s.index - s.index[0]).total_seconds()
print (s)
0.0       1
3600.0    8
7200.0    9
dtype: int64
© www.soinside.com 2019 - 2024. All rights reserved.