diff --git a/dags/office/rent/base_price.py b/dags/office/rent/base_price.py index 4a4d258..1f84650 100644 --- a/dags/office/rent/base_price.py +++ b/dags/office/rent/base_price.py @@ -52,10 +52,14 @@ def calcByOffice(city, district, office_name): query['region'] = district if pd.notna(city): query['name'] = office_name - office = seaweed_df.find_one(query, {'city': 1, 'region': 1, 'name': 1, 'alias': 1, 'lng': 1, 'lat': 1, 'estate_type2': 1, 'bed_kind': 1}) + office = seaweed_df.find_one(query, {'city': 1, 'region': 1, 'name': 1, 'alias': 1, 'lng': 1, 'lat': 1, 'estate_type2': 1, 'bed_kind': 1, '_id': 0}) + if 'estate_type2' not in office: + office['estate_type2'] = None + if 'bed_kind' not in office: + office['bed_kind'] = None names = list() names.append(office['name']) - if type(office['alias']) == list: + if ('alias' in office) and (type(office['alias']) == list): for alias_item in office['alias']: names.append(alias_item) names_str = "','".join(names) @@ -80,7 +84,7 @@ def calcByOffice(city, district, office_name): curr_sample = sample[(sample['p_date']>=int(str(curr_date)+'01')) & (sample['p_date']<=int(str(curr_date)+'31'))] else: curr_sample = pd.DataFrame(columns=['city', 'region', 'district_name', 'avg_price']) - update_office(city, pd.DataFrame(office), curr_sample, curr_date) + update_office(city, pd.DataFrame([office]), curr_sample, curr_date) # 核查当月写字楼是否存在,不存在取最后6个月的平均值 office_db = office_rent_stat_conn.find_one({'cat': 'office', 'city': city, 'district': district, 'name': office_name, 'date': curr_date}) if pd.isna(office_db): @@ -152,7 +156,13 @@ def calcByCity(city, year_month): def update_office(city, office, sample, year_month): + if 'bed_kind' not in office.columns: + office['bed_kind'] = None + if 'estate_type2' not in office.columns: + office['estate_type2'] = None sample_new = sample + if 'alias' not in sample_new.columns: + sample_new['alias'] = None if len(sample_new) > 0: sample_new['sample_avg_price'] = sample_new.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform( 'median') @@ -167,9 +177,12 @@ def update_office(city, office, sample, year_month): # 使用主名与别名查找样本 merge_df1 = pd.merge(office, sample_new[['city', 'region', 'name', 'day_avg_price']], how='left', on=['city', 'region', 'name']) - merge_df2 = pd.merge(office, sample_new[['city', 'region', 'alias', 'day_avg_price']], how='left', - on=['city', 'region', 'alias']) - merge_df = pd.concat([merge_df1, merge_df2]) + if len(sample_new['alias'].unique()) > 0: + merge_df2 = pd.merge(office, sample_new[['city', 'region', 'alias', 'day_avg_price']], how='left', + on=['city', 'region', 'alias']) + merge_df = pd.concat([merge_df1, merge_df2]) + else: + merge_df = merge_df1.copy() merge_df = merge_df[merge_df['day_avg_price'].notna()] merge_df = merge_df.drop_duplicates(subset=['city', 'region', 'name'], keep='first') else: @@ -458,4 +471,4 @@ def getOffice(city, district, name): if __name__ == "__main__": # 测试脚本:直接运行时模拟 Airflow 的上下文 - calc(dag_run=type('DagRun', (), {'conf': {'city': '福州', 'district': '晋安', 'name': '筑源大厦'}})) \ No newline at end of file + calc(dag_run=type('DagRun', (), {'conf': {'city': '北京', 'district': '朝阳', 'name': '中核华建大厦'}})) \ No newline at end of file diff --git a/dags/office/sell/base_price.py b/dags/office/sell/base_price.py index fff8628..d2c7718 100644 --- a/dags/office/sell/base_price.py +++ b/dags/office/sell/base_price.py @@ -53,9 +53,13 @@ def calcByOffice(city, district, office_name): if pd.notna(city): query['name'] = office_name office = seaweed_df.find_one(query, {'city': 1, 'region': 1, 'name': 1, 'alias': 1, 'lng': 1, 'lat': 1, 'estate_type2': 1, 'bed_kind': 1}) + if 'estate_type2' not in office: + office['estate_type2'] = None + if 'bed_kind' not in office: + office['bed_kind'] = None names = list() names.append(office['name']) - if type(office['alias']) == list: + if ('alias' in office) and (type(office['alias']) == list): for alias_item in office['alias']: names.append(alias_item) names_str = "','".join(names) @@ -77,7 +81,7 @@ def calcByOffice(city, district, office_name): curr_date = int(dateutils.format_date(date, fm='%Y%m')) # 筛选出当前月的样本 curr_sample = sample[(sample['p_date']>=int(str(curr_date)+'01')) & (sample['p_date']<=int(str(curr_date)+'31'))] - update_office(city, pd.DataFrame(office), curr_sample, curr_date) + update_office(city, pd.DataFrame([office]), curr_sample, curr_date) # 核查当月写字楼是否存在,不存在取最后6个月的平均值 office_db = office_sell_stat_conn.find_one({'cat': 'office', 'city': city, 'district': district, 'name': office_name, 'date': curr_date}) if pd.isna(office_db): @@ -148,7 +152,13 @@ def calcByCity(city, year_month): def update_office(city, office, sample, year_month): + if 'bed_kind' not in office.columns: + office['bed_kind'] = None + if 'estate_type2' not in office.columns: + office['estate_type2'] = None sample_new = sample + if 'alias' not in sample_new.columns: + sample_new['alias'] = None sample_new['sample_avg_price'] = sample_new.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform( 'median') sample_new['sample_count'] = sample_new.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform( @@ -162,9 +172,12 @@ def update_office(city, office, sample, year_month): # 使用主名与别名查找样本 merge_df1 = pd.merge(office, sample_new[['city', 'region', 'name', 'day_avg_price']], how='left', on=['city', 'region', 'name']) - merge_df2 = pd.merge(office, sample_new[['city', 'region', 'alias', 'day_avg_price']], how='left', - on=['city', 'region', 'alias']) - merge_df = pd.concat([merge_df1, merge_df2]) + if len(sample_new['alias'].unique()) > 0: + merge_df2 = pd.merge(office, sample_new[['city', 'region', 'alias', 'day_avg_price']], how='left', + on=['city', 'region', 'alias']) + merge_df = pd.concat([merge_df1, merge_df2]) + else: + merge_df = merge_df1.copy() merge_df = merge_df[merge_df['day_avg_price'].notna()] merge_df = merge_df.drop_duplicates(subset=['city', 'region', 'name'], keep='first') # 第一步:计算有样本写字楼的基准价