从原点和偏移量创建Pandas DatetimeIndex

时间:2019-03-14 17:48:19

标签: python pandas

我想将几秒钟转换为适合数据框的熊猫索引。

我想我正在寻找dt.total_seconds()操作的逆函数,就像对https://stackoverflow.com/a/36156620/1653571Accessing 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))

1 个答案:

答案 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)