5

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
jason
  • 3,811
  • 18
  • 92
  • 147
  • What date would a quarter resolve to? The start, middle or end of the applicable quarter? – dawg Oct 16 '15 at 01:15
  • Couldn't you just step up three months at a time? – intcreator Oct 16 '15 at 01:17
  • @ brandaemon, i guess i could, then use regex to covert? the month ends? there is no convention for quarters in `datetime` right? – jason Oct 16 '15 at 01:21
  • A text search "quarter" on the docs for [2](https://docs.python.org/2/library/datetime.html) and [3](https://docs.python.org/3/library/datetime.html) yielded nothing. Looks like you'll have to hand-code this one. By the way, I didn't get a notification because of the space between @ and my username. – intcreator Oct 16 '15 at 02:27

2 Answers2

3

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.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
2

not pretty but works

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")

b=[(x[1].date().month-1)//3 for x in a]
c=[]
for i in range(0,len(b),3):
    c.append(str(a[i][1].year)[2:]+"Q"+str(b[i]))
jason
  • 3,811
  • 18
  • 92
  • 147