python - 合并 pandas 中的历史和实时股票价格数据

标签 python pandas resampling stock ohlc

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

                    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 分钟行)附加到历史数据中?另一个问题是最后一分钟的历史数据和第一分钟的实时数据之间的重叠 - 这些需要合并。

最佳答案

# 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()

关于python - 合并 pandas 中的历史和实时股票价格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58628799/

相关文章:

python - Tensorflow - 带条件的掩模张量元素

分配给命名组的 Python 正则表达式

python - 是否可以选择特定的网络接口(interface)在 Python 中传输数据?

python - 每小时对有间隙的时间序列数据重新采样

python - 通过python更新Mysql数据库出错

python - 从 Dataframe 的一行中获取数据子集

python - 从 pandas 数据框中获取符号

python - 为什么 iloc 使用 [] 而不是 ()?

Python:使用反向求和运算进行上采样

ffmpeg - 将格式为 8Khz `mulaw` 的 Twilio <Stream> 保存到文件中