Writing consecutive dates in the excel file

Viewed 27

I want to write consecutive dates in a column in the excel file (for example 10,11,2022, 10,11.2024, 10,11,2026,.....), but I am not sure how to do this in Python? I appreciate it if you help me with this, thanks!

1 Answers

Use date_range by 2 years with offsets.DateOffset:

first='2022-10-11'

r = pd.date_range(first, periods=5, freq=pd.offsets.DateOffset(years=2))
print (r)
DatetimeIndex(['2022-10-11', '2024-10-11', '2026-10-11', '2028-10-11',
               '2030-10-11'],
              dtype='datetime64[ns]', freq='<DateOffset: years=2>')


print (r.strftime('%m.%d.%Y'))
Index(['10.11.2022', '10.11.2024', '10.11.2026', '10.11.2028', '10.11.2030'],dtype='object')
Related