0
import pandas as pd

start = datetime.datetime(2021, 1, 8)
end = datetime.datetime(2021, 5, 7)

required_output = ['2021-01-08', '2021-02-01', '2021-03-01', '2021-04-01', '2021-05-07']

Given two input date start and end. I would like to have list as output which first date of each month used for interp. can this be done? I was trying to do via pandas as below.

df = pd.date_range(start=start, end=end, freq='1M')
Henry
  • 3,472
  • 2
  • 12
  • 36
  • use `freq='MS'` – Deven Ramani Jul 20 '21 at 10:49
  • hi! Is any one of the answers below working? If so & if you wish, you might consider [accepting](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) one of them to signal others that the issue is resolved. If not, you can provide feedback so they can be improved (or removed altogether) – Anurag Dabas Jul 27 '21 at 06:43

3 Answers3

0

One solution that does not require pandas:

from datetime import datetime, timedelta

delta = timedelta(days=32)
start = datetime(2021, 1, 8)
end = datetime(2021, 5, 7)

dates = [start]
cur_date = start

# Remove two months from the end date
# 1. for the end date which we use as is
# 2. for the while loop which alwasy generates +1
while cur_date < end - 2 * delta:
    # Add 32 days since delta does not support month
    # and set the day to 1 for each generated date
    cur_date = (cur_date + delta).replace(day=1)
    dates.append(cur_date)

# Add the end date to the result
dates.append(end)
print(dates)

Output:

$ python3 ~/tmp/test.py
[
    datetime.datetime(2021, 1, 8, 0, 0), 
    datetime.datetime(2021, 2, 1, 0, 0), 
    datetime.datetime(2021, 3, 1, 0, 0),  
    datetime.datetime(2021, 4, 1, 0, 0),  
    datetime.datetime(2021, 5, 7, 0, 0)
]

Making it into a pd frame should be easy from here (if that is the format you need)

urban
  • 5,392
  • 3
  • 19
  • 45
0

You can map each item in your range the following way to get your desired output:

import pandas as pd
import datetime

start = datetime.datetime(2021, 1, 8)
end = datetime.datetime(2021, 6, 7)
date_range = pd.date_range(start=start, end=end, freq='1MS')
output = list(date_range.map(lambda d: str(d.date())))
print(output)
Alon Gadot
  • 555
  • 3
  • 10
0

Try:

start='2021-01-08'
end='2021-05-07'

Finally:

out=pd.date_range(start=start, end=end, freq='MS').astype(str)[0:-1]
required_output =[start,*out,end]

output of required_output:

['2021-01-08', '2021-02-01', '2021-03-01', '2021-04-01', '2021-05-07']

Update:

For your case use:

out=pd.date_range(start=start, end=end, freq='MS').astype(str)[0:-1]
required_output=[str(start.date()),*out,str(end.date())]
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41