如何在Python中向时间序列数据添加秒



所以我有以下时间戳,它们属于pandas数据帧中的TS:

Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),
Timestamp('2010-11-20 00:00:00'),

原始csv文件的读数为每分钟60次,但时间戳只有hh:mm(例如13:23(,当我转换/解析日期时,它只会在所有秒条目中添加00。有熊猫的功能可以增加秒数吗?这背后的动机是为了在matplotlib中很好地绘制图形。目前,我每分钟有60个重叠点,但我希望时间戳增加,例如00:00:01、00:00:02、00:00:03等。

手动在其中添加秒,假设它们已排序并且增量始终为1秒:

df = pd.Series([pd.Timestamp(2020,11,20,0,0)]*10)
df += pd.Series(pd.Timedelta(seconds=i) for i in range(10))
0   2020-11-20 00:00:00
1   2020-11-20 00:00:01
2   2020-11-20 00:00:02
3   2020-11-20 00:00:03
4   2020-11-20 00:00:04
5   2020-11-20 00:00:05
6   2020-11-20 00:00:06
7   2020-11-20 00:00:07
8   2020-11-20 00:00:08
9   2020-11-20 00:00:09
dtype: datetime64[ns]

最新更新