Pandas offset DatetimeIndex to next business if date is not a business day

后端 未结 3 916
醉梦人生
醉梦人生 2021-01-03 06:20

I have a DataFrame which is indexed with the last day of the month. Sometimes this date is a weekday and sometimes it is a weekend. Ignoring holidays, I\'m looking to offset

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 07:21

    You can map the index with a lambda function, and set the result back to the index.

    df.index = df.index.map(lambda x: x if x.dayofweek < 5 else x + pd.DateOffset(7-x.dayofweek))
    
    df
                A
    2015-04-30  0
    2015-06-01  0
    2015-06-30  0
    

提交回复
热议问题