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
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)
14Q3
14Q4
15Q1
15Q2
15Q3
The current quarter (15Q4
) is not included.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With