Tomas Mariano Arditi
Tomas Mariano Arditi

Reputation: 23

How to create a range of dates in Python from a specific date to another separating by 6 months?

I need to create a range of dates from '2014-07-05' to '2024-07-05', with intervals of 6 months, '2014-07-05', '2015-01-05', '2015-07-05'.... I am using pd.date_range('2014-07-05', '2024-07-05', freq = '6M'), but the output don't follow my dates, it starts from '2014-07-31' instead of '2014-07-05'. I have read the documentation but I cannot find the solution.

I am trying to use pd.date_range because it is the more direct way to create a range.

import pandas as pd

mydates = pd.date_range('2014-07-05', '2024-07-05', freq = '6M')

I am expecting:

DatetimeIndex(['2014-07-05', '2015-01-05', '2015-07-05',...,'2024-01-05','2024-07-05'],dtype='datetime64[ns]', freq='6M')

I am receiving:

DatetimeIndex(['2014-07-31', '2015-01-31', '2015-07-31', '2016-01-31',
               '2016-07-31', '2017-01-31', '2017-07-31', '2018-01-31',
               '2018-07-31', '2019-01-31', '2019-07-31', '2020-01-31',
               '2020-07-31', '2021-01-31', '2021-07-31', '2022-01-31',
               '2022-07-31', '2023-01-31', '2023-07-31', '2024-01-31'],
              dtype='datetime64[ns]', freq='6M')

Upvotes: 2

Views: 142

Answers (1)

Massifox
Massifox

Reputation: 4487

If I understand your question correctly, this code is for you:

from datetime import datetime
import pandas as pd

start = datetime.strptime('2014-07-05', '%Y-%m-%d')
end = datetime.strptime('2024-07-05', '%Y-%m-%d') + pd.offsets.MonthBegin()

mydate = pd.date_range(start, end,freq='6M') 
mydate -= pd.offsets.Day(mydate[0].day - start.day)

and gives:

DatetimeIndex(['2014-07-05', '2015-01-05', '2015-07-05', '2016-01-05',
           '2016-07-05', '2017-01-05', '2017-07-05', '2018-01-05',
           '2018-07-05', '2019-01-05', '2019-07-05', '2020-01-05',
           '2020-07-05', '2021-01-05', '2021-07-05', '2022-01-05',
           '2022-07-05', '2023-01-05', '2023-07-05', '2024-01-05',
           '2024-07-05'],
          dtype='datetime64[ns]', freq=None)

With respect to your code, I have arranged 2 things:

  1. I added 1 month to end date, in order to include the value in the range, which was previously excluded

  2. I added the correct offset to mydate

Upvotes: 1

Related Questions