Those who work with time series or time dependent data sometimes need to get the months between two dates.

Naturally we would incline to use pandas.date_range. However, pandas date_range does not enclude the end month. In the following example, the end month 2021-06 is not included Pandas’s output, with closed parameter set to None, left, or right.

>>> import pandas as pd
>>> start_date = '2020-01-01'
>>> end_date = '2021-06-04'
>>> pd.date_range(start=start_date, end=end_date, freq='M').strftime("%Y-%m").to_list()   
['2020-01', '2020-02', '2020-03', '2020-04', '2020-05', '2020-06', '2020-07', '2020-08', '2020-09', '2020-10', '2020-11', '2020-12', '2021-01', '2021-02', '2021-03', '2021-04', '2021-05']
>>> pd.date_range(start=start_date, end=end_date, freq='M', closed='right').strftime("%Y-%m").to_list() 
['2020-01', '2020-02', '2020-03', '2020-04', '2020-05', '2020-06', '2020-07', '2020-08', '2020-09', '2020-10', '2020-11', '2020-12', '2021-01', '2021-02', '2021-03', '2021-04', '2021-05']
>>> pd.date_range(start=start_date, end=end_date, freq='M', closed='left').strftime("%Y-%m").to_list() 
['2020-01', '2020-02', '2020-03', '2020-04', '2020-05', '2020-06', '2020-07', '2020-08', '2020-09', '2020-10', '2020-11', '2020-12', '2021-01', '2021-02', '2021-03', '2021-04', '2021-05']

There are different ways to solve for this, including adding an offset month/week, or adding the next logical value. Alternatively, one convenient way to do it is to use period_range instead of date_range.

>>> import pandas as pd
>>> start_date = '2020-01-01'
>>> end_date = '2021-06-04'
>>> pd.period_range(start=start_date, end=end_date, freq='M').strftime("%Y-%m").to_list()
['2020-01', '2020-02', '2020-03', '2020-04', '2020-05', '2020-06', '2020-07', '2020-08', '2020-09', '2020-10', '2020-11', '2020-12', '2021-01', '2021-02', '2021-03', '2021-04', '2021-05', '2021-06']

Interesting kinks in Panda’s API. And yes, it’s much easier to use Pandas as it has simple frequency sampling API.

References:

pandas.preiod_range

pandas.date_range

@misc{leehanchung,
author = {Lee, Hanchung},
title = {Getting Months from a Date Range in Pandas},
year = {2021},
howpublished = {Github Repo},
url = {https://leehanchung.github.io/2021-06-04-pandas-date-ranges/}