98 lines
4.2 KiB
Python
98 lines
4.2 KiB
Python
import logging
|
|
import pandas as pd
|
|
from sqlalchemy import create_engine
|
|
|
|
# 创建数据库引擎
|
|
engine235 = create_engine("postgresql+psycopg2://postgres:Goojia123456@192.168.0.235:5432/fangjia?client_encoding=utf8",
|
|
echo=False)
|
|
|
|
# 执行SQL查询获取区域价格数据
|
|
region_sql = "SELECT DISTINCT urbn_nm,gxda_admndiv_nm FROM price_stat.avm_district_price_copy1"
|
|
region_data = pd.read_sql(region_sql, engine235)
|
|
|
|
# 假设这是你的pgsql数据
|
|
all_region_data = pd.DataFrame()
|
|
pd.set_option('mode.chained_assignment', None)
|
|
|
|
def region_increase(region_data, all_region_data):
|
|
# 遍历区域列表,对每个区域进行查询
|
|
for index, row in region_data.iterrows():
|
|
city = row['urbn_nm']
|
|
region = row['gxda_admndiv_nm']
|
|
query = f"SELECT urbn_nm as 城市, gxda_admndiv_nm as 区域, gujiabds_clc_yrmo_val as 月份, gujia_bds_clc_avprc as 估家计算均价, gujia_bds_urbn_avprc as 估家城市均价 FROM price_stat.avm_district_price_copy1 WHERE urbn_nm = '{city}' AND gxda_admndiv_nm = '{region}'"
|
|
region_data = pd.read_sql(query, engine235)
|
|
|
|
if region_data.empty:
|
|
continue
|
|
|
|
# 复制一份原始的日期列,用于计算涨跌幅时保持原始数据不变
|
|
region_data['original_date'] = region_data['月份']
|
|
|
|
# 将日期列转换为日期类型
|
|
region_data['月份'] = pd.to_datetime(region_data['月份'], format='%Y%m')
|
|
|
|
# 对价格数据按照月份排序
|
|
region_data.sort_values(by='月份', inplace=True)
|
|
|
|
# 计算涨跌幅
|
|
region_data['涨跌幅'] = region_data['估家计算均价'].diff() / region_data['估家计算均价'].shift(1) * 100
|
|
|
|
# 计算涨跌幅,并保留两位小数,展示为百分比形式
|
|
price_change = region_data['估家计算均价'].pct_change() * 100
|
|
region_data['涨跌幅'] = (price_change[price_change != 0].fillna(0).round(2).astype(str) + '%').replace('0.0%', '')
|
|
|
|
# 将原始日期列替换为计算涨跌幅后的日期列
|
|
region_data['月份'] = region_data['original_date']
|
|
|
|
# 删除临时列
|
|
region_data.drop(columns=['original_date'], inplace=True)
|
|
|
|
# 将区域数据添加到总的数据中
|
|
all_region_data = pd.concat([all_region_data, region_data], ignore_index=True)
|
|
|
|
return all_region_data
|
|
|
|
def region_missing():
|
|
df = region_increase(region_data, all_region_data)
|
|
df['月份1'] = df['月份']
|
|
# 将日期字符串转换为日期时间对象
|
|
df["月份"] = pd.to_datetime(df["月份"], format="%Y%m")
|
|
|
|
# 获取当前时间
|
|
current_time = pd.Timestamp.today().replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
# 计算前一年到上月的时间范围
|
|
one_year_ago = current_time - pd.DateOffset(years=1, days=current_time.day - 1)
|
|
last_month = current_time - pd.DateOffset(months=1, days=current_time.day - 1)
|
|
|
|
# 创建一个包含所有可能月份的日期范围
|
|
all_months = pd.date_range(start=one_year_ago, end=last_month, freq="MS")
|
|
|
|
# 获取每个城市-区域组合缺失的月份
|
|
missing_months = []
|
|
for (city, region), group in df.groupby(["城市", "区域"]):
|
|
available_months = group["月份"]
|
|
missing = all_months.difference(available_months)
|
|
if len(missing) > 0:
|
|
missing_str = ",".join([m.strftime("%Y%m") for m in missing])
|
|
missing_months.append({"城市": city, "区域": region, "缺失月份": missing_str})
|
|
|
|
if missing_months:
|
|
# 创建DataFrame来存储结果
|
|
missing_df = pd.DataFrame(missing_months)
|
|
path = 'E:\\goojia\\file\\excel文件\\airflow\\missing_region.xlsx'
|
|
missing_df.to_excel(path, index=False)
|
|
|
|
# 如果区域在 missing_df 中,则将 df 中的该区域数据删除
|
|
for city, region in zip(missing_df['城市'], missing_df['区域']):
|
|
df = df[(df['城市'] != city) | (df['区域'] != region)]
|
|
|
|
df['月份'] = df['月份1']
|
|
# 删除月份1列
|
|
df.drop(columns=['月份1'], inplace=True)
|
|
output_path = 'E:\\goojia\\file\\excel文件\\airflow\\all_region_price_with_change.xlsx'
|
|
df.to_excel(output_path, index=False)
|
|
logging.info(f"区域涨跌幅数据已写入Excel文件:{output_path}")
|
|
|
|
region_missing()
|