履约表日期调整

This commit is contained in:
z66
2025-12-02 17:34:47 +08:00
parent 6c316e6c61
commit 502b3d4e4e
12 changed files with 232 additions and 56 deletions
+6 -22
View File
@@ -100,28 +100,12 @@ class ImportPerformanceData:
time_columns = ['saas开户时间', '服务期起始时间', '下单支付成功时间', '操作时间',
"下单支付成功日期", "服务期结束时间"]
for col in tqdm(time_columns):
if col in tqdm(new_df.columns): # 安全检查列是否存在
try:
# 1. 转换为datetime(自动推断格式,处理无效值为NaT)
new_df[col] = pd.to_datetime(new_df[col], errors='coerce', utc=False)
# 2. 时区转换(仅对有效日期操作)
mask = new_df[col].notna() # 只处理非空值
if mask.any(): # 如果有有效日期才转换
# 本地化为北京时间,然后转换为UTC
new_df.loc[mask, col + '_utc'] = (
new_df.loc[mask, col]
.dt.tz_localize('Asia/Shanghai', ambiguous='infer', nonexistent='shift_forward')
.dt.tz_convert('UTC')
.dt.strftime('%Y-%m-%dT%H:%M:%SZ')
)
else:
new_df[col + '_utc'] = pd.NA # 全部为空时保持一致性
except Exception as e:
print(f"处理列 {col} 时出错: {str(e)}")
new_df[col + '_utc'] = pd.NA # 出错时设为NA
new_df[time_columns] = new_df[time_columns].apply(
lambda col: pd.to_datetime(col, errors='coerce')
.dt.tz_localize('Asia/Shanghai') # 假设原时间是北京时间
.dt.tz_convert('UTC') # 转为 UTC
.dt.strftime('%Y-%m-%d %H:%M:%S') # 格式化为字符串(无时区标记)
)
return new_df