我想将几秒钟转换为适合数据框的熊猫索引。
我想我正在寻找dt.total_seconds()
操作的逆函数,就像对https://stackoverflow.com/a/36156620/1653571的Accessing total_seconds() in pandas data column答案一样
给出:
t0="2010-10-01 00:00:00 UTC"
seconds = [0,1,2,3,5,7,11,13]
我想要类似的东西
ii = pd.DatetimeIndex( ???(t0,seconds))
答案 0 :(得分:3)
将seconds
列表转换为Timedelta
并做一点datetime
算术。
# origin + offset
pd.to_datetime(t0) + pd.to_timedelta(seconds, unit='s')
DatetimeIndex(['2010-10-01 00:00:00+00:00', '2010-10-01 00:00:01+00:00',
'2010-10-01 00:00:02+00:00', '2010-10-01 00:00:03+00:00',
'2010-10-01 00:00:05+00:00', '2010-10-01 00:00:07+00:00',
'2010-10-01 00:00:11+00:00', '2010-10-01 00:00:13+00:00'],
dtype='datetime64[ns, UTC]', freq=None)