Mark T
Mark T

Reputation: 155

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?

Upvotes: 1

Views: 105

Answers (1)

Mark T
Mark T

Reputation: 155

I've found the solution by myself.

changed:

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

to:

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

Upvotes: 1

Related Questions