简体   繁体   中英

How to resample in pandas dataframe and ignore weekends

I have a dataframe where I read out the last value of the previous day in column 'yVAH' and join it to the next day in 'yVAHnew' with this code:

df = df.join(df.resample('B', on='Date')['yVAH'].last().rename('yVAHnew'), on=pd.to_datetime((df['Date'] - pd.Timedelta('1d')).dt.date))

It works basically but the problem is I get a NaN after the weekends, although I stated the 'B' in resample:

    Date                Time    yVAH    yVAHnew
360 2019-06-21 21:45:00 214500  2942.75 2943.5
361 2019-06-21 21:50:00 215000  2942.75 2943.5
362 2019-06-21 21:55:00 215500  2942.75 2943.5
363 2019-06-21 22:00:00 220000  2942.75 2943.5
364 2019-06-21 22:05:00 220500  2942.75 2943.5
365 2019-06-21 22:10:00 221000  2942.75 2943.5
366 2019-06-24 07:00:00 70000   2941.00 NaN
367 2019-06-24 07:05:00 70500   2941.00 NaN
368 2019-06-24 07:10:00 71000   2941.00 NaN
369 2019-06-24 07:15:00 71500   2941.00 NaN
370 2019-06-24 07:20:00 72000   2941.00 NaN
371 2019-06-24 07:25:00 72500   2941.00 NaN   

I assume timedelta is the problem? What could I do to get rid of the NaN's after the weekends?

I've found the solution by myself.

changed:

pd.Timedelta('1d')).dt.date))

to:

pd.tseries.offsets.BusinessDay()).dt.date))

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