i have a df in pandas like
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
where time is the index , there is 5 seconds differance between each row. I want one second differance adding to the index column.
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
Any way to achieve this?
CodePudding user response:
You could use date_range
to create a range with frequency of 1 second, then use reindex
:
df.index = pd.to_datetime(df.index).floor('S')
df = df.reindex(pd.date_range(df.index.min(), df.index.max(), freq='S'))
Output:
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
CodePudding user response:
Use:
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)
Output: