def getworkday(): strt = time.strftime('%Y-%m-%d', time.localtime(time.time())) strt = strt.split("-") y = int(strt[0]) m = int(strt[1]) d = int(strt[2]) april_last = date(y, m, d) print(april_last) print(is_workday(april_last)) print("fuckshit", is_holiday(april_last)) print("dick", not is_holiday(april_last)) print("elecshit", is_workday(april_last) and (not is_holiday(april_last))) return is_workday(april_last) and (not is_holiday(april_last))
def get_trade_days(start_date, end_date): """计算两个日期间的工作日""" from chinese_calendar import is_holiday # 字符串格式日期的处理 if type(start_date) == str: start_date = datetime.strptime(start_date, "%Y-%m-%d").date() if type(end_date) == str: end_date = datetime.strptime(end_date, "%Y-%m-%d").date() # 开始日期大,颠倒开始日期和结束日期 if start_date > end_date: start_date, end_date = end_date, start_date date_list = [] counts = 0 while True: if start_date > end_date: break if is_holiday(start_date) or start_date.weekday() == 5 or start_date.weekday() == 6: start_date += timedelta(days=1) continue date_list.append(str(start_date.strftime("%Y-%m-%d"))) counts += 1 start_date += timedelta(days=1) return counts, date_list
def tradedays(start, end): ''' 计算两个日期间的工作日 start:开始时间 end:结束时间 ''' from datetime import datetime, timedelta from chinese_calendar import is_holiday # 字符串格式日期的处理 if type(start) == str: start = datetime.strptime(start, '%Y-%m-%d').date() if type(end) == str: end = datetime.strptime(end, '%Y-%m-%d').date() # 开始日期大,颠倒开始日期和结束日期 if start > end: start, end = end, start counts = 0 while True: if start > end: break if is_holiday(start) or start.weekday() == 5 or start.weekday() == 6: start += timedelta(days=1) continue counts += 1 start += timedelta(days=1) return counts
def process_query(name): print('process query:', name) if name == 'train': train = pd.read_csv(data_path + 'train_queries.csv') else: train = pd.read_csv(data_path + 'test_queries.csv') train['o_lon'] = train.o.apply(lambda x: float(x.split(',')[0])) train['o_lat'] = train.o.apply(lambda x: float(x.split(',')[1])) train['d_lon'] = train.d.apply(lambda x: float(x.split(',')[0])) train['d_lat'] = train.d.apply(lambda x: float(x.split(',')[1])) train['o_i'] = (train['o_lon'] - lon_min) // precision train['o_j'] = (train['o_lat'] - lat_min) // precision train['d_i'] = (train['d_lon'] - lon_min) // precision train['d_j'] = (train['d_lat'] - lat_min) // precision num_i = np.ceil((lon_max - lon_min) / precision) num_j = np.ceil((lat_max - lat_min) / precision) train['o_grid'] = train['o_i'] * num_j + train['o_j'] train['d_grid'] = train['d_i'] * num_j + train['d_j'] train['req_time'] = pd.to_datetime(train['req_time']) train['hod'] = train['req_time'].dt.hour train['dow'] = train['req_time'].dt.dayofweek + 1 train['holiday'] = train.req_time.apply(lambda x: is_holiday(x) * 1) train['holiday'][(train.dow == 6) | (train.dow == 7)] = 1 if name == 'train': train.to_csv(save_path + 'train_od_date.csv', index=False) else: train.to_csv(save_path + 'test_od_date.csv', index=False) train.drop(['o', 'd', 'req_time'], axis=1, inplace=True) return train
def get_pro_features(self, ds_pt_list, hours, weather_dict): holiday = is_holiday(ds_pt_list[0].time)*1 day = ds_pt_list[0].time.day hour = {'hour': np.bincount(hours).max()} # find most frequent hours as hour of the trajectory weather = {'weather': weather_dict[(day, hour['hour'])]} features = self.one_hot(hour) + self.one_hot(weather) + [holiday] return features
def gen_feas(all_data_df): # 添加特征 all_data_df["year"] = all_data_df["date_t"].dt.year all_data_df["month"] = all_data_df["date_t"].dt.month all_data_df["day"] = all_data_df["date_t"].dt.day # mon -> 0, sun->6 all_data_df["dow"] = all_data_df["date_t"].dt.dayofweek all_data_df["doy"] = all_data_df["date_t"].dt.dayofyear all_data_df["is_sa"] = all_data_df["dow"] == 5 all_data_df["is_su"] = all_data_df["dow"] == 6 BC.log("Add more features : year, month, day, dow, doy, is_sa, is_su") # 处理节假日 all_data_df["is_holiday"] = [ 1 if cc.is_holiday(x.date()) else 0 for x in all_data_df.date_t ] all_data_df["is_holiday"] = (all_data_df["is_sa"] != 1) & ( all_data_df["is_su"] != 1) & (all_data_df["is_holiday"] == 1) all_data_df["is_holiday"] = [1 if x else 0 for x in all_data_df.is_holiday] all_data_df["is_WDA"] = [ 1 if cc.is_workday(x.date()) else 0 for x in all_data_df.date_t ] all_data_df["is_WDA"] = ( (all_data_df["is_sa"] == 1) | (all_data_df["is_su"] == 1)) & (all_data_df["is_WDA"] == 1) BC.log("Add more features : is_holiday, is_WDA") # 对比了下,单独划分没有明显效果,根据数据特征分组 all_data_df["is_B12367"] = all_data_df["brand"].isin([1, 2, 3, 6, 7]) all_data_df["is_B410"] = all_data_df["brand"].isin([4, 10]) all_data_df["is_B5"] = all_data_df["brand"].isin([5]) all_data_df["is_B8"] = all_data_df["brand"].isin([8]) all_data_df["is_B9"] = all_data_df["brand"].isin([9]) BC.log("Add more features : is_B12367, is_B410, is_B5, is_B8,is_B9") # 外部数据 # 增加车牌量 # 处理最后一个月的值 filename = "../Input/car_sale_volume.csv" car_sales_df = pd.read_csv(filename) car_sales_df = car_sales_df[["sale_quantity", "year", "month"]] BC.log("Read cas sales : " + filename) # 最后一个月的数据处理 df_ = pd.Series([car_sales_df[-3:]["sale_quantity"].mean(), 2017, 11], index=["sale_quantity", "year", "month"]).to_frame() car_sales_df = pd.concat([car_sales_df, df_.T]) # car_sales_df.reset_index(inplace=True) all_data_df = pd.merge(all_data_df, car_sales_df, on=["year", "month"]) all_data_df.drop("day_of_week", inplace=True, axis=1) all_data_df["is_Jan"] = all_data_df["month"] == 1 all_data_df["is_Feb"] = all_data_df["month"] == 2 BC.log("Add more features : is_Jan, is_Feb") filename = BC.outputFolder + "all_data.csv" all_data_df.to_csv(filename) BC.log("Write all_data_df to " + filename) return all_data_df
def test_chinesecalendar(): """ 测试判断chinese_calendar这个库CC :return: """ test_day = datetime.date(2019, 10, 1) print("是工作日:%s" % is_workday(test_day)) print("是假日:%s" % is_holiday(test_day))
def __get_datetime_range(cls, current, offset): ''' 获得时间的上下限,目标是找到一个日期,该日期是有休息日变成工作日的界限 offset为负数时,则反过来 :param current:当前日期 :param offset:偏移量 :return: ''' while True: another_day = cls.__get_offset_date(current, offset) current_is_holiday = cc.is_holiday(current) another_day_is_holiday = cc.is_holiday(another_day) if not another_day_is_holiday and current_is_holiday: return current else: current = another_day before_day = cls.__get_offset_date(current, offset)
def is_holidays(date): ''' 判断是否为节假日 ''' Y = date.year M = date.month D = date.day april_last = datetime.date(Y, M, D) return is_holiday(april_last)
def send_set_time(): now = datetime.datetime.now() set_time = [ '10:00', '10:30', '11:00', '11:30', '13:30', '14:00', '14:30', '15:00' ] time1_str = datetime.datetime.strftime(now, '%Y-%m-%d %H:%M:%S')[-8:-3] if time1_str in set_time and is_holiday(now) == False: return True else: return False
def test_workdays(self): dates = [ datetime.date(year=2017, month=5, day=27), datetime.date(year=2017, month=6, day=5), datetime.date(year=2017, month=10, day=31), datetime.date(year=2018, month=5, day=10), ] for date in dates: self.assertFalse(is_holiday(date)) self.assertTrue(is_workday(date))
def add_holiday_info(cls, vol: pd.DataFrame): ''' 为数据添加是否为节假日的信息 :param vol: 原始的数据 :return: 添加了'whether_holiday_filter'列,1表示这一天是节假日,为0则表示不是节假日 ''' vol = copy.deepcopy(vol) vol[Columns.Is_holiday] = pd.to_datetime( vol[Columns.Date]).apply(lambda t: { True: 1, False: 0 }[cc.is_holiday(t)]) return vol
def alarm_clock(): date = datetime.date.today() localtime = time.localtime(time.time()) if is_holiday(date): text = holiday_msg(localtime) elif is_workday(date): text = workday_msg(localtime) if text is not None: msg_body = {"msgtype": "text", "text": {"content": text}, "at": { "isAtAll": True} } ret = requests.post(ding_url, json = msg_body)
def deal_TestInput(testFilePath): data = pd.read_csv(testFilePath) deal_data = data.copy(deep=True) deal_data['date'] = pd.to_datetime(deal_data['date']) deal_data['week'] = deal_data['date'].dt.dayofweek deal_data['month'] = deal_data['date'].dt.month deal_data['year'] = deal_data['date'].dt.year deal_data['is_workday'] = deal_data['date'].apply( lambda x: 1 if is_workday(pd.to_datetime(x)) else 0) deal_data['is_holiday'] = deal_data['date'].apply( lambda x: 1 if is_holiday(pd.to_datetime(x)) else 0) deal_data.drop(['date'], axis=1, inplace=True) deal_data.to_csv('data/dealed_testInput.csv', index=False)
def test_same_code_as_readme(self): import datetime # Check if 2018-04-30 is holiday in China from chinese_calendar import is_workday, is_holiday april_last = datetime.date(2018, 4, 30) self.assertFalse(is_workday(april_last)) self.assertTrue(is_holiday(april_last)) # or check and get the holiday name import chinese_calendar as calendar # with different import style on_holiday, holiday_name = calendar.get_holiday_detail(april_last) self.assertTrue(on_holiday) self.assertEqual(calendar.Holiday.labour_day.value, holiday_name)
def holiday_and_dev(self, line): """ 判断是否为假期和开发服务器,同时为True才返回holiday。 line为传进来的每一行日志。 :return: """ now = datetime.now() now2 = now + timedelta(hours=15) # 需要加15小时时差才能获取到服务器上最新log的日期 dev = ["devel", "beta"] for hostname in dev: try: # 判断是假期的同时满足是开发服务器 if is_holiday(now2) and hostname in line: return "holiday" except NotImplementedError: return "NotImplementedError"
def isHolidayOrWeekend(day): """ 判断是否是星期六 0-周一,1-周二.....5-周六,6-周日, 返回值是<int>类型 :param day: %Y%m%d :return: """ april_last = datetime.date(int(day[:4]), int(day[4:6]), int(day[6:])) if is_holiday(april_last): return True day_date = datetime.datetime.strptime(day, '%Y%m%d') day_week = day_date.weekday() # print('new_time:', new_time, ' day_week: ', day_week) if day_week == 5 or day_week == 6: return True return False
def getweekday(DataSet): dataset_test = DataSet dataset_test['is_weekend'] = dataset_test['day_of_week'].map( lambda x: 1 if x == 5 or x == 6 else 0) weekday_dummies = pd.get_dummies(dataset_test['day_of_week']) weekday_dummies.columns = [ 'weekday' + str(i) for i in range(weekday_dummies.shape[1]) ] dataset_test = pd.concat([dataset_test, weekday_dummies], axis=1) #新加的特征 dataset_test['is_holiday'] = dataset_test['date_received'].map( lambda x: 1 if is_holiday(x) else 0) dataset_test['is_workday'] = dataset_test['date_received'].map( lambda x: 1 if is_workday(x) else 0) return dataset_test
def __supplementary_days(cls, data: pd.DataFrame): '''补充时间,使得数据中的时间以完整的假期开始,以完整的假期结束''' # 对数据按照时间进行排序 df = copy.deepcopy(data) df.index = pd.to_datetime(df[Columns.Date]) del df[Columns.Date] df.sort_index(inplace=True) df.reset_index(inplace=True) upday = cls.__get_datetime_range(df.iloc[0][Columns.Date], -1) lowerday = cls.__get_datetime_range(df.iloc[-1][Columns.Date], 1) newdf = pd.DataFrame({Columns.Date: pd.date_range(upday, lowerday)}) result = pd.merge(newdf, df, how='left', on=[Columns.Date]) result[Columns.Is_holiday] = pd.to_datetime(result[Columns.Date])\ .apply(lambda t: {True:int(1),False:int(0)}[cc.is_holiday(t)]) if Columns.OutpatientVol in result.columns: result[Columns.OutpatientVol].fillna(0, inplace=True) return result
def test_holiday_and_dev(line): """ 判断是否为假期和开发服务器,同时为True才返回True。 :return: 布尔值 """ now = datetime.datetime.now() now2 = now + datetime.timedelta(hours=15) # 需要加15小时时差才能获取到服务器上最新log的日期 test_day = datetime.date( 2019, 10, 2) # 测试的时候修改这里, 分别测2019,10,2;2019,10,8;2020,10,2之类 dev = ["devel", "beta"] for hostname in dev: try: if is_holiday(test_day) and hostname in line: # 测试的时候使用 if is_holiday(test_day) and hostname in line: return "holiday" except NotImplementedError: return "NotImplementedError"
def clockIn(userId, clockTime): res = Record.query.filter( and_( Record.time.like('{}%'.format( clockTime.isoformat(' ').split()[0])), Record.user_id == userId)).order_by(Record.time).all() print('a1', res) # 判断当天是否已打卡两次 if len(res) > 1: return False data = Record(userId, clockTime) db.session.add(data) # 法定假日 if is_holiday(clockTime): if len(res) == 1: res = Record.query.filter( and_( Record.time.like('{}%'.format( clockTime.isoformat(' ').split()[0])), Record.user_id == userId)).order_by(Record.time).all() dura = (res[1].time - res[0].time).total_seconds() dura = format(dura / 3600., '.2f') data = Summary(res[0].user_id, res[0].time.year, res[0].time.month, res[0].time.day, dura) db.session.add(data) db.session.commit() return True else: # 工作日 # if clockoff, compute overtime duration hms = clockTime.time() if hms > datetime.time.fromisoformat('17:00:00'): assert len(res) == 1 res = Record.query.filter( and_( Record.time.like('{}%'.format( clockTime.isoformat(' ').split()[0])), Record.user_id == userId)).order_by(Record.time).all() print("dura: ", res) dura = computeDurationPerDay(res[0].time, res[1].time) data = Summary(res[0].user_id, res[1].time.year, res[1].time.month, res[1].time.day, dura) db.session.add(data) db.session.commit() return True
def add_overtime(e_id, time_len, ot_reason, ot_date): ''' 添加加班记录 Args: e_id : 员工编号 time_len:加班时长 ot_reason:加班理由 ot_date:加班日期 Returns: void ''' sign = is_holiday(ot_date) e = User.objects.get(e_id=e_id) add_ot = Overtime(ot_date=ot_date, time_length=time_len, o_reason=ot_reason, is_holiday=sign, o_emp=e) add_ot.save()
def build_date_fea_defult(df, date_col): """ 构建日期相关特征. 为一个DataFrame的日期列构造日期相关特征, 构建的特征有: ['year', 'month', 'day','dayofyear', 'dayofweek', 'daysinmonth', 'is_leap_year', 'is_in_lieu', 'is_holiday', 'is_workday']. Args: df: 输入的DataFrame. date_col: 日期列名,为一个字符串,需存在于df中,对应列可以是字符串格式或者日期格式. Returns: df: 构建完成的新的DataFrame. """ year, month, day, dayofyear, \ dayofweek, daysinmonth, is_leap_year, \ is_in_lieu, is_holiday, is_workday = create_empty_list(10) for t in df[date_col]: t = pd.to_datetime(t) year.append(t.year) month.append(t.month) day.append(t.day) dayofyear.append(t.dayofyear) dayofweek.append(t.dayofweek) daysinmonth.append(t.daysinmonth) is_leap_year.append(int(t.is_leap_year)) is_in_lieu.append(int(cc.is_in_lieu(t))) is_holiday.append(int(cc.is_holiday(t))) is_workday.append(int(cc.is_workday(t))) df['year'] = year df['month'] = month df['day'] = day df['dayofyear'] = dayofyear df['dayofweek'] = dayofweek df['daysinmonth'] = daysinmonth df['is_leap_year'] = is_leap_year df['is_in_lieu'] = is_in_lieu df['is_holiday'] = is_holiday df['is_workday'] = is_workday return df
def Get_external_dataset(start_time, stop_time): time_slots = [] time_slots_between = [] index_to_delete = [27] # 10min:91 20min:43 30min:27 num_days = (stop_time - start_time).days while start_time <= stop_time: time_slots.append(start_time) start_time = start_time + datetime.timedelta( minutes=30) # 10min 20min 30min for time in time_slots: hour = time.hour minute = time.minute if is_time_between(hour, minute): time_slots_between.append(time) for i in range(1, num_days + 1): index = 27 + i * 33 #10min:91 97 20min:43 49 30min:27 33 index_to_delete.append(index) time_slots_between = [ time_slots_between[i] for i in range(0, len(time_slots_between), 1) if i not in index_to_delete ] external_feature = [] for time in time_slots_between: day = time.day cut_time = '2015-1-' + str(day) temp_f = f[cut_time] diff = temp_f.index - time temp_f['diff'] = abs(diff) idx = temp_f['diff'].idxmin() row = temp_f.loc[idx] # 该时段的天气特征(数值 已min-max归一化) 6维 + 时间差(不用) dim_0 = int(is_workday(time)) dim_1 = int(is_holiday(time)) # 该时间段的节假日特征(one-hot编码) 2维 [是否工作日,是否节假日] feature = np.array( (dim_0, dim_1, row[0], row[1], row[2], row[3], row[4], row[5])) feature = tile(feature, (76, 1)) # 76个站点, 同一个时间external feature一样 external_feature.append(feature) external_feature = np.array(external_feature) return external_feature
def create_casual(casual_start, casual_end, casual_reason): ''' 经理创建临时加班 Args: casual_start:开始时间 casual_end:结束时间 casual_reason:加班理由 Returns: void ''' c_date = casual_start.date() sign = is_holiday(c_date) cas = Casual(c_reason=casual_reason, c_begin=casual_start, c_end=casual_end, is_holiday=sign) cas.save() emps = User.objects.all() for e in emps: emp_cas = Emp_Casual(casual=cas, emp=e) emp_cas.save()
def get_tradingday(start_day,end_day): start_week = start_day.isoweekday() days = [] end = False global number for day in range(6-start_week): t = start_day+timedelta(days=day) if t == end_day: end = True day = 6 - start_week - 1 if t.isoweekday() < 6: if not is_holiday(t): days.append(t) if day == 6 - start_week - 1: if days: result[str(number)] = days number = number + 1 if end: break next_weekday = t + timedelta(days=3) get_tradingday(next_weekday,end_day)
def test_same_code_as_readme_english(self): import datetime # Check if 2018-04-30 is holiday in China from chinese_calendar import is_holiday, is_workday april_last = datetime.date(2018, 4, 30) assert is_workday(april_last) is False assert is_holiday(april_last) is True # or check and get the holiday name import chinese_calendar as calendar # 也可以这样 import on_holiday, holiday_name = calendar.get_holiday_detail(april_last) assert on_holiday is True assert holiday_name == calendar.Holiday.labour_day.value # even check if a holiday is in lieu import chinese_calendar assert chinese_calendar.is_in_lieu(datetime.date(2006, 2, 1)) is False assert chinese_calendar.is_in_lieu(datetime.date(2006, 2, 2)) is True
def daily_check(): if is_holiday( datetime.datetime.now( datetime.timezone(datetime.timedelta(hours=8))).date()): logging.info('Today is holiday') else: run_after = random.randint(0, 540) logging.info('Auto Check in/out start in ' + str(run_after) + 'secs') time.sleep(run_after) try: uid, token = login() check(uid, token) except LoginException as e: logging.exception(e) emergence_trigger() except HTTPError as e: logging.exception(e) emergence_trigger() except CheckException as e: logging.exception(e) emergence_trigger()
def start_date_match(start_date, end_date, **kw): # 生成初始日期序列 rst_date = pd.DataFrame({"date": [], "count": []}) last_count = 0 for tryDate in pd.date_range(start=startDatein, end=endDatein): # print(x.dayofweek) #0-6 -> 1-7 train_by_date_zeromatch_df = kw["train_by_date_df"] if tryDate.dayofweek == 2: train_by_date_zeromatch_df[ "date_t"] = utils.Briefcase.gen_date_by_dows( tryDate, train_by_date_zeromatch_df["dow_diff"]) train_by_date_zeromatch_df["is_holiday"] = [ 1 if cc.is_holiday(x.date()) else 0 for x in train_by_date_zeromatch_df["date_t"] ] zeromatch_idx = train_by_date_zeromatch_df.loc[:, "day_of_week"].isin( [6, 7]) for x in range(len(zeromatch_idx)): if zeromatch_idx[x] == True: train_by_date_zeromatch_df.drop(x, inplace=True) curr_count = train_by_date_zeromatch_df["is_holiday"].sum() if last_count == 0: last_count = curr_count min_count = curr_count min_count = min(min_count, curr_count) last_count = curr_count print(tryDate.date(), last_count, min_count) df = pd.DataFrame({ "date": [tryDate], "count": [train_by_date_zeromatch_df["is_holiday"].sum()] }) rst_date = pd.concat([rst_date, df], ignore_index=True) rst_date.describe() return rst_date[rst_date["count"] == rst_date["count"].min()]
def test_same_code_as_readme_chinese(self): import datetime # 判断 2018年4月30号 是不是节假日 from chinese_calendar import is_holiday, is_workday april_last = datetime.date(2018, 4, 30) assert is_workday(april_last) is False assert is_holiday(april_last) is True # 或者在判断的同时,获取节日名 import chinese_calendar as calendar # 也可以这样 import on_holiday, holiday_name = calendar.get_holiday_detail(april_last) assert on_holiday is True assert holiday_name == calendar.Holiday.labour_day.value # 还能判断法定节假日是不是调休 import chinese_calendar assert chinese_calendar.is_in_lieu(datetime.date(2006, 2, 1)) is False assert chinese_calendar.is_in_lieu(datetime.date(2006, 2, 2)) is True