如何在 Presto 中的大数据集上使用线性插值法对时间序列数据进行插值?
How to interpolate Time series data using Linear interpolation on big datasets in Presto?
我正在处理大型数据集,使用 prestodb 作为查询引擎,使用 Hive 作为与外部存储链接的元存储。以下是我的场景的详细信息:
我有一个包含时间序列数据 (timestamp) 和数字列的数据集。我想使用插值技术填充缺失值。例如
输入数据集:
执行操作后输出应如下所示:
一个更简单的解决方案是使用 pandas 并插入所有值,但在内存中加载数百万行并不是一个好方法。同样presto不支持分页
实现上述场景的最佳方法是什么?
基本上,您可以使用 lag(ignore nulls)
/lead(ignore nulls)
和一些插值算法:
select t.*,
coalesce(t.pressure,
(time_ms - prev_time_ms) * (next_pressure - prev_pressure) / (next_time_ms - prev_time_ms)
) as imputed_pressure
from (select t.*,
to_milliseconds(time) as time_ms
lag(pressure ignore nulls) over (order by time) as prev_pressure,
lag(to_milliseconds(time) ignore nulls) over (order by time) as prev_time_ms,
lag(pressure ignore nulls) over (order by time) as next_pressure,
lag(to_milliseconds(time) ignore nulls) over (order by time) as next_time_ms
from t
) t
我正在处理大型数据集,使用 prestodb 作为查询引擎,使用 Hive 作为与外部存储链接的元存储。以下是我的场景的详细信息:
我有一个包含时间序列数据 (timestamp) 和数字列的数据集。我想使用插值技术填充缺失值。例如 输入数据集:
执行操作后输出应如下所示:
一个更简单的解决方案是使用 pandas 并插入所有值,但在内存中加载数百万行并不是一个好方法。同样presto不支持分页
实现上述场景的最佳方法是什么?
基本上,您可以使用 lag(ignore nulls)
/lead(ignore nulls)
和一些插值算法:
select t.*,
coalesce(t.pressure,
(time_ms - prev_time_ms) * (next_pressure - prev_pressure) / (next_time_ms - prev_time_ms)
) as imputed_pressure
from (select t.*,
to_milliseconds(time) as time_ms
lag(pressure ignore nulls) over (order by time) as prev_pressure,
lag(to_milliseconds(time) ignore nulls) over (order by time) as prev_time_ms,
lag(pressure ignore nulls) over (order by time) as next_pressure,
lag(to_milliseconds(time) ignore nulls) over (order by time) as next_time_ms
from t
) t