Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -14,7 +14,7 @@ def sync(province):
|
|||||||
index="didata_company",
|
index="didata_company",
|
||||||
body={"index": {"refresh_interval": "-1"}}
|
body={"index": {"refresh_interval": "-1"}}
|
||||||
)
|
)
|
||||||
ids = DIDATA_DEV_ELASTIC_CLIENT.scan_by_sql_to_data_frame(f"select company_name from didata_company where province='{province}'")
|
ids = DIDATA_DEV_ELASTIC_CLIENT.scan_by_sql_to_data_frame(f"select company_name, committee from didata_company where province='{province}'")
|
||||||
if len(ids) > 0:
|
if len(ids) > 0:
|
||||||
company_ids = ids['_id'].to_list()
|
company_ids = ids['_id'].to_list()
|
||||||
else:
|
else:
|
||||||
@@ -92,6 +92,10 @@ def sync(province):
|
|||||||
body={"index": {"refresh_interval": "5s"}}
|
body={"index": {"refresh_interval": "5s"}}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
flag = False
|
||||||
for province in areautils.get_all_province():
|
for province in areautils.get_all_province():
|
||||||
|
if '山东省' == province:
|
||||||
|
flag = True
|
||||||
|
if flag:
|
||||||
sync(province)
|
sync(province)
|
||||||
@@ -66,3 +66,7 @@ def sync(province):
|
|||||||
index="didata_company",
|
index="didata_company",
|
||||||
body={"index": {"refresh_interval": "5s"}}
|
body={"index": {"refresh_interval": "5s"}}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
for province in areautils.get_all_province():
|
||||||
|
sync(province)
|
||||||
@@ -6,6 +6,7 @@ import re
|
|||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def sync():
|
||||||
"""
|
"""
|
||||||
对于没有围栏结果的,通过match_phrase匹配出来的开发区一定正确,如果有围栏出来的开发区,则企业经纬度不正确
|
对于没有围栏结果的,通过match_phrase匹配出来的开发区一定正确,如果有围栏出来的开发区,则企业经纬度不正确
|
||||||
"""
|
"""
|
||||||
@@ -128,3 +129,6 @@ for dev in dev_df.itertuples():
|
|||||||
count += len(actions)
|
count += len(actions)
|
||||||
print(f"已更新 {dev.name} 开发区" + str(count) + "记录")
|
print(f"已更新 {dev.name} 开发区" + str(count) + "记录")
|
||||||
actions.clear()
|
actions.clear()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sync()
|
||||||
@@ -14,7 +14,6 @@ last_day = last.strftime('%Y%m%d')
|
|||||||
|
|
||||||
log.info(f"系统当前时间 : {now}")
|
log.info(f"系统当前时间 : {now}")
|
||||||
|
|
||||||
|
|
||||||
def proc():
|
def proc():
|
||||||
# 区域维度,统计2个月内样本数据量
|
# 区域维度,统计2个月内样本数据量
|
||||||
es206_client = ElasticSearchClient(host='192.168.0.206')
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
||||||
@@ -100,10 +99,6 @@ def proc():
|
|||||||
air_file_name = path + '/' + file_name
|
air_file_name = path + '/' + file_name
|
||||||
return text, air_file_name, df_region_res, df_root_city_res, df_root_res, file_name, count_old, count_new
|
return text, air_file_name, df_region_res, df_root_city_res, df_root_res, file_name, count_old, count_new
|
||||||
|
|
||||||
|
|
||||||
text, air_file_name, df_region_res, df_root_city_res, df_root_res, file_name, count_old, count_new = proc()
|
|
||||||
|
|
||||||
|
|
||||||
def up_to_nextcloud():
|
def up_to_nextcloud():
|
||||||
"""
|
"""
|
||||||
上传统计信息至nextcloud
|
上传统计信息至nextcloud
|
||||||
@@ -116,7 +111,8 @@ def up_to_nextcloud():
|
|||||||
nxc = NextCloud(endpoint=NEXTCLOUD_URL, user=NEXTCLOUD_USERNAME, password=NEXTCLOUD_PASSWORD)
|
nxc = NextCloud(endpoint=NEXTCLOUD_URL, user=NEXTCLOUD_USERNAME, password=NEXTCLOUD_PASSWORD)
|
||||||
nxc.upload_file(air_file_name, nextcloud_file_name)
|
nxc.upload_file(air_file_name, nextcloud_file_name)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
text, air_file_name, df_region_res, df_root_city_res, df_root_res, file_name, count_old, count_new = proc()
|
||||||
if text != '':
|
if text != '':
|
||||||
with pd.ExcelWriter(air_file_name) as writer:
|
with pd.ExcelWriter(air_file_name) as writer:
|
||||||
if not df_region_res.empty:
|
if not df_region_res.empty:
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
"""
|
"""
|
||||||
写字楼基准价计算
|
写字楼基准价计算
|
||||||
"""
|
"""
|
||||||
from airflow.operators.bash import BashOperator
|
from airflow.operators.python import PythonOperator
|
||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
|
from office.rent.base_price import calc
|
||||||
|
|
||||||
from airflow import DAG
|
from airflow import DAG
|
||||||
|
|
||||||
@@ -14,9 +15,9 @@ with DAG(
|
|||||||
catchup=True,
|
catchup=True,
|
||||||
description="写字楼基准价计算"
|
description="写字楼基准价计算"
|
||||||
) as dag:
|
) as dag:
|
||||||
t1 = BashOperator(
|
t1 = PythonOperator(
|
||||||
task_id='base_price',
|
task_id='base_price',
|
||||||
bash_command='python /opt/airflow/dags/repo/dags/office/rent/base_price.py {{dag_run.conf["yearmonth"] if "yearmonth" in dag_run.conf}}',
|
python_callable=calc,
|
||||||
|
provide_context=True
|
||||||
)
|
)
|
||||||
t1
|
t1
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
写字楼基准价计算
|
写字楼基准价计算
|
||||||
"""
|
"""
|
||||||
from airflow.operators.bash import BashOperator
|
from airflow.operators.bash import BashOperator
|
||||||
|
from airflow.operators.python import PythonOperator
|
||||||
|
from office.sell.base_price import calc
|
||||||
|
from office.sell.fill_base_price import fill_base_price
|
||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
|
|
||||||
@@ -14,13 +17,15 @@ with DAG(
|
|||||||
catchup=True,
|
catchup=True,
|
||||||
description="写字楼出售基准价计算"
|
description="写字楼出售基准价计算"
|
||||||
) as dag:
|
) as dag:
|
||||||
t1 = BashOperator(
|
t1 = PythonOperator(
|
||||||
task_id='sell_base_price',
|
task_id='calc',
|
||||||
bash_command='python /opt/airflow/dags/repo/dags/office/sell/base_price.py {{dag_run.conf["yearmonth"] if "yearmonth" in dag_run.conf}}',
|
python_callable=calc,
|
||||||
|
provide_context=True
|
||||||
)
|
)
|
||||||
t2 = BashOperator(
|
t2 = PythonOperator(
|
||||||
task_id='fill_base_price',
|
task_id='fill_base_price',
|
||||||
bash_command='python /opt/airflow/dags/repo/dags/office/sell/fill_base_price.py {{dag_run.conf["yearmonth"] if "yearmonth" in dag_run.conf}}',
|
python_callable=fill_base_price,
|
||||||
|
provide_context=True
|
||||||
)
|
)
|
||||||
t1 >> t2
|
t1 >> t2
|
||||||
|
|
||||||
|
|||||||
+155
-32
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# 第五步:本身没有等级和类型的写字楼
|
# 第五步:本身没有等级和类型的写字楼
|
||||||
# 最近10个有样本写字楼的基准价的平均值
|
# 最近10个有样本写字楼的基准价的平均值
|
||||||
from goojia_utils import ElasticSearchClient, dateutils, MAIN_MONGO_CLIENT
|
from goojia_utils import ElasticSearchClient, dateutils, MAIN_MONGO_CLIENT, commutils
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pymongo import UpdateOne
|
from pymongo import UpdateOne
|
||||||
@@ -23,7 +23,111 @@ seaweed_df = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
|
|||||||
office_rent_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_rent_stat')
|
office_rent_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_rent_stat')
|
||||||
|
|
||||||
# 计算有样本写字楼的基准价 1. 取近一年,最近100个样本的中位值
|
# 计算有样本写字楼的基准价 1. 取近一年,最近100个样本的中位值
|
||||||
def calc(city, year_month):
|
|
||||||
|
def calc(**kwargs):
|
||||||
|
"""
|
||||||
|
写字楼基准价
|
||||||
|
"""
|
||||||
|
# 从 Airflow 的 dag_run.conf 获取参数
|
||||||
|
conf = kwargs.get('dag_run').conf if kwargs.get('dag_run') else {}
|
||||||
|
city = conf.get('city', '')
|
||||||
|
year_month = conf.get('year_month', '')
|
||||||
|
district = conf.get('district', '')
|
||||||
|
office_name = conf.get('name', '')
|
||||||
|
|
||||||
|
if office_name == '':
|
||||||
|
calcByCity(city, year_month)
|
||||||
|
else:
|
||||||
|
# 直接重算出最近一年的数据,保证一年有数据
|
||||||
|
calcByOffice(city, district, office_name)
|
||||||
|
|
||||||
|
def calcByOffice(city, district, office_name):
|
||||||
|
"""
|
||||||
|
写字楼价格重算
|
||||||
|
"""
|
||||||
|
query = {'cat': 'office', 'status': 0}
|
||||||
|
if pd.notna(city):
|
||||||
|
query['city'] = city
|
||||||
|
if pd.notna(district):
|
||||||
|
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, '_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 ('alias' in office) and (type(office['alias']) == list):
|
||||||
|
for alias_item in office['alias']:
|
||||||
|
names.append(alias_item)
|
||||||
|
names_str = "','".join(names)
|
||||||
|
names_str = "'" + names_str + "'"
|
||||||
|
|
||||||
|
# 只查询该小区的样本
|
||||||
|
year_time = dateutils.format_date(dateutils.datetime.datetime.now())
|
||||||
|
one_year_time = dateutils.format_date(dateutils.add_months(dateutils.datetime.datetime.now(), days=-365))
|
||||||
|
sql = f"select * from latest_seawater where cat='office_rent' and city='{city}' and region='{district}' and district_name in ({names_str}) and p_date > {one_year_time}"
|
||||||
|
sample = es.scan_by_sql_to_data_frame(sql)
|
||||||
|
|
||||||
|
location = {
|
||||||
|
"type": "Point",
|
||||||
|
"coordinates": [office['lng'] / math.pow(10, 10), office['lat'] / math.pow(10, 10)]
|
||||||
|
}
|
||||||
|
|
||||||
|
date_range = pd.date_range(start=one_year_time, end=year_time, freq='M')
|
||||||
|
for date in date_range:
|
||||||
|
curr_date = int(dateutils.format_date(date, fm='%Y%m'))
|
||||||
|
# 筛选出当前月的样本
|
||||||
|
if len(sample) > 0:
|
||||||
|
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)
|
||||||
|
# 核查当月写字楼是否存在,不存在取最后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):
|
||||||
|
avg_price_ret = office_rent_stat_conn.aggregate([
|
||||||
|
{
|
||||||
|
"$match": {
|
||||||
|
"cat": "office",
|
||||||
|
"city": city,
|
||||||
|
"district": district,
|
||||||
|
"name": office_name,
|
||||||
|
"date": { "$gte": curr_date - 3, "$lte": curr_date + 3 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$group": {
|
||||||
|
"_id": "$name",
|
||||||
|
"avg_price": { "$avg": "$avg_price" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
])
|
||||||
|
avg_prices = [doc["avg_price"] for doc in avg_price_ret]
|
||||||
|
# 计算平均值
|
||||||
|
average_price = np.mean(avg_prices) if len(avg_prices) > 0 else 0
|
||||||
|
new_avg_price = None
|
||||||
|
if average_price > 0:
|
||||||
|
new_avg_price = round(average_price, 2)
|
||||||
|
elif len(sample) > 0:
|
||||||
|
# 取样本的平均值
|
||||||
|
new_avg_price = round(sample['avg_price'].mean(), 2)
|
||||||
|
if pd.notna(new_avg_price):
|
||||||
|
action = UpdateOne(
|
||||||
|
{'cat': 'office', 'city': city, 'district': district, 'name': office_name, 'date': curr_date},
|
||||||
|
{'$set': {'city': city, 'cat': 'office', 'date': curr_date, 'district': district,
|
||||||
|
'name': office_name,
|
||||||
|
'avg_price': new_avg_price, 'estate_type2': office['estate_type2'],
|
||||||
|
'bed_kind': office['bed_kind'],
|
||||||
|
'location': location,
|
||||||
|
'calc_type': 5, 'updated_date': datetime.now()}},
|
||||||
|
upsert=True)
|
||||||
|
office_rent_stat_conn.bulk_write([action])
|
||||||
|
|
||||||
|
|
||||||
|
def calcByCity(city, year_month):
|
||||||
"""
|
"""
|
||||||
写字楼基准价
|
写字楼基准价
|
||||||
"""
|
"""
|
||||||
@@ -46,14 +150,20 @@ def calc(city, year_month):
|
|||||||
print("开始计算年" + str(year_month) + "写字楼基准价")
|
print("开始计算年" + str(year_month) + "写字楼基准价")
|
||||||
|
|
||||||
sample = es.scan_by_sql_to_data_frame(sql)
|
sample = es.scan_by_sql_to_data_frame(sql)
|
||||||
query = {'cat': 'office', 'status': 0}
|
office = getOffice(city)
|
||||||
if pd.notna(city):
|
|
||||||
query['city'] = city
|
|
||||||
office = pd.DataFrame(list(seaweed_df.find(query).batch_size(10000)))
|
|
||||||
office = office[['city', 'region', 'name', 'alias', 'estate_type2', 'bed_kind', 'lng', 'lat']]
|
|
||||||
office = office.explode('alias')
|
|
||||||
|
|
||||||
|
update_office(city, office, sample, 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
|
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(
|
sample_new['sample_avg_price'] = sample_new.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform(
|
||||||
'median')
|
'median')
|
||||||
sample_new['sample_count'] = sample_new.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform(
|
sample_new['sample_count'] = sample_new.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform(
|
||||||
@@ -64,15 +174,19 @@ def calc(city, year_month):
|
|||||||
sample_new = sample_new[['city', 'region', 'district_name', 'day_avg_price']]
|
sample_new = sample_new[['city', 'region', 'district_name', 'day_avg_price']]
|
||||||
sample_new = sample_new.rename(columns={'district_name': 'name'})
|
sample_new = sample_new.rename(columns={'district_name': 'name'})
|
||||||
sample_new['alias'] = sample_new['name']
|
sample_new['alias'] = sample_new['name']
|
||||||
|
|
||||||
# 使用主名与别名查找样本
|
# 使用主名与别名查找样本
|
||||||
merge_df1 = pd.merge(office, sample_new[['city', 'region', 'name', 'day_avg_price']], how='left',
|
merge_df1 = pd.merge(office, sample_new[['city', 'region', 'name', 'day_avg_price']], how='left',
|
||||||
on=['city', 'region', 'name'])
|
on=['city', 'region', 'name'])
|
||||||
|
if len(sample_new['alias'].unique()) > 0:
|
||||||
merge_df2 = pd.merge(office, sample_new[['city', 'region', 'alias', 'day_avg_price']], how='left',
|
merge_df2 = pd.merge(office, sample_new[['city', 'region', 'alias', 'day_avg_price']], how='left',
|
||||||
on=['city', 'region', 'alias'])
|
on=['city', 'region', 'alias'])
|
||||||
merge_df = pd.concat([merge_df1, merge_df2])
|
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[merge_df['day_avg_price'].notna()]
|
||||||
merge_df = merge_df.drop_duplicates(subset=['city', 'region', 'name'], keep='first')
|
merge_df = merge_df.drop_duplicates(subset=['city', 'region', 'name'], keep='first')
|
||||||
|
else:
|
||||||
|
merge_df = pd.DataFrame()
|
||||||
# 第一步:计算有样本写字楼的基准价
|
# 第一步:计算有样本写字楼的基准价
|
||||||
print("有样本数量:" + str(len(merge_df)))
|
print("有样本数量:" + str(len(merge_df)))
|
||||||
actions = list()
|
actions = list()
|
||||||
@@ -88,7 +202,8 @@ def calc(city, year_month):
|
|||||||
estate_type2 = item['estate_type2'] if pd.notna(item['estate_type2']) else None
|
estate_type2 = item['estate_type2'] if pd.notna(item['estate_type2']) else None
|
||||||
bed_kind = item['bed_kind'] if pd.notna(item['bed_kind']) else None
|
bed_kind = item['bed_kind'] if pd.notna(item['bed_kind']) else None
|
||||||
action = UpdateOne({'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
action = UpdateOne({'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
||||||
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region, 'name': name,
|
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
||||||
|
'name': name,
|
||||||
'avg_price': avg_price, 'estate_type2': estate_type2, 'bed_kind': bed_kind,
|
'avg_price': avg_price, 'estate_type2': estate_type2, 'bed_kind': bed_kind,
|
||||||
'location': location,
|
'location': location,
|
||||||
'calc_type': 1, 'updated_date': datetime.now()}},
|
'calc_type': 1, 'updated_date': datetime.now()}},
|
||||||
@@ -100,13 +215,14 @@ def calc(city, year_month):
|
|||||||
if len(actions) > 0:
|
if len(actions) > 0:
|
||||||
office_rent_stat_conn.bulk_write(actions)
|
office_rent_stat_conn.bulk_write(actions)
|
||||||
actions.clear()
|
actions.clear()
|
||||||
|
|
||||||
# 第二步 取周边3公里内同等级、同类型的最近10个有样本写字楼的基准价的平均值
|
# 第二步 取周边3公里内同等级、同类型的最近10个有样本写字楼的基准价的平均值
|
||||||
office_info = office[['city', 'region', 'name', 'lng', 'lat', 'estate_type2', 'bed_kind']].drop_duplicates(keep='first')
|
office_info = office[['city', 'region', 'name', 'lng', 'lat', 'estate_type2', 'bed_kind']].drop_duplicates(
|
||||||
|
keep='first')
|
||||||
combined = pd.concat([office_info, merge_df])
|
combined = pd.concat([office_info, merge_df])
|
||||||
diff_df = combined.drop_duplicates(subset=['city', 'region', 'name'], keep=False)
|
diff_df = combined.drop_duplicates(subset=['city', 'region', 'name'], keep=False)
|
||||||
actions = list()
|
actions = list()
|
||||||
for index, item in diff_df[(diff_df['estate_type2'].notna()) & (diff_df['bed_kind'].notna()) & (diff_df['lng'].notna()) ].iterrows():
|
for index, item in diff_df[
|
||||||
|
(diff_df['estate_type2'].notna()) & (diff_df['bed_kind'].notna()) & (diff_df['lng'].notna())].iterrows():
|
||||||
location = {
|
location = {
|
||||||
"type": "Point",
|
"type": "Point",
|
||||||
"coordinates": [item['lng'] / math.pow(10, 10), item['lat'] / math.pow(10, 10)]
|
"coordinates": [item['lng'] / math.pow(10, 10), item['lat'] / math.pow(10, 10)]
|
||||||
@@ -159,7 +275,6 @@ def calc(city, year_month):
|
|||||||
if len(actions) > 0:
|
if len(actions) > 0:
|
||||||
office_rent_stat_conn.bulk_write(actions)
|
office_rent_stat_conn.bulk_write(actions)
|
||||||
actions.clear()
|
actions.clear()
|
||||||
|
|
||||||
# 系数计算在Jupyter中
|
# 系数计算在Jupyter中
|
||||||
bed_kind_dict = {'甲级': 1.111888111888112, '丙级': 0.9143356643356645, '乙级': 1}
|
bed_kind_dict = {'甲级': 1.111888111888112, '丙级': 0.9143356643356645, '乙级': 1}
|
||||||
radio_dict = {'创业园区': 0.7799999999999999, '商住两用': 1.0466666666666666, '标准写字楼': 1}
|
radio_dict = {'创业园区': 0.7799999999999999, '商住两用': 1.0466666666666666, '标准写字楼': 1}
|
||||||
@@ -210,7 +325,8 @@ def calc(city, year_month):
|
|||||||
count += 1
|
count += 1
|
||||||
if count > 0:
|
if count > 0:
|
||||||
avg_price = round(sum_price / count, 2)
|
avg_price = round(sum_price / count, 2)
|
||||||
action = UpdateOne({'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
action = UpdateOne(
|
||||||
|
{'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
||||||
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
||||||
'name': name,
|
'name': name,
|
||||||
'avg_price': avg_price, 'estate_type2': estate_type2, 'bed_kind': None,
|
'avg_price': avg_price, 'estate_type2': estate_type2, 'bed_kind': None,
|
||||||
@@ -249,7 +365,8 @@ def calc(city, year_month):
|
|||||||
count += 1
|
count += 1
|
||||||
if count > 0:
|
if count > 0:
|
||||||
avg_price = round(sum_price / count, 2)
|
avg_price = round(sum_price / count, 2)
|
||||||
action = UpdateOne({'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
action = UpdateOne(
|
||||||
|
{'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
||||||
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
||||||
'name': name,
|
'name': name,
|
||||||
'avg_price': avg_price, 'estate_type2': None, 'bed_kind': bed_kind,
|
'avg_price': avg_price, 'estate_type2': None, 'bed_kind': bed_kind,
|
||||||
@@ -273,15 +390,13 @@ def calc(city, year_month):
|
|||||||
# 计算价格的写字楼
|
# 计算价格的写字楼
|
||||||
db_df = db_df.rename(columns={'district': 'region'})
|
db_df = db_df.rename(columns={'district': 'region'})
|
||||||
# 使用 merge 操作并结合 indicator 参数
|
# 使用 merge 操作并结合 indicator 参数
|
||||||
|
if len(db_df) > 0:
|
||||||
merged = office_info.merge(db_df[['city', 'region', 'name']], on=['city', 'region', 'name'], how='left',
|
merged = office_info.merge(db_df[['city', 'region', 'name']], on=['city', 'region', 'name'], how='left',
|
||||||
indicator=True)
|
indicator=True)
|
||||||
|
|
||||||
# 过滤出仅在 office_info 中存在的记录
|
# 过滤出仅在 office_info 中存在的记录
|
||||||
diff_df = merged[merged['_merge'] == 'left_only'].drop(columns=['_merge'])
|
diff_df = merged[merged['_merge'] == 'left_only'].drop(columns=['_merge'])
|
||||||
|
|
||||||
# 如果需要保持原始列顺序,可以重新排列列
|
# 如果需要保持原始列顺序,可以重新排列列
|
||||||
diff_df = diff_df[office_info.columns]
|
diff_df = diff_df[office_info.columns]
|
||||||
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
for index, item in diff_df.iterrows():
|
for index, item in diff_df.iterrows():
|
||||||
try:
|
try:
|
||||||
@@ -336,16 +451,24 @@ def calc(city, year_month):
|
|||||||
office_rent_stat_conn.bulk_write(actions)
|
office_rent_stat_conn.bulk_write(actions)
|
||||||
actions.clear()
|
actions.clear()
|
||||||
|
|
||||||
yearmonth = sys.argv[1] if len(sys.argv) > 1 else None
|
|
||||||
# for yearmonth in [
|
def getOffice(city, district, name):
|
||||||
# "202406",
|
"""
|
||||||
# "202407",
|
获取写字楼
|
||||||
# "202408",
|
"""
|
||||||
# "202409",
|
query = {'cat': 'office', 'status': 0}
|
||||||
# "202410",
|
if pd.notna(city):
|
||||||
# "202411",
|
query['city'] = city
|
||||||
# "202412",
|
if pd.notna(district):
|
||||||
# "202501"
|
query['region'] = district
|
||||||
# ]:
|
if pd.notna(city):
|
||||||
# calc(None, yearmonth)
|
query['name'] = name
|
||||||
calc(None, yearmonth)
|
office = pd.DataFrame(list(seaweed_df.find(query).batch_size(10000)))
|
||||||
|
office = office[['city', 'region', 'name', 'alias', 'estate_type2', 'bed_kind', 'lng', 'lat']]
|
||||||
|
office = office.explode('alias')
|
||||||
|
return office
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 测试脚本:直接运行时模拟 Airflow 的上下文
|
||||||
|
calc(dag_run=type('DagRun', (), {'conf': {'city': '北京', 'district': '朝阳', 'name': '中核华建大厦'}}))
|
||||||
+148
-38
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# 第五步:本身没有等级和类型的写字楼
|
# 第五步:本身没有等级和类型的写字楼
|
||||||
# 最近10个有样本写字楼的基准价的平均值
|
# 最近10个有样本写字楼的基准价的平均值
|
||||||
from goojia_utils import ElasticSearchClient, dateutils, MAIN_MONGO_CLIENT
|
from goojia_utils import ElasticSearchClient, dateutils, MAIN_MONGO_CLIENT, commutils
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pymongo import UpdateOne
|
from pymongo import UpdateOne
|
||||||
@@ -23,7 +23,107 @@ seaweed_df = MAIN_MONGO_CLIENT.get_collection('fangjia', 'seaweed')
|
|||||||
office_sell_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_sell_stat')
|
office_sell_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_sell_stat')
|
||||||
|
|
||||||
# 计算有样本写字楼的基准价 1. 取近一年,最近100个样本的中位值
|
# 计算有样本写字楼的基准价 1. 取近一年,最近100个样本的中位值
|
||||||
def calc(city, year_month):
|
|
||||||
|
def calc(**kwargs):
|
||||||
|
"""
|
||||||
|
写字楼基准价
|
||||||
|
"""
|
||||||
|
# 从 Airflow 的 dag_run.conf 获取参数
|
||||||
|
conf = kwargs.get('dag_run').conf if kwargs.get('dag_run') else {}
|
||||||
|
city = conf.get('city', '')
|
||||||
|
year_month = conf.get('year_month', '')
|
||||||
|
district = conf.get('district', '')
|
||||||
|
office_name = conf.get('name', '')
|
||||||
|
|
||||||
|
if pd.isna(office_name):
|
||||||
|
calcByCity(city, year_month)
|
||||||
|
else:
|
||||||
|
# 直接重算出最近一年的数据,保证一年有数据
|
||||||
|
calcByOffice(city, district, office_name)
|
||||||
|
|
||||||
|
def calcByOffice(city, district, office_name):
|
||||||
|
"""
|
||||||
|
写字楼价格重算
|
||||||
|
"""
|
||||||
|
query = {'cat': 'office', 'status': 0}
|
||||||
|
if pd.notna(city):
|
||||||
|
query['city'] = city
|
||||||
|
if pd.notna(district):
|
||||||
|
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})
|
||||||
|
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 ('alias' in office) and (type(office['alias']) == list):
|
||||||
|
for alias_item in office['alias']:
|
||||||
|
names.append(alias_item)
|
||||||
|
names_str = "','".join(names)
|
||||||
|
names_str = "'" + names_str + "'"
|
||||||
|
|
||||||
|
# 只查询该小区的样本
|
||||||
|
year_time = dateutils.format_date(dateutils.datetime.datetime.now())
|
||||||
|
one_year_time = dateutils.format_date(dateutils.add_months(dateutils.datetime.datetime.now(), days=-365))
|
||||||
|
sql = f"select * from latest_seawater where cat='office_sell' and city='{city}' and region='{district}' and district_name in ({names_str}) and p_date > {one_year_time}"
|
||||||
|
sample = es.scan_by_sql_to_data_frame(sql)
|
||||||
|
|
||||||
|
location = {
|
||||||
|
"type": "Point",
|
||||||
|
"coordinates": [office['lng'] / math.pow(10, 10), office['lat'] / math.pow(10, 10)]
|
||||||
|
}
|
||||||
|
|
||||||
|
date_range = pd.date_range(start=one_year_time, end=year_time, freq='M')
|
||||||
|
for date in date_range:
|
||||||
|
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)
|
||||||
|
# 核查当月写字楼是否存在,不存在取最后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):
|
||||||
|
avg_price_ret = office_sell_stat_conn.aggregate([
|
||||||
|
{
|
||||||
|
"$match": {
|
||||||
|
"cat": "office",
|
||||||
|
"city": city,
|
||||||
|
"district": district,
|
||||||
|
"name": office_name,
|
||||||
|
"date": { "$gte": curr_date - 3, "$lte": curr_date + 3 }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$group": {
|
||||||
|
"_id": "$name",
|
||||||
|
"avg_price": { "$avg": "$avg_price" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
])
|
||||||
|
avg_prices = [doc["avg_price"] for doc in avg_price_ret]
|
||||||
|
# 计算平均值
|
||||||
|
average_price = np.mean(avg_prices) if len(avg_prices) > 0 else 0
|
||||||
|
if average_price > 0:
|
||||||
|
new_avg_price = int(average_price)
|
||||||
|
else:
|
||||||
|
# 取样本的平均值
|
||||||
|
new_avg_price = int(sample['avg_price'].mean())
|
||||||
|
if pd.notna(new_avg_price):
|
||||||
|
action = UpdateOne(
|
||||||
|
{'cat': 'office', 'city': city, 'district': district, 'name': office_name, 'date': curr_date},
|
||||||
|
{'$set': {'city': city, 'cat': 'office', 'date': curr_date, 'district': district,
|
||||||
|
'name': office_name,
|
||||||
|
'avg_price': new_avg_price, 'estate_type2': office['estate_type2'],
|
||||||
|
'bed_kind': office['bed_kind'],
|
||||||
|
'location': location,
|
||||||
|
'calc_type': 5, 'updated_date': datetime.now()}},
|
||||||
|
upsert=True)
|
||||||
|
office_sell_stat_conn.bulk_write([action])
|
||||||
|
|
||||||
|
|
||||||
|
def calcByCity(city, year_month):
|
||||||
"""
|
"""
|
||||||
写字楼基准价
|
写字楼基准价
|
||||||
"""
|
"""
|
||||||
@@ -46,14 +146,19 @@ def calc(city, year_month):
|
|||||||
print("开始计算年" + str(year_month) + "写字楼基准价")
|
print("开始计算年" + str(year_month) + "写字楼基准价")
|
||||||
|
|
||||||
sample = es.scan_by_sql_to_data_frame(sql)
|
sample = es.scan_by_sql_to_data_frame(sql)
|
||||||
query = {'cat': 'office', 'status': 0}
|
office = getOffice(city)
|
||||||
if pd.notna(city):
|
|
||||||
query['city'] = city
|
|
||||||
office = pd.DataFrame(list(seaweed_df.find(query).batch_size(10000)))
|
|
||||||
office = office[['city', 'region', 'name', 'alias', 'estate_type2', 'bed_kind', 'lng', 'lat']]
|
|
||||||
office = office.explode('alias')
|
|
||||||
|
|
||||||
|
update_office(city, office, sample, 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
|
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(
|
sample_new['sample_avg_price'] = sample_new.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform(
|
||||||
'median')
|
'median')
|
||||||
sample_new['sample_count'] = sample_new.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform(
|
sample_new['sample_count'] = sample_new.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform(
|
||||||
@@ -64,19 +169,20 @@ def calc(city, year_month):
|
|||||||
sample_new = sample_new[['city', 'region', 'district_name', 'day_avg_price']]
|
sample_new = sample_new[['city', 'region', 'district_name', 'day_avg_price']]
|
||||||
sample_new = sample_new.rename(columns={'district_name': 'name'})
|
sample_new = sample_new.rename(columns={'district_name': 'name'})
|
||||||
sample_new['alias'] = sample_new['name']
|
sample_new['alias'] = sample_new['name']
|
||||||
|
|
||||||
# 使用主名与别名查找样本
|
# 使用主名与别名查找样本
|
||||||
merge_df1 = pd.merge(office, sample_new[['city', 'region', 'name', 'day_avg_price']], how='left',
|
merge_df1 = pd.merge(office, sample_new[['city', 'region', 'name', 'day_avg_price']], how='left',
|
||||||
on=['city', 'region', 'name'])
|
on=['city', 'region', 'name'])
|
||||||
|
if len(sample_new['alias'].unique()) > 0:
|
||||||
merge_df2 = pd.merge(office, sample_new[['city', 'region', 'alias', 'day_avg_price']], how='left',
|
merge_df2 = pd.merge(office, sample_new[['city', 'region', 'alias', 'day_avg_price']], how='left',
|
||||||
on=['city', 'region', 'alias'])
|
on=['city', 'region', 'alias'])
|
||||||
merge_df = pd.concat([merge_df1, merge_df2])
|
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[merge_df['day_avg_price'].notna()]
|
||||||
merge_df = merge_df.drop_duplicates(subset=['city', 'region', 'name'], keep='first')
|
merge_df = merge_df.drop_duplicates(subset=['city', 'region', 'name'], keep='first')
|
||||||
# 第一步:计算有样本写字楼的基准价
|
# 第一步:计算有样本写字楼的基准价
|
||||||
print("有样本数量:" + str(len(merge_df)))
|
print("有样本数量:" + str(len(merge_df)))
|
||||||
actions = list()
|
actions = list()
|
||||||
count = 0
|
|
||||||
for index, item in merge_df.iterrows():
|
for index, item in merge_df.iterrows():
|
||||||
city = item['city']
|
city = item['city']
|
||||||
region = item['region']
|
region = item['region']
|
||||||
@@ -89,7 +195,8 @@ def calc(city, year_month):
|
|||||||
estate_type2 = item['estate_type2'] if pd.notna(item['estate_type2']) else None
|
estate_type2 = item['estate_type2'] if pd.notna(item['estate_type2']) else None
|
||||||
bed_kind = item['bed_kind'] if pd.notna(item['bed_kind']) else None
|
bed_kind = item['bed_kind'] if pd.notna(item['bed_kind']) else None
|
||||||
action = UpdateOne({'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
action = UpdateOne({'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
||||||
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region, 'name': name,
|
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
||||||
|
'name': name,
|
||||||
'avg_price': avg_price, 'estate_type2': estate_type2, 'bed_kind': bed_kind,
|
'avg_price': avg_price, 'estate_type2': estate_type2, 'bed_kind': bed_kind,
|
||||||
'location': location,
|
'location': location,
|
||||||
'calc_type': 1, 'updated_date': datetime.now()}},
|
'calc_type': 1, 'updated_date': datetime.now()}},
|
||||||
@@ -97,21 +204,18 @@ def calc(city, year_month):
|
|||||||
actions.append(action)
|
actions.append(action)
|
||||||
if len(actions) == 1000:
|
if len(actions) == 1000:
|
||||||
office_sell_stat_conn.bulk_write(actions)
|
office_sell_stat_conn.bulk_write(actions)
|
||||||
count += len(actions)
|
|
||||||
print("已计算" + str(count) + "条数据")
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
if len(actions) > 0:
|
if len(actions) > 0:
|
||||||
office_sell_stat_conn.bulk_write(actions)
|
office_sell_stat_conn.bulk_write(actions)
|
||||||
count += len(actions)
|
|
||||||
print("已计算" + str(count) + "条数据")
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
|
|
||||||
# 第二步 取周边3公里内同等级、同类型的最近10个有样本写字楼的基准价的平均值
|
# 第二步 取周边3公里内同等级、同类型的最近10个有样本写字楼的基准价的平均值
|
||||||
office_info = office[['city', 'region', 'name', 'lng', 'lat', 'estate_type2', 'bed_kind']].drop_duplicates(keep='first')
|
office_info = office[['city', 'region', 'name', 'lng', 'lat', 'estate_type2', 'bed_kind']].drop_duplicates(
|
||||||
|
keep='first')
|
||||||
combined = pd.concat([office_info, merge_df])
|
combined = pd.concat([office_info, merge_df])
|
||||||
diff_df = combined.drop_duplicates(subset=['city', 'region', 'name'], keep=False)
|
diff_df = combined.drop_duplicates(subset=['city', 'region', 'name'], keep=False)
|
||||||
actions = list()
|
actions = list()
|
||||||
for index, item in diff_df[(diff_df['estate_type2'].notna()) & (diff_df['bed_kind'].notna()) & (diff_df['lng'].notna()) ].iterrows():
|
for index, item in diff_df[
|
||||||
|
(diff_df['estate_type2'].notna()) & (diff_df['bed_kind'].notna()) & (diff_df['lng'].notna())].iterrows():
|
||||||
location = {
|
location = {
|
||||||
"type": "Point",
|
"type": "Point",
|
||||||
"coordinates": [item['lng'] / math.pow(10, 10), item['lat'] / math.pow(10, 10)]
|
"coordinates": [item['lng'] / math.pow(10, 10), item['lat'] / math.pow(10, 10)]
|
||||||
@@ -160,15 +264,10 @@ def calc(city, year_month):
|
|||||||
actions.append(action)
|
actions.append(action)
|
||||||
if len(actions) == 1000:
|
if len(actions) == 1000:
|
||||||
office_sell_stat_conn.bulk_write(actions)
|
office_sell_stat_conn.bulk_write(actions)
|
||||||
count += len(actions)
|
|
||||||
print("已计算" + str(count) + "条数据")
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
if len(actions) > 0:
|
if len(actions) > 0:
|
||||||
office_sell_stat_conn.bulk_write(actions)
|
office_sell_stat_conn.bulk_write(actions)
|
||||||
count += len(actions)
|
|
||||||
print("已计算" + str(count) + "条数据")
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
|
|
||||||
# 系数计算在Jupyter中
|
# 系数计算在Jupyter中
|
||||||
bed_kind_dict = {'甲级': 1.111888111888112, '丙级': 0.9143356643356645, '乙级': 1}
|
bed_kind_dict = {'甲级': 1.111888111888112, '丙级': 0.9143356643356645, '乙级': 1}
|
||||||
radio_dict = {'创业园区': 0.7799999999999999, '商住两用': 1.0466666666666666, '标准写字楼': 1}
|
radio_dict = {'创业园区': 0.7799999999999999, '商住两用': 1.0466666666666666, '标准写字楼': 1}
|
||||||
@@ -219,7 +318,8 @@ def calc(city, year_month):
|
|||||||
count += 1
|
count += 1
|
||||||
if count > 0:
|
if count > 0:
|
||||||
avg_price = int(sum_price / count)
|
avg_price = int(sum_price / count)
|
||||||
action = UpdateOne({'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
action = UpdateOne(
|
||||||
|
{'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
||||||
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
||||||
'name': name,
|
'name': name,
|
||||||
'avg_price': avg_price, 'estate_type2': estate_type2, 'bed_kind': None,
|
'avg_price': avg_price, 'estate_type2': estate_type2, 'bed_kind': None,
|
||||||
@@ -258,7 +358,8 @@ def calc(city, year_month):
|
|||||||
count += 1
|
count += 1
|
||||||
if count > 0:
|
if count > 0:
|
||||||
avg_price = int(sum_price / count)
|
avg_price = int(sum_price / count)
|
||||||
action = UpdateOne({'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
action = UpdateOne(
|
||||||
|
{'cat': 'office', 'city': city, 'district': region, 'name': name, 'date': year_month},
|
||||||
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
{'$set': {'city': city, 'cat': 'office', 'date': year_month, 'district': region,
|
||||||
'name': name,
|
'name': name,
|
||||||
'avg_price': avg_price, 'estate_type2': None, 'bed_kind': bed_kind,
|
'avg_price': avg_price, 'estate_type2': None, 'bed_kind': bed_kind,
|
||||||
@@ -268,13 +369,9 @@ def calc(city, year_month):
|
|||||||
actions.append(action)
|
actions.append(action)
|
||||||
if len(actions) == 1000:
|
if len(actions) == 1000:
|
||||||
office_sell_stat_conn.bulk_write(actions)
|
office_sell_stat_conn.bulk_write(actions)
|
||||||
count += len(actions)
|
|
||||||
print("已计算" + str(count) + "条数据")
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
if len(actions) > 0:
|
if len(actions) > 0:
|
||||||
office_sell_stat_conn.bulk_write(actions)
|
office_sell_stat_conn.bulk_write(actions)
|
||||||
count += len(actions)
|
|
||||||
print("已计算" + str(count) + "条数据")
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
# 第五步
|
# 第五步
|
||||||
query = {'cat': 'office', 'date': year_month, 'calc_type': {'$in': [1, 2, 3]}}
|
query = {'cat': 'office', 'date': year_month, 'calc_type': {'$in': [1, 2, 3]}}
|
||||||
@@ -286,15 +383,13 @@ def calc(city, year_month):
|
|||||||
# 计算价格的写字楼
|
# 计算价格的写字楼
|
||||||
db_df = db_df.rename(columns={'district': 'region'})
|
db_df = db_df.rename(columns={'district': 'region'})
|
||||||
# 使用 merge 操作并结合 indicator 参数
|
# 使用 merge 操作并结合 indicator 参数
|
||||||
|
if len(db_df) > 0:
|
||||||
merged = office_info.merge(db_df[['city', 'region', 'name']], on=['city', 'region', 'name'], how='left',
|
merged = office_info.merge(db_df[['city', 'region', 'name']], on=['city', 'region', 'name'], how='left',
|
||||||
indicator=True)
|
indicator=True)
|
||||||
|
|
||||||
# 过滤出仅在 office_info 中存在的记录
|
# 过滤出仅在 office_info 中存在的记录
|
||||||
diff_df = merged[merged['_merge'] == 'left_only'].drop(columns=['_merge'])
|
diff_df = merged[merged['_merge'] == 'left_only'].drop(columns=['_merge'])
|
||||||
|
|
||||||
# 如果需要保持原始列顺序,可以重新排列列
|
# 如果需要保持原始列顺序,可以重新排列列
|
||||||
diff_df = diff_df[office_info.columns]
|
diff_df = diff_df[office_info.columns]
|
||||||
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
for index, item in diff_df.iterrows():
|
for index, item in diff_df.iterrows():
|
||||||
try:
|
try:
|
||||||
@@ -344,14 +439,29 @@ def calc(city, year_month):
|
|||||||
actions.append(action)
|
actions.append(action)
|
||||||
if len(actions) == 1000:
|
if len(actions) == 1000:
|
||||||
office_sell_stat_conn.bulk_write(actions)
|
office_sell_stat_conn.bulk_write(actions)
|
||||||
count += len(actions)
|
|
||||||
print("已计算" + str(count) + "条数据")
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
if len(actions) > 0:
|
if len(actions) > 0:
|
||||||
office_sell_stat_conn.bulk_write(actions)
|
office_sell_stat_conn.bulk_write(actions)
|
||||||
count += len(actions)
|
|
||||||
print("已计算" + str(count) + "条数据")
|
|
||||||
actions.clear()
|
actions.clear()
|
||||||
|
|
||||||
yearmonth = sys.argv[1] if len(sys.argv) > 1 else None
|
|
||||||
calc(None, yearmonth)
|
def getOffice(city, district, name):
|
||||||
|
"""
|
||||||
|
获取写字楼
|
||||||
|
"""
|
||||||
|
query = {'cat': 'office', 'status': 0}
|
||||||
|
if pd.notna(city):
|
||||||
|
query['city'] = city
|
||||||
|
if pd.notna(district):
|
||||||
|
query['region'] = district
|
||||||
|
if pd.notna(city):
|
||||||
|
query['name'] = name
|
||||||
|
office = pd.DataFrame(list(seaweed_df.find(query).batch_size(10000)))
|
||||||
|
office = office[['city', 'region', 'name', 'alias', 'estate_type2', 'bed_kind', 'lng', 'lat']]
|
||||||
|
office = office.explode('alias')
|
||||||
|
return office
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 测试脚本:直接运行时模拟 Airflow 的上下文
|
||||||
|
calc(dag_run=type('DagRun', (), {'conf': {'city': '上海', 'district': '静安', 'name': '凯迪克大厦'}}))
|
||||||
@@ -11,15 +11,37 @@ office_sell_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office
|
|||||||
office_rent_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_rent_stat')
|
office_rent_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_rent_stat')
|
||||||
|
|
||||||
|
|
||||||
def calc(city, year_month):
|
def fill_base_price(**kwargs):
|
||||||
query = {'date': year_month, 'cat': 'office'}
|
"""
|
||||||
if pd.notna(city):
|
写字楼基准价
|
||||||
|
"""
|
||||||
|
# 从 Airflow 的 dag_run.conf 获取参数
|
||||||
|
conf = kwargs.get('dag_run').conf if kwargs.get('dag_run') else {}
|
||||||
|
city = conf.get('city', '')
|
||||||
|
year_month = conf.get('year_month', '')
|
||||||
|
district = conf.get('district', '')
|
||||||
|
office_name = conf.get('name', '')
|
||||||
|
|
||||||
|
query = {'cat': 'office'}
|
||||||
|
if year_month != '':
|
||||||
|
query['date'] = year_month
|
||||||
|
if city != '':
|
||||||
query['city'] = city
|
query['city'] = city
|
||||||
|
if district != '' and office_name != '':
|
||||||
|
query['district'] = district
|
||||||
|
query['name'] = office_name
|
||||||
office_sell = pd.DataFrame(list(office_sell_stat_conn.find(query)))
|
office_sell = pd.DataFrame(list(office_sell_stat_conn.find(query)))
|
||||||
office_rent = pd.DataFrame(list(office_rent_stat_conn.find(query)))
|
office_rent = pd.DataFrame(list(office_rent_stat_conn.find(query)))
|
||||||
|
|
||||||
if len(office_rent) <= 0:
|
if len(office_rent) <= 0:
|
||||||
return
|
return
|
||||||
|
if year_month != '':
|
||||||
|
calc_by_yearmonth(office_sell, office_rent, year_month)
|
||||||
|
else:
|
||||||
|
# 对于单个重算的不进行补充,已经自己补充过了
|
||||||
|
pass
|
||||||
|
|
||||||
|
def calc_by_yearmonth(office_sell, office_rent, year_month):
|
||||||
office_sell_df = office_sell[['city', 'district', 'name', 'avg_price']]
|
office_sell_df = office_sell[['city', 'district', 'name', 'avg_price']]
|
||||||
office_sell_df = office_sell_df.rename(columns={'avg_price': 'sell_price'})
|
office_sell_df = office_sell_df.rename(columns={'avg_price': 'sell_price'})
|
||||||
office_rent_df = office_rent[['city', 'district', 'name', 'avg_price', 'estate_type2', 'bed_kind', 'location']]
|
office_rent_df = office_rent[['city', 'district', 'name', 'avg_price', 'estate_type2', 'bed_kind', 'location']]
|
||||||
@@ -43,13 +65,13 @@ def calc(city, year_month):
|
|||||||
填充存在租金,不存在售价的数据
|
填充存在租金,不存在售价的数据
|
||||||
"""
|
"""
|
||||||
if item['city'] + "_" + item['district'] in district_radio.keys():
|
if item['city'] + "_" + item['district'] in district_radio.keys():
|
||||||
item['sell_price'] = round(district_radio[item['city'] + "_" + item['district']] * item['rent_price'] * 30 * 12, 0)
|
item['sell_price'] = round(
|
||||||
|
district_radio[item['city'] + "_" + item['district']] * item['rent_price'] * 30 * 12, 0)
|
||||||
elif item['city'] in city_radio.keys():
|
elif item['city'] in city_radio.keys():
|
||||||
item['sell_price'] = round(city_radio[item['city']] * item['rent_price'] * 30 * 12, 0)
|
item['sell_price'] = round(city_radio[item['city']] * item['rent_price'] * 30 * 12, 0)
|
||||||
return item
|
return item
|
||||||
|
|
||||||
if len(merge2_df) == 0:
|
if len(merge2_df) == 0:
|
||||||
print(city + "无数据补充")
|
|
||||||
return
|
return
|
||||||
merge2_df = threads.process_dataframe_multithread(merge2_df, deal, 10)
|
merge2_df = threads.process_dataframe_multithread(merge2_df, deal, 10)
|
||||||
merge2_df = merge2_df[merge2_df['sell_price'].notna()]
|
merge2_df = merge2_df[merge2_df['sell_price'].notna()]
|
||||||
@@ -83,16 +105,7 @@ def calc(city, year_month):
|
|||||||
print("已处理{}条数据".format(count))
|
print("已处理{}条数据".format(count))
|
||||||
print("处理完毕")
|
print("处理完毕")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
yearmonth = sys.argv[1] if len(sys.argv) > 1 else ''
|
# 测试脚本:直接运行时模拟 Airflow 的上下文
|
||||||
|
fill_base_price(dag_run=type('DagRun', (), {'conf': {'city': '上海', 'district': '静安', 'name': '凯迪克大厦'}}))
|
||||||
if yearmonth == '':
|
|
||||||
date = dateutils.add_months(datetime.today(), months=-1)
|
|
||||||
yearmonth = int(dateutils.format_date(date, '%Y%m'))
|
|
||||||
try:
|
|
||||||
if type(yearmonth) is str:
|
|
||||||
yearmonth = int(yearmonth)
|
|
||||||
calc(None, yearmonth)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ es_client = ElasticSearchClient(host='192.168.0.206')
|
|||||||
office_sell_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_sell_stat')
|
office_sell_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_sell_stat')
|
||||||
office_rent_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_rent_stat')
|
office_rent_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'office_rent_stat')
|
||||||
|
|
||||||
|
|
||||||
def calc(city, year_month):
|
def calc(city, year_month):
|
||||||
print(f"开始处理{city}, {year_month}")
|
print(f"开始处理{city}, {year_month}")
|
||||||
query = {'date': year_month, 'cat': 'office'}
|
query = {'date': year_month, 'cat': 'office'}
|
||||||
|
|||||||
Reference in New Issue
Block a user