简体   繁体   中英

Infilling every other Row in a Pandas Dataframe the the value from the last row + 30 minutes

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM