You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用对应月份normal值填补日粒度时间序列DataFrame缺失数据

实现方案(基于Python Pandas)

步骤1:数据预处理

首先确保日度时序数据的索引为datetime类型,方便提取所属月份:

import pandas as pd

# 假设日度数据存储在变量 df_daily 中,基准值表存储在变量 df_monthly 中
# 转换日度数据的日期索引为datetime格式,适配你提供的日/月/年日期格式
df_daily.index = pd.to_datetime(df_daily.index, format='%d/%m/%Y')

步骤2:关联月度基准值

给日度数据新增临时月份列,基于月份匹配对应行的全字段基准值:

# 提取每行日期对应的月份
df_daily['month'] = df_daily.index.month
# 基于月份匹配生成和日度数据同结构的填补基准表
fill_base = pd.DataFrame(df_daily['month'].map(df_monthly.to_dict('index')).tolist(), index=df_daily.index)

步骤3:填补缺失值

用匹配好的基准值填补日度数据中的空值,最后删除临时月份列:

# 对应位置空值自动用基准值填充
df_daily = df_daily.fillna(fill_base)
# 删除临时生成的month列
df_daily = df_daily.drop(columns=['month'])

注意事项

  • 需确保df_daily和df_monthly的字段名(A/B/C/D/E)完全一致,才能实现自动对应列填补
  • 若日期格式有变化,需调整pd.to_datetime中的format参数适配你的日期规则

内容的提问来源于stack exchange,提问作者spcol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 04:03:02