python - 将大型数据帧插值到稀疏、不规则的索引上

标签 python pandas interpolation

我有一个数据帧,其中包含以 30 分钟间隔采样的几年数据(来自连续水质传感器的 7 个参数),并且我有另一个数据帧,其中包含数百个随机时间点的数据,其中一个分钟精度。我想找到这 7 个参数在几百个随机时间点的插值。

以下是这些数据框的几行内容:

print df1    
                    Temp  SpCond   Sal  DO_pct  DO_mgl  Depth   pH  Turb
2002-07-16 14:00:00  26.0   45.31  29.3    71.6     4.9   0.95  7.9    -5
2002-07-16 14:30:00  25.9   45.22  29.2    70.4     4.9   0.98  7.9    -6
2002-07-16 15:00:00  26.0   44.92  29.0    76.2     5.3   1.02  7.9    -6
2002-07-16 15:30:00  26.0   45.06  29.1    77.9     5.4   1.06  7.9    -5
2002-07-16 16:00:00  25.9   45.23  29.2    67.0     4.6   1.11  7.8    -6
2002-07-16 16:30:00  25.9   45.33  29.3    72.9     5.0   1.17  7.9    -6
2002-07-16 17:00:00  25.9   45.46  29.4    65.8     4.5   1.21  7.9    -6
2002-07-16 17:30:00  25.9   45.40  29.4    70.5     4.9   1.19  7.9    -6
2002-07-16 18:00:00  25.9   45.27  29.3    74.3     5.1   1.15  7.9    -6
2002-07-16 18:30:00  25.8   45.57  29.5    67.6     4.7   1.11  7.8    -6
...

print df2
                      PO4F   NH4F   NO2F   NO3F  NO23F  CHLA_N
DateTimeStamp                                                 
2002-07-16 14:01:00  0.053  0.073  0.005  0.021  0.026     8.6
2002-07-16 16:05:00  0.029  0.069  0.002  0.016  0.018     9.6
2002-07-16 18:09:00  0.023  0.073  0.000    NaN  0.014     5.8
...

我想在 df2 的索引值处找到 df1 的值,但是我可以从阅读文档和其他 stackoverflow 答案中找出答案的唯一方法是将 df1 放在一分钟的时间基上(这将生成一堆nans),然后使用Series.interpolate填充nans,然后在df2的离散时间提取一分钟值。这看起来非常浪费。一定还有别的办法吧?

最佳答案

如果您想要插值,我认为您会坚持使用您所描述的方法,或者近似“浪费”的方法。如果您可以设置采用最新值或下一个值,请分别使用 ffillbfill

In [34]: df1.reindex(df2.index, method='ffill')
Out[34]: 
                     Temp  SpCond   Sal  DO_pct  DO_mgl  Depth   pH  Turb
DateTimeStamp                                                            
2002-07-16 14:01:00  26.0   45.31  29.3    71.6     4.9   0.95  7.9    -5
2002-07-16 16:05:00  25.9   45.23  29.2    67.0     4.6   1.11  7.8    -6
2002-07-16 18:09:00  25.9   45.27  29.3    74.3     5.1   1.15  7.9    -6

关于python - 将大型数据帧插值到稀疏、不规则的索引上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18002070/

相关文章:

python - scipy中二次和二阶样条插值的区别

python - Scipy 中二维插值的问题

python - 如何在python中对日期进行排序?

python - Virtualenvwrapper - .bash_profile 的正确设置

python - 更改 numpy 数组上特定维度的指定值

python - Pandas:上传到 mysql 表

python - 将列值分配给数据框中的变量

python身份危机为什么l或x复制0而不产生新的0

python - 如何减去 Pandas Dataframe 中的两个日期时间值

image-processing - 双三次插值如何工作?