You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用pandas resample与Scipy interp1d实现时间序列升采样及插值?

用Pandas Resample + Scipy Interp1d实现高频插值

完全可以用Pandas的resample()结合Scipy的interpolate.interp1d()完成从250Hz到1000Hz的重采样插值,具体操作步骤如下:

1. 预处理时间序列索引

先把datetime列转换为Pandas的datetime类型,并设置为DataFrame的索引——这是时间序列操作的基础:

import pandas as pd
from scipy.interpolate import interp1d

# 构造示例数据(和你提供的一致)
df = pd.DataFrame({
    'datetime': ['2000-01-01 00:00:00.004', '2000-01-01 00:00:00.008',
                 '2000-01-01 00:00:00.012', '2000-01-01 00:00:00.016'],
    'sample_value': [56, 58, 64, 61]
})

# 转换时间格式并设为索引
df['datetime'] = pd.to_datetime(df['datetime'])
df = df.set_index('datetime')

2. 生成1000Hz的目标时间框架

用resample()生成每1ms一个样本(对应1000Hz)的空序列,此时采样值列会填充NaN:

# 重采样到1ms频率,得到所有需要的时间点
df_resampled = df.resample('1ms').asfreq()

3. 构建Scipy插值函数

因为interp1d只能处理数值型输入,所以先把原始时间转换为相对于起始时间的毫秒数,再构建插值函数:

# 把原始时间转成相对起始点的毫秒数
original_rel_ms = (df.index - df.index[0]).total_seconds() * 1000
original_values = df['sample_value'].values

# 构建插值函数,这里用线性插值,也可以选'quadratic'(二次)、'cubic'(三次)等
interp_func = interp1d(original_rel_ms, original_values, kind='linear', fill_value="extrapolate")

注:如果不需要对原始时间范围外的点插值,去掉fill_value="extrapolate"即可,超出范围的点会保留NaN。

4. 计算并填充插值结果

把重采样后的时间也转成相对毫秒数,用插值函数计算每个时间点的采样值:

# 转换重采样时间为相对起始点的毫秒数
resampled_rel_ms = (df_resampled.index - df.index[0]).total_seconds() * 1000

# 填充插值结果
df_resampled['sample_value'] = interp_func(resampled_rel_ms)

完成后df_resampled就是1000Hz的插值后数据,你可以直接查看或导出:

print(df_resampled)

内容的提问来源于stack exchange,提问作者MAtennis9

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 06:35:28