Reqbisha
Reqbisha

Reputation: 117

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]

Upvotes: 0

Views: 209

Answers (2)

TheWhistler
TheWhistler

Reputation: 21

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
...

Upvotes: 1

Ralf
Ralf

Reputation: 16515

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?

Upvotes: 0

Related Questions