Home > Blockchain >  Infilling every other Row in a Pandas Dataframe the the value from the last row 30 minutes
Infilling every other Row in a Pandas Dataframe the the value from the last row 30 minutes

Time:08-31

I'm adding dummy rows to some data so I am able to Join it with an existing dataset. I need data every 30 minutes but the dataset has every 1 hour. I have created a dummy row for every other row and imputed its value using .ffill() but have no idea of to fill the Pandas time series in the same manner but add 30 minutes to each value.

I want something along this line that replaces Nan values and indexes 30 minutes from the last non-Nan.

Input DF

0        2011-11-11 00:00:00
1        Nan
2        2011-11-11 01:00:00
3        Nan
4        2011-11-11 02:00:00

Code

df['time'] = df['time'].ffill()   000-00-00 00:30:00

Output

0        2011-11-11 00:00:00
1        2011-11-11 00:30:00
2        2011-11-11 01:00:00
3        2011-11-11 01:30:00
4        2011-11-11 02:00:00

CodePudding user response:

df['time'] = df['time'].fillna(df['time'].shift()   pd.Timedelta('30 min'))

Alternatively, if you are sure that there are no missed rows, you can just generate the new timestamps:

min_time = df.loc[0, 'time']
df['time'] = pd.date_range(min_time, freq='30 min', periods=len(df))
  • Related