pythondataframedatetimetime-series

How to set the start date of the workweek for a month using Python?


I have a database with a date column in the following format:

data

However, I need to work with this database so that it can be recognized as a time series. In this case, the names "week 1", "week 2", "week 3", and "week 4" should be replaced with dates. If we know that each week has 7 days, each of the weeks in the month would start on the first business day of that month, and the next week would be 7 days later. Is it possible to do this with Python?

For example, in the month of June 2024, the first business day was the 3rd. So,

week 1 = 06/03/2024
week 2 = 06/10/2024
week 3 = 06/17/2024
week 4 = 06/24/2024

And so on, each month would have its own specificity and start date. Is it possible to do this with Python and then obtain a weekly frequency of business days?


Solution

  • Python's datetime provides a numeric weekday, so you just need to add the missing days to the next monday (in case the first day is on a weekend):

    import datetime as dt
    
    def first_business_day(year, month):
        # Start with the first day of the month and it's weekday (0=Monday, 6=Sunday)
        first_day = dt.datetime(year, month, 1)
        first_weekday = first_day.weekday()
    
        for i, weekday in enumerate([5,6]): # Add days to next monday for sat/sun
            if first_weekday == weekday:
                first_day += dt.timedelta(days=2-i)
        # returns 4 weeks based on the first business day!
        return list(first_day + dt.timedelta(days=x*7) for x in range(0, 4))
    
    for m in range(1, 13):
        first_business_day_weeks = first_business_day(2024, m)
        print("first_business_day_weeks:", [x.strftime("%Y-%m-%d") for x in first_business_day_weeks])
    

    Out:

    first_business_day_weeks: ['2024-01-01', '2024-01-08', '2024-01-15', '2024-01-22']
    first_business_day_weeks: ['2024-02-01', '2024-02-08', '2024-02-15', '2024-02-22']
    first_business_day_weeks: ['2024-03-01', '2024-03-08', '2024-03-15', '2024-03-22']
    first_business_day_weeks: ['2024-04-01', '2024-04-08', '2024-04-15', '2024-04-22']
    first_business_day_weeks: ['2024-05-01', '2024-05-08', '2024-05-15', '2024-05-22']
    first_business_day_weeks: ['2024-06-03', '2024-06-10', '2024-06-17', '2024-06-24']
    first_business_day_weeks: ['2024-07-01', '2024-07-08', '2024-07-15', '2024-07-22']
    first_business_day_weeks: ['2024-08-01', '2024-08-08', '2024-08-15', '2024-08-22']
    first_business_day_weeks: ['2024-09-02', '2024-09-09', '2024-09-16', '2024-09-23']
    first_business_day_weeks: ['2024-10-01', '2024-10-08', '2024-10-15', '2024-10-22']
    first_business_day_weeks: ['2024-11-01', '2024-11-08', '2024-11-15', '2024-11-22']
    first_business_day_weeks: ['2024-12-02', '2024-12-09', '2024-12-16', '2024-12-23']
    

    Note: In case you need a pandas solution, have a look at: First Business Date of Month Python