更改 pandas 列时间戳的索引

Change the index of pandas column timestamp

我在pandas有一个df喜欢

     time                      lower_boundary    mark_price  
  2022-04-05T02:51:36.147633Z  -116914.699042   17.174680   
  2022-04-05T02:51:41.246010Z  -116746.074569   17.263622   
  2022-04-05T02:51:46.345506Z  -116677.835351   17.371671   

其中时间是索引,每行之间有5秒的差异。我想在索引列中添加一秒钟的差异。

 time                        lower_boundary                        
2022-04-05T02:51:36.147633Z   -116914.699042
2022-04-05T02:51:37.147633Z    None
2022-04-05T02:51:38.147633Z    None
2022-04-05T02:51:39.147633Z    None
2022-04-05T02:51:40.147633Z    None
2022-04-05T02:51:41.246010Z   -116746.074569

有什么方法可以实现吗?

使用:

s =['2022-04-05T02:51:36.147633Z', '2022-04-05T02:51:41.246010Z', '2022-04-05T02:51:46.345506Z']
vals = [1,2,3]
df = pd.DataFrame(vals, columns = ['val'], index = pd.to_datetime(s).floor('S'))
temp = pd.date_range(df.index[0], df.index[-1], freq='S')
df.reindex(temp)

输出:

您可以使用 date_range 创建频率为 1 秒的范围,然后使用 reindex:

df.index = pd.to_datetime(df.index).floor('S')
df = df.reindex(pd.date_range(df.index.min(), df.index.max(), freq='S'))

输出:

                            lower_boundary  mark_price
2022-04-05 02:51:36+00:00  -116914.699042   17.174680
2022-04-05 02:51:37+00:00             NaN         NaN
2022-04-05 02:51:38+00:00             NaN         NaN
2022-04-05 02:51:39+00:00             NaN         NaN
2022-04-05 02:51:40+00:00             NaN         NaN
2022-04-05 02:51:41+00:00  -116746.074569   17.263622
2022-04-05 02:51:42+00:00             NaN         NaN
2022-04-05 02:51:43+00:00             NaN         NaN
2022-04-05 02:51:44+00:00             NaN         NaN
2022-04-05 02:51:45+00:00             NaN         NaN
2022-04-05 02:51:46+00:00  -116677.835351   17.371671