2

首先,我创建了一个 Pandas 数据框,其中包含当天的 1 分钟 OHLCV 历史数据,例如:

                    open    high    low close   volume
date                    
2019-10-30 07:55:00 3034.00 3034.25 3033.75 3034.00 209
2019-10-30 07:56:00 3034.00 3034.25 3033.75 3034.00 315
2019-10-30 07:57:00 3034.25 3034.50 3033.75 3034.25 432
2019-10-30 07:58:00 3034.00 3034.25 3033.75 3033.75 329
2019-10-30 07:59:00 3034.00 3034.25 3033.75 3034.00 231

下一刻,我使用侦听器类订阅实时报价并将其重新采样到持续更新的 1 分钟 OHLCV 数据数据帧中,例如:

                    open    high    low close   volume
date                    
2019-10-30 07:59:00 3033.75 3034.00 3033.75 3034.00 35
2019-10-30 08:00:00 3033.75 3034.25 3033.25 3033.75 117
2019-10-30 08:01:00 3033.75 3034.00 3033.75 3034.00 78

如何合并这两者,以便将每一行新的实时数据(重新采样为 1 分钟行的刻度)附加到历史数据中?另一个问题是最后一分钟的历史数据和第一分钟的实时数据之间的重叠——这些需要结合起来。

4

2 回答 2

0
# isolate the new indexes, (present in live_df but not in hist_df)
new_locs = ~live_df.index.isin(hist_df.index)

# append just the new entries in the live_df
new_df = hist_df.append(live_df.loc[new_locs])

如果你的历史 df 增长得特别长,这可能会随着时间的推移而变慢。如果您保持数据框按升序排序,则可以简化new_locs检查以仅查看最近的几行。和.iloc()

于 2019-10-30T16:32:09.333 回答
-1

首次使用:

for i in live_df.index:
    if i in historical_df.index:
        new_live_df = live_df.drop(i)

这样,第一行实时数据就被删除了,因为它已经在历史数据中了。

然后使用:

df_total = historical_df.append(new_live_df)
于 2019-10-30T15:44:16.447 回答