finding last business day of a month in python

前端 未结 6 1145
半阙折子戏
半阙折子戏 2020-12-31 08:33

I\'m trying to find last business day of of the month. I wrote the code below for that and it works fine but I was wondering if there is a cleaner way of doing it?

6条回答
  •  心在旅途
    2020-12-31 09:09

    with rollforward(d) you will skip to the next month if the date is past the last business day of the current month, so below might be safer for any day of the month:

    from datetime import date
    import pandas as pd
    
    d = date(2011, 12, 31) # a caturday
    pd.bdate_range(end=pd.offsets.MonthEnd().rollforward(d), periods=1)
    pd.offsets.BMonthEnd().rollforward(d)
    

提交回复
热议问题