fix(office): filter abnormal rent sample prices and fix city variable shadowing in base_price

This commit is contained in:
王文艳
2026-10-19 10:46:15 +08:00
parent fb8f6400be
commit 0d65af4be7
3 changed files with 18 additions and 10 deletions
+6 -5
View File
@@ -69,7 +69,7 @@ def calcByOffice(city, district, office_name):
# 只查询该小区的样本
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}"
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} and avg_price>=30 and avg_price<=450"
sample = es.scan_by_sql_to_data_frame(sql)
location = {
@@ -114,7 +114,7 @@ def calcByOffice(city, district, office_name):
new_avg_price = round(average_price, 2)
elif len(sample) > 0:
# 取样本的平均值
new_avg_price = round(sample['avg_price'].mean(), 2)
new_avg_price = round((sample['avg_price'] / 30).mean(), 2)
if pd.notna(new_avg_price):
action = UpdateOne(
{'cat': 'office', 'city': city, 'district': district, 'name': office_name, 'date': curr_date},
@@ -190,7 +190,7 @@ def calcByCity(city, year_month):
"""
写字楼基准价
"""
sql = "select * from latest_seawater where cat='office_rent' "
sql = "select * from latest_seawater where cat='office_rent' and avg_price>=30 and avg_price<=450 "
if pd.notna(city):
sql += "and city='" + city + "' "
if pd.notna(year_month):
@@ -215,6 +215,7 @@ def calcByCity(city, year_month):
def update_office(city, office, sample, year_month):
target_city = city
if 'bed_kind' not in office.columns:
office['bed_kind'] = None
if 'estate_type2' not in office.columns:
@@ -443,8 +444,8 @@ def update_office(city, office, sample, year_month):
actions.clear()
# 第五步
query = {'cat': 'office', 'date': year_month, 'calc_type': {'$in': [1, 2, 3]}}
if pd.notna(city):
query['city'] = city
if pd.notna(target_city) and target_city != '':
query['city'] = target_city
db_df = pd.DataFrame(list(office_rent_stat_conn.find(query)))
office_info = office[['city', 'region', 'name', 'lng', 'lat', 'estate_type2', 'bed_kind']].drop_duplicates(
keep='first')
+9 -3
View File
@@ -18,7 +18,7 @@ def calc(city, year_month):
raise Exception('年月格式错误yyyyMM')
office = es_client.scan_by_sql_to_data_frame(
"select * from latest_seawater where cat='office_rent' and city='" + city + "' and p_date>=" + str(
year_month) + "01 and p_date<=" + str(year_month) + "31 ")
year_month) + "01 and p_date<=" + str(year_month) + "31 and avg_price>=30 and avg_price<=450 ")
if len(office) > 0:
office['day_avg_price'] = office['avg_price'] / 30
# 计算总价的Z-score
@@ -42,7 +42,10 @@ def calc(city, year_month):
dateutils.format_date(dateutils.add_months(dateutils.parse_date(str(year_month), '%Y%m'), months=-1), '%Y%m'))
if pd.isna(city_price):
# 处理均价为空情况,用上月均价替代
stat = office_rent_stat_conn.find_one({'city': city, 'cat': 'city', 'date': int(last_month)})
stat = office_rent_stat_conn.find_one(
{'city': city, 'cat': 'city', 'date': {'$lte': int(last_month)}, 'avg_price': {'$lte': 15, '$gt': 0}},
sort=[('date', -1)]
)
if pd.isna(stat):
return False
last_month_city_price = stat['avg_price']
@@ -64,7 +67,10 @@ def calc(city, year_month):
avg_price = region_avg_prices[region]
else:
# 处理均价为空情况,用上月均价替代
district_stat = office_rent_stat_conn.find_one({'city': city, 'cat': 'district', 'date': int(last_month), 'district': region})
district_stat = office_rent_stat_conn.find_one(
{'city': city, 'cat': 'district', 'date': {'$lte': int(last_month)}, 'district': region, 'avg_price': {'$lte': 15, '$gt': 0}},
sort=[('date', -1)]
)
if pd.isna(district_stat):
continue
last_month_region_price = district_stat['avg_price']
+3 -2
View File
@@ -217,6 +217,7 @@ def calcByCity(city, year_month):
def update_office(city, office, sample, year_month):
target_city = city
if 'bed_kind' not in office.columns:
office['bed_kind'] = None
if 'estate_type2' not in office.columns:
@@ -446,8 +447,8 @@ def update_office(city, office, sample, year_month):
actions.clear()
# 第五步
query = {'cat': 'office', 'date': year_month, 'calc_type': {'$in': [1, 2, 3]}}
if pd.notna(city):
query['city'] = city
if pd.notna(target_city) and target_city != '':
query['city'] = target_city
db_df = pd.DataFrame(list(office_sell_stat_conn.find(query)))
office_info = office[['city', 'region', 'name', 'lng', 'lat', 'estate_type2', 'bed_kind']].drop_duplicates(
keep='first')