在时间序列中插入缺失日期的最简单方法?

2024-10-16 22:25:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在excel中有一些过去20年左右的股市数据,其中包含假期和周末的缺口。我希望对这些缺失的日期进行插值,以获得这些天的大致股票指数。
Sample of excel data

我已经使用pandas将这两列读入Python,并将它们分配给各自的变量。检测日期中的间隔并对其进行插值的最佳方法是什么


Tags: 数据方法pandas间隔excel股市插值缺口
1条回答
网友
1楼 · 发布于 2024-10-16 22:25:22

熊猫有专门针对这种情况的方法:

df.interpolate() # will fill in based on the linear average of the before and after 
df.fillna(method='ffill') #  forward fill
df.fillna(method='bfill') #  backward fill

相关问题 更多 >