简体   繁体   中英

Generate list of specific dates every month

I would like to obtain a list of the 1st, 6th, 11th, 16th, 21th, 26th day of every month over the period November to March.

The only code I have knowledge of, however, counts the 5 day interval between start and end dates and this does not produce a desired result.

daterange = pd.date_range('1982-11-01','1983-03-30' , freq='5D') 
daterange = daterange.union([daterange[-1] + 1])  
daterange = [d.strftime('%Y-%b-%d') for x in daterange]

I am confused with what you want.

daterange = pd.date_range('1982-11-01','1983-03-30', freq='5D')

for d in daterange:
    print(d)

Output:

1982-11-01 00:00:00
1982-11-06 00:00:00
1982-11-11 00:00:00
1982-11-16 00:00:00
1982-11-21 00:00:00
1982-11-26 00:00:00
1982-12-01 00:00:00
1982-12-06 00:00:00
1982-12-11 00:00:00
...

You do not give much information about your expected output, but simple for loops could achieve the list of dates that you seek:

import datetime

periods = [(1982, 11), (1982, 12), (1983, 1), (1983, 2), (1983, 3)]
date_list = [
    datetime.date(y, m, d)
    for y, m in periods
    for d in [1, 6, 11, 16, 21, 26]]

Is that what you are looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM