Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added a QuarterEnd class to datetools module. #321

Closed
wants to merge 1 commit into from

2 participants

@npinger

Wes,

Hoping to get this in to the public version so we don't have to keep a separate local copy. If its, a no go, just let me know and we'll work around.

Thanks,

Nathan Pinger
YCharts.com

@wesm
Owner

Cool. This will need unit tests but I'll take it as is

@wesm wesm closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 2, 2011
  1. @npinger
This page is out of date. Refresh to see the latest.
Showing with 48 additions and 0 deletions.
  1. +48 −0 pandas/core/datetools.py
View
48 pandas/core/datetools.py
@@ -491,6 +491,51 @@ def onOffset(self, someDate):
modMonth = (someDate.month - self.startingMonth) % 3
return BMonthEnd().onOffset(someDate) and modMonth == 0
+class QuarterEnd(DateOffset, CacheableOffset):
+ """DateOffset increments between business Quarter dates
+ startingMonth = 1 corresponds to dates like 1/31/2007, 4/30/2007, ...
+ startingMonth = 2 corresponds to dates like 2/28/2007, 5/31/2007, ...
+ startingMonth = 3 corresponds to dates like 3/31/2007, 6/30/2007, ...
+ """
+ _outputName = 'QuarterEnd'
+ _normalizeFirst = True
+
+ def __init__(self, n=1, **kwds):
+ self.n = n
+ self.startingMonth = kwds.get('startingMonth', 3)
+
+ if self.startingMonth < 1 or self.startingMonth > 3:
+ raise Exception('Start month must be 1<=day<=3, got %d'
+ % self.startingMonth)
+
+ self.offset = MonthEnd(3)
+ self.kwds = kwds
+
+ def isAnchored(self):
+ return (self.n == 1 and self.startingMonth is not None)
+
+ def apply(self, other):
+ n = self.n
+
+ wkday, nDaysInMonth = calendar.monthrange(other.year, other.month)
+
+ monthsToGo = 3 - ((other.month - self.startingMonth) % 3)
+ if monthsToGo == 3:
+ monthsToGo = 0
+
+ if n > 0 and not (other.day >= nDaysInMonth and monthsToGo == 0):
+ n = n - 1
+ elif n <= 0 and other.day > nDaysInMonth and monthsToGo == 0:
+ n = n + 1
+
+ other = other + relativedelta(months=monthsToGo + 3*n, day=31)
+
+ return other
+
+ def onOffset(self, someDate):
+ modMonth = (someDate.month - self.startingMonth) % 3
+ return MonthEnd().onOffset(someDate) and modMonth == 0
+
class BYearEnd(DateOffset, CacheableOffset):
"""DateOffset increments between business EOM dates"""
_outputName = 'BusinessYearEnd'
@@ -607,15 +652,18 @@ class Second(Tick):
bmonthEnd = BMonthEnd()
businessMonthEnd = bmonthEnd
bquarterEnd = BQuarterEnd()
+quarterEnd = QuarterEnd()
byearEnd = BYearEnd()
week = Week()
+
# Functions/offsets to roll dates forward
thisMonthEnd = MonthEnd(0)
thisBMonthEnd = BMonthEnd(0)
thisYearEnd = YearEnd(0)
thisYearBegin = YearBegin(0)
thisBQuarterEnd = BQuarterEnd(0)
+thisQuarterEnd = QuarterEnd(0)
# Functions to check where a date lies
isBusinessDay = BDay().onOffset
Something went wrong with that request. Please try again.