Given previous datetime values in a Pandas DataFrame–either as an index or as values in a column–is there a way to “autofill” remaining time increments based on the previous fixed increments?
For example, given:
JavaScript
x
9
1
import pandas as pd
2
import numpy as np
3
df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]},
4
index = [pd.Timestamp('20130101 09:00:00'),
5
pd.Timestamp('20130101 09:00:05'),
6
pd.Timestamp('20130101 09:00:10'),
7
np.nan,
8
np.nan])
9
I would like to apply a function to yield:
B | |
---|---|
2013-01-01 09:00:00 | 0.0 |
2013-01-01 09:00:05 | 1.0 |
2013-01-01 09:00:10 | 2.0 |
2013-01-01 09:00:15 | NaN |
2013-01-01 09:00:20 | 4.0 |
Where I have missing timesteps for my last two data points. Here, timesteps are fixed in 5 second increments.
This will be for thousands of rows. While I might reset_index and then create a function to apply to each row, this seems cumbersome. Is there a slick or built-in way to do this that I’m not finding?
Advertisement
Answer
Assuming the first index value is a valid datetime and all the values are spaced 5s apart, you could do the following:
JavaScript
1
9
1
df.index = pd.date_range(df.index[0], periods=len(df), freq='5s')
2
>>> df
3
B
4
2013-01-01 09:00:00 0.0
5
2013-01-01 09:00:05 1.0
6
2013-01-01 09:00:10 2.0
7
2013-01-01 09:00:15 NaN
8
2013-01-01 09:00:20 4.0
9