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
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') rng = pandas.date_range('01 Jan 2014', '05 Jan 2014', freq='1min') intraday_vals = pandas.DataFrame(data=pandas.np.random.randn(len(rng)), index=rng) intraday_vals = tsf.resample_time_series(intraday_vals, '60min') intraday_vals = tsf.remove_out_FX_out_of_hours(intraday_vals)