-3

I need to get the list of quarters between the given dates in python.

For example: start_date = March 2012 , end_date = June 2013

Expected output:

['March 2012', 'June 2012', 'September 2012', 'December 2012', 'March 2013', 'June 2013']

I checked other threads like generate time series by quarter, increment by one quarter but my requirement is little different. I need it in the exact same format as mentioned above.

yadav
  • 99
  • 3
  • 9
  • 1
    Can you show what you have tried so far. – pulkit-singhal May 30 '18 at 10:59
  • I have tried script mentioned my post link. But format is different that what i want. Also i was wonder if there is any lib or built in function that does the same instead of doing ot through math – yadav May 30 '18 at 11:03

3 Answers3

6

Use date_range with add one quarter, last change format to strftime:

r = (pd.date_range(pd.to_datetime(start_date), 
                   pd.to_datetime(end_date) + pd.offsets.QuarterBegin(1), freq='Q')
      .strftime('%B %Y')
      .tolist())
print (r)
['March 2012', 'June 2012', 'September 2012', 'December 2012', 'March 2013', 'June 2013']
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thanks for the quick & short reply. I wanted exactly the same. @zipa & Rakesh, all are working. – yadav May 30 '18 at 12:28
  • @yadav - You are welcome! – jezrael May 30 '18 at 12:28
  • Stackoverflow have all kind of people, some are always ready to help, others are always ready for the downvote. If someone knows everything wouldn't be posting here, so instead of discouraging help someone or just ignore, why downvote.? – yadav May 30 '18 at 12:32
  • @yadav - hmmm, hard question. Maybe the worse is if get first downvote. Fo avoid it you need some code, what you try. So instead copy link to solution add it and explain, what need change, what is problem with it. – jezrael May 30 '18 at 12:35
  • Here is one with code also: https://stackoverflow.com/questions/50296390/pandas-panel-is-deprecated same story. – yadav May 30 '18 at 12:37
4

Here's a robust one:

from datetime import datetime
from dateutil import relativedelta

start_date, end_date = 'March 2012', 'June 2013'

start_date, end_date = datetime.strptime(start_date, '%B %Y'), datetime.strptime(end_date, '%B %Y')

delta = relativedelta.relativedelta(end_date, start_date)
result =  [datetime.strftime(start_date + relativedelta.relativedelta(months=i), '%B %Y')\
                            for i in range(0, delta.years * 12 + delta.months + 1, 3)]
result
#['March 2012',
# 'June 2012',
# 'September 2012',
# 'December 2012',
# 'March 2013',
# 'June 2013']

But it works.

zipa
  • 27,316
  • 6
  • 40
  • 58
1

Using datetime and dateutil

Demo:

import datetime
from dateutil.relativedelta import relativedelta


def getQ(start_date, end_date):
    res = [start_date]
    start_date = datetime.datetime.strptime(start_date, "%B %Y")
    end_date = datetime.datetime.strptime(end_date, "%B %Y")
    while True:
        cDate = start_date + relativedelta(months=3)
        if cDate >= end_date:
            break
        res.append(cDate.strftime("%B %Y"))
        start_date = cDate
    return res

start_date = 'March 2012'
end_date = 'June 2013'
print(getQ(start_date, end_date))

Output:

['March 2012', 'June 2012', 'September 2012', 'December 2012', 'March 2013']
Rakesh
  • 81,458
  • 17
  • 76
  • 113