Loop through dates except for weekends

后端 未结 3 717
生来不讨喜
生来不讨喜 2020-12-15 10:28

So I have a script that has date arguments for different functions and I want it to loop through 01-01-2012 to 06-09-2012 not including weekends. I

3条回答
  •  失恋的感觉
    2020-12-15 11:17

    There is easier way to do this using freq = 'B' for business day frequency.

    import pandas as pd
    dt = pd.date_range(start=datetime.date.today(), periods=10, freq='B')
    dt
    

    which gives you :

    DatetimeIndex(['2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16',
                   '2018-08-17', '2018-08-20', '2018-08-21', '2018-08-22',
                   '2018-08-23', '2018-08-24'],
                  dtype='datetime64[ns]', freq='B')
    

    you check name of day also by:

    dt.weekday_name
    
    Index(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Monday',
           'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
          dtype='object')
    

提交回复
热议问题