【问题标题】:pandas - finding most recent (but previous) date in a second reference dataframepandas - 在第二个参考数据框中查找最近(但以前)的日期
【发布时间】:2020-06-16 21:12:26
【问题描述】:

我有两个数据框,其中一个我想在另一个中找到最接近(以前)的日期。

如果日期匹配,那么我需要取上一个日期

df_main包含参考信息

对于df_sample,我想在df_main 中查找Time 以获得最近(但之前)的条目。我可以使用 method='ffill' 来做到这一点,但是 Time 字段的日期是当天返回的同一天 - 我希望它返回前一个 - 基本上是 < 而不是 <=。

在我的示例df_res 中,我希望closest_val 列包含[ "n/a", 90, 90, 280, 280, 280]

import pandas as pd

dsample = {'Index': [1, 2, 3, 4, 5, 6],
     'Time': ["2020-06-01", "2020-06-02", "2020-06-03", "2020-06-04" ,"2020-06-05" ,"2020-06-06"],
     'Pred': [100, -200, 300, -400 , -500, 600]
    }

dmain = {'Index': [1, 2, 3],
     'Time': ["2020-06-01", "2020-06-03","2020-06-06"],
     'Actual': [90, 280, 650]
    }

def find_closest(x, df2):
    df_res = df2.iloc[df2.index.get_loc(x['Time'], method='ffill')]
    x['closest_time'] = df_res['Time']
    x['closest_val'] = df_res['Actual']
    return x

df_sample = pd.DataFrame(data=dsample)
df_main = pd.DataFrame(data=dmain)

df_sample = df_sample.set_index(pd.DatetimeIndex(df_sample['Time']))
df_main = df_main.set_index(pd.DatetimeIndex(df_main['Time']))
df_res = df_sample.apply(find_closest, df2=df_main ,axis=1)



【问题讨论】:

  • 您可以将所需的输出作为数据框提供吗?从您的帖子中,我了解到 df_res 不是您想要的。
  • 使用 pd.merge_asof 并设置 allow_exact_matches=False

标签: python pandas dataframe datetime


【解决方案1】:

使用pd.merge_asof(确保“时间”确实是日期时间):

pd.merge_asof(dsample, dmain, left_on="Time", right_on="Time", allow_exact_matches=False)

输出是:

   Index_x       Time  Pred  Index_y  Actual
0        1 2020-06-01   100      NaN     NaN
1        2 2020-06-02  -200      1.0    90.0
2        3 2020-06-03   300      1.0    90.0
3        4 2020-06-04  -400      2.0   280.0
4        5 2020-06-05  -500      2.0   280.0
5        6 2020-06-06   600      2.0   280.0

【讨论】:

    【解决方案2】:

    IIUC,我们可以对您的两个数据框进行笛卡尔积,然后过滤掉完全匹配的内容,然后应用一些逻辑来找出最接近的日期。

    最后,我们会将您的精确匹配和非精确匹配加入到最终数据帧中。

    s = pd.merge(
        df_sample.assign(key="var1"),
        df_main.assign(key="var1").rename(columns={"Time": "TimeDelta"}).drop("Index", 1),
        on="key",
        how="outer",
    ).drop("key", 1)
    
    
    extact_matches = s[s['Time'].eq(s['TimeDelta'])]
    non_exact_matches_cart = s[~s['Time'].isin(extact_matches['Time'])]
    
    
    non_exact_matches = non_exact_matches_cart.assign(
        delta=(non_exact_matches_cart["Time"] - non_exact_matches_cart["TimeDelta"])
        / np.timedelta64(1, "D") 
    ).query("delta >= 0").sort_values(["Time", "delta"]).drop_duplicates(
        "Time", keep="first"
    ).drop('delta',1)
    

    需要大量考虑上述变量,但基本上我们是在寻找时间差异,消除未来的任何差异,并删除保持过去最接近日期的值。

    df = pd.concat([extact_matches, non_exact_matches], axis=0).sort_values("Time").rename(
        columns={"TimeDelta": "closest_time", "Actual": "closest val"}
    )
    
    
    print(df)
    
        Index       Time  Pred closest_time  closest val
    0       1 2020-06-01   100   2020-06-01           90
    3       2 2020-06-02  -200   2020-06-01           90
    7       3 2020-06-03   300   2020-06-03          280
    10      4 2020-06-04  -400   2020-06-03          280
    13      5 2020-06-05  -500   2020-06-03          280
    17      6 2020-06-06   600   2020-06-06          650
    

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 2020-11-13
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      相关资源
      最近更新 更多