Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate time series by quarter, increment by one quarter

I looked through the arrow and python docs, doesn't seem to be anything that incrementally steps by quarter. For example, the following code incrementally gives you the month, given a starting month up until now. Looking thru the arrow docs, month end is convenient. anything out there that does quarterly?

import arrow
from datetime import datetime
a=arrow.Arrow.span_range('month', datetime(2012,7,1,0,0),datetime.now() )

for i in a:
    print i[1].floor('day').datetime.strftime("%Y-%m-%d")

I'm looking for a solution that steps by quarter up to now

input: 14Q3

output:

14Q3
14Q4
15Q1
15Q2
15Q3
like image 568
jason Avatar asked Oct 16 '15 01:10

jason


1 Answers

To get the current quarter, use: (month - 1) // 3 + 1. To generate quarters in the given range:

from datetime import date

def generate_quarters(start, end):
    while start < end: #NOTE: not including *end*
        yield start
        start = start.increment()

start = Quarter.from_string('14Q3')
end = Quarter.from_date(date.today())
print("\n".join(map(str, generate_quarters(start, end))))

where Quarter is a simple namedtuple subclass:

from collections import namedtuple

class Quarter(namedtuple('Quarter', 'year quarter')):
    __slots__ = ()

    @classmethod
    def from_string(cls, text):
        """Convert 'NQM' into Quarter(year=2000+N, quarter=M)."""
        year, quarter = map(int, text.split('Q'))
        return cls(year + 2000, quarter)

    @classmethod
    def from_date(cls, date):
        """Create Quarter from datetime.date instance."""
        return cls(date.year, (date.month - 1) // 3 + 1)

    def increment(self):
        """Return the next quarter."""
        if self.quarter < 4:
            return self.__class__(self.year, self.quarter + 1)
        else:
            assert self.quarter == 4
            return self.__class__(self.year + 1, 1)

    def __str__(self):
        """Convert to "NQM" text representation."""
        return "{year}Q{quarter}".format(year=self.year-2000, quarter=self.quarter)

Output

14Q3
14Q4
15Q1
15Q2
15Q3

The current quarter (15Q4) is not included.

like image 173
jfs Avatar answered Oct 31 '22 13:10

jfs