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

后端 未结 3 915
醉梦人生
醉梦人生 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:03

    You can add 0*BDay()

    from pandas.tseries.offsets import BDay
    df.index = df.index.map(lambda x : x + 0*BDay())
    

    You can also use this with a Holiday calendar with CDay(calendar) in case there are holidays.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-03 07:23

    Using DataFrame.resample

    A more idiomatic method would be to resample to business days:

    df.resample('B', label='right', closed='right').first().dropna() 
    
                  A
    2015-04-30  0.0
    2015-06-01  0.0
    2015-06-30  0.0
    
    0 讨论(0)
提交回复
热议问题