Home > Enterprise >  Splitting dates in 6months interval
Splitting dates in 6months interval

Time:10-27

I need to split two dates into 6 months interval in Python. For example:

start_date = 2018-09-23
end_date = 2020-07-13

What i want as output in something like:

[2018-09-23, 2018-12-31] , [2019-01-01, 2019-06-30], [2019-07-01,2019-12-31], [2020-01-01, 2020-06-30], [2020-07-01, 2020-07-13]

Could you please help me with this issue? Thanks a lot

CodePudding user response:

pd.date_range() returns the range of equally spaced time points.

start_date = '2018-09-23'
end_date = '2020-07-13'

pd.date_range(start_date, end_date, freq='6M')

Output:

DatetimeIndex(['2018-09-30', '2019-03-31', '2019-09-30', '2020-03-31'], dtype='datetime64[ns]', freq='6M')

Setting freq = '6M' creates equally spaced date time points, starting at the end of start_date month. We need to then shift all the dates back by the distance from our start_date to the end of the month, to ensure that our date time points begin with start_date

dates = pd.date_range(start_date, end_date, freq='6M', closed='left')
dates = dates - pd.offsets.Day((dates[0] - pd.to_datetime(start_date)).days)

Output:

DatetimeIndex(['2018-09-23', '2019-03-24', '2019-09-23', '2020-03-24'], dtype='datetime64[ns]', freq=None)

We could now loop through our dates list, and create our date range list.

[[dates[i], dates[i 1]-pd.offsets.Day(1)] for i in range(len(dates)-1)]

Output:

[[Timestamp('2018-09-23 00:00:00'), Timestamp('2019-03-23 00:00:00')],[Timestamp('2019-03-24 00:00:00'), Timestamp('2019-09-22 00:00:00')],[Timestamp('2019-09-23 00:00:00'), Timestamp('2020-03-23 00:00:00')]]
  • Related