We help IT Professionals succeed at work.

In python, looking for a range of "quarters" from a date range

newparadigmz
newparadigmz asked
on
So with the inputs being from_date=2019-01-01 and to_date=today

I need

2019-01-01 - 2019-03-31
2019-04-01 - 2019-06-30
2019-07-01 - 2019-09-30
2019-10-01 - 2019-12-31
2020-01-01 - 2020-02-04

I don't really know any python, but this is the closest I can come up with so far.
The dates are there, I just don't know how to access them or fill in today's date on the last item....

    def get_date_ranges(self, fr_date, to_date):
        qtr_beg = pd.date_range(start=fr_date, end=datetime.date.today(), freq='QS')
        qtr_end = pd.date_range(start=fr_date, end=datetime.date.today(), freq='Q')
        date_range = zip(qtr_beg, qtr_end)
        return date_range

Open in new window

Comment
Watch Question

nociSoftware Engineer
Distinguished Expert 2019

Commented:

Try this:


def get_date_ranges(fr_date, to_date):
        qrange=pd.period_range(start=fr_date,end=to_date,freq='Q')
        qtr_beg = qrange.asfreq('D','s')
        qtr_end = qrange.asfreq('D','e')
        date_range = zip(qtr_beg, qtr_end)
        return date_range

Author

Commented:
How do I use it, to access the dates inside?
NorieAnalyst Assistant

Commented:
Do you mean you want to list all the dates for each quarter?
nociSoftware Engineer
Distinguished Expert 2019

Commented:
import pandas as pd

def get_date_ranges(fr_date, to_date):
         qrange=pd.period_range(start=fr_date,end=to_date,freq='Q')
         qtr_beg = qrange.asfreq('D','s')
         qtr_end = qrange.asfreq('D','e')
         date_range = zip(qtr_beg, qtr_end)
         return date_range

for x in get_date_ranges('2018-02-01','2020-06-30'):
  print(x)

which delivers:

(Period('2018-01-01', 'D'), Period('2018-03-31', 'D'))
(Period('2018-04-01', 'D'), Period('2018-06-30', 'D'))
(Period('2018-07-01', 'D'), Period('2018-09-30', 'D'))
(Period('2018-10-01', 'D'), Period('2018-12-31', 'D'))
(Period('2019-01-01', 'D'), Period('2019-03-31', 'D'))
(Period('2019-04-01', 'D'), Period('2019-06-30', 'D'))
(Period('2019-07-01', 'D'), Period('2019-09-30', 'D'))
(Period('2019-10-01', 'D'), Period('2019-12-31', 'D'))
(Period('2020-01-01', 'D'), Period('2020-03-31', 'D'))
(Period('2020-04-01', 'D'), Period('2020-06-30', 'D'))

The input arrays are the dates generated from period_range and from there the first (qtr_beg) & last (qtr_end) are derived.

The zip function (python built-in) creates pair of numbers from two input arrays (qtr_beg, qtr_end)


Thank you, but I still wasn't sure how to "use" them, entirely due to how little I know python, and me not framing the question correctly.

I was able to get what I need this way....

        dr_qtr_beg = list(pd.date_range(start=fr_date, end=to_date, freq='QS').date)
        dr_qtr_end = list(pd.date_range(start=fr_date, end=to_date, freq='Q').date)

        if len(dr_qtr_end) < len(dr_qtr_beg):
            dr_qtr_end.append(to_date)

        for i in range(len(dr_qtr_end)):
            qtr_beg = dr_qtr_beg[i].strftime("%Y-%m-%d")
            qtr_end = dr_qtr_end[i].strftime("%Y-%m-%d")

Open in new window