【问题标题】:Interpolate a series missing values based on another series根据另一个系列插入一个系列缺失值
【发布时间】:2021-11-06 02:51:25
【问题描述】:

我有这些数据:

{
"col1" : [1,np.nan,np.nan,np.nan,np.nan,8],
"col2" : [2,np.nan,np.nan,np.nan,np.nan,2]
"col_macro" : [3,4,7,7,13,18]
}

两个数据列都是时间序列。 col1col2 列是微观数据,col_macro 是宏观数据 我想使用col_macro 用插值替换缺失值。这意味着它们之间的变化率和冲击应该相似。

还有什么方法可以设置限制吗?例如,如果两端的值相似,我希望插值严格增加、减少或不变?

【问题讨论】:

    标签: pandas scipy time-series data-science interpolation


    【解决方案1】:

    创建数据框

    df=pd.DataFrame({
    "col1" : [1,np.nan,np.nan,np.nan,np.nan,8],
    "col2" : [2,np.nan,np.nan,np.nan,np.nan,2],
    "col_macro" : [3,4,7,7,13,18]
    })
    

    找到 col3 中的连续差异,使用 combine_first 传递到右列。因为它的连续差,做一个扩展和

    df['col1'] = df['col1'].combine_first(df['col_macro'].diff()).expanding(1).sum()
    df['col2'] = df['col1'].combine_first(df['col_macro'].diff()).expanding(1).sum()
    

    【讨论】:

    • 谢谢!它就像一个魅力!但我认为第二行有错字。它使用 col1 作为 col2。
    猜你喜欢
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多