def get_bus_day_of_month(self, date, cal = 'FX'): """ get_bus_day_of_month(date = list of dates, cal = calendar name) returns the business day of the month (ie. 3rd Jan, on a Monday, would be the 1st business day of the month """ filter = Filter() try: date = date.normalize() # strip times off the dates - for business dates just want dates! except: pass start = pandas.to_datetime(datetime.datetime(date.year[0], date.month[0], 1)) end = datetime.datetime.today()#pandas.to_datetime(datetime.datetime(date.year[-1], date.month[-1], date.day[-1])) holidays = filter.get_holidays(start, end, cal) bday = CustomBusinessDay(holidays=holidays, weekmask='Mon Tue Wed Thu Fri') bus_dates = pandas.date_range(start, end, freq=bday) month = bus_dates.month work_day_index = numpy.zeros(len(bus_dates)) work_day_index[0] = 1 for i in range(1, len(bus_dates)): if month[i] == month[i-1]: work_day_index[i] = work_day_index[i-1] + 1 else: work_day_index[i] = 1 bus_day_of_month = work_day_index[bus_dates.searchsorted(date)] return bus_day_of_month
df_sorted = df_sorted.head(n=n) return df_sorted def convert_month_day_to_date_time(self, df, year=1970): new_index = [] # TODO use map? for i in range(0, len(df.index)): x = df.index[i] new_index.append(datetime.date(year, x[0], int(x[1]))) df.index = pandas.DatetimeIndex(new_index) return df if __name__ == '__main__': # test functions calc = Calculations() tsf = Filter() # test rolling ewma date_range = pandas.bdate_range('2014-01-01', '2014-02-28') print(calc.get_bus_day_of_month(date_range)) foo = pandas.DataFrame(numpy.arange(0.0, 13.0)) print(calc.rolling_ewma(foo, span=3))
if month[i] == month[i - 1]: work_day_index[i] = work_day_index[i - 1] + 1 else: work_day_index[i] = 1 bus_day_of_month = work_day_index[bus_dates.searchsorted(date)] return bus_day_of_month # functions to test class if __name__ == '__main__': logger = LoggerManager.getLogger(__name__) tsf = Filter() if False: start = pandas.to_datetime('2000-01-01') end = pandas.to_datetime('2020-01-01') logger.info('Get FX holidays') hols = tsf.get_holidays(start, end, cal='FX') print(hols) logger.info('Get business days, excluding holidays') bus_days = tsf.create_calendar_bus_days(start, end, cal='FX') print(bus_days) if False: logger.info('Remove out of hours')