Get List of Months Between Two Dates in Python

Use pandas.period_range() Method

To get the list of months between two specified dates in Python:

  • Use pandas’s period_range() to get list of months between start date and end date with freq='M'.
  • Use list comprehension to iterate over list of months.
  • For each month, use strftime() method to convert it in appropriate format.

The execution of the above program will print the following on the console.

The pandas library is a powerful data analysis and manipulation tool for Python. It provides data structures and operations for manipulating numerical tables and time series.

It supports high-level data structures and functions for working with relational or labelled data. The pandas library provides the pd.period_range() method that creates a TimeSeries object from a date range and returns a tuple of the days between two dates.

It holds start, end, period, and freq as arguments. Of the three parameters: start, end, and period, exactly two must be specified; otherwise, the method will return a ValueError.

We used pd.period_range() to find the months between start_date and end_date by providing the Month end frequency M as an offset alias to the freq.

The strftime() function in Python formats date and time objects into strings according to the specified format. It is a part of the datetime module, which provides various functions for working with dates and times.

Once we got the month_list, we used list comprehension to apply the strftime() method to format every month of month_list using %b-%Y as format codes. There are many other format codes documented.

To print every month in month_list, we provided the print() method with sep=", " to the print statement to print the list separated by a comma.

Use Nested List Comprehension

To get the list of months between two dates in Python:

  • Use the datetime.strptime() method to convert start and end date to appropriate format.
  • Use the for loop to iterate year that lies in the range of start date’s year and end date’s year.
  • Use an inner for loop to iterate every month that lies in the below range().
  • In the range() function, set the start to start date's month if year equals start date's year; otherwise set it to 1.
  • In the range() function, set the end to (end date's month)+1 if year equals end date's year; otherwise set it to 13.

On the successful execution of the above code, we will get the following output.

The datetime module in Python provides classes for manipulating dates and times easily. From this module, we imported the datetime class.

The strptime() function of datetime parses a string and interprets it as a date and time in a specified format. We applied it on every date in dates to get formatted start_date and end_date.

We used the for loop to iterate every year lying in the range start_date.year and end_date.year. The loop creates a formatted month_list using strptime() and strftime() functions which we discussed earlier.

We used an inner for loop to iterate both dates concerning their months. In that loop, we used the range() function as:

  • We set the start to start_date.month if year equals start_date.year; otherwise, the program jumped the else part to set it to 1.
  • We set the end to start_date.month+1 if year equals end_date.year; otherwise, the program jumped the else part to set it to 13.

Printing is the same as we discussed while explaining the code snippet using the pandas.period_range() method.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *