熊猫重新采样器插值未正确插值

时间:2020-02-18 20:31:34

标签: python pandas series

我正在处理熊猫系列,我想对该数据重新采样以获取10秒间隔。我正在处理的系列:

volumeResampler = volSeries.resample('10S')
resampledVolumeData = volumeResampler.interpolate('linear')
resampledVolumeData.head(20)

Time
2018-10-14 12:00:00    6.000000
2018-10-14 12:00:10    6.198148
2018-10-14 12:00:20    6.396296
2018-10-14 12:00:30    6.594444
2018-10-14 12:00:40    6.792593
2018-10-14 12:00:50    6.990741
2018-10-14 12:01:00    7.188889
2018-10-14 12:01:10    7.387037
2018-10-14 12:01:20    7.585185
2018-10-14 12:01:30    7.783333
2018-10-14 12:01:40    7.981481
2018-10-14 12:01:50    8.179630
2018-10-14 12:02:00    8.377778
2018-10-14 12:02:10    8.575926
2018-10-14 12:02:20    8.774074
2018-10-14 12:02:30    8.972222
2018-10-14 12:02:40    9.170370
2018-10-14 12:02:50    9.368519
2018-10-14 12:03:00    9.566667
2018-10-14 12:03:10    9.764815
Freq: 10S, Name: Value, dtype: float64

当我尝试重新采样时,输出呈线性增加,与数据不匹配:

add_new_realisaties_admin_column_show_value

在重采样前后比较图形时,您会发现它显然没有正确地插值。

重采样之前:

Volume before Resampling

重采样后:

Volume after Resampling

2 个答案:

答案 0 :(得分:0)

错误似乎与我内插的频率有关。我要插值的值之间的最小间距为2秒,相邻点之间的最大值为37080。

如果我在<= 4秒的任何时间重新采样,则插值工作正常。我不确定这是错误还是功能。

答案 1 :(得分:0)

如果要对频率窗口外的值进行插值,可以将所需的频率插入初始数据帧中,对其进行排序,然后进行插值,然后仅在所需的确切频率下选择值:< / p>

# example from your sample, change the end date
sample = pd.DataFrame(
    {
        'time': pd.date_range(
            '2018-10-14 12:00:00', '2018-10-14 12:22:00', freq='10S'
        )
    }
)
df = (
    pd.concat(
        [
            volSeries.to_frame(),
            sample,
        ],
        sort=False,
    )
    .sort_values(['time'])
    .drop_duplicates(subset=['time'])
    .set_index('time')
    .interpolate('index')
    .loc[sample['time'].values, :]
)

这给出了:

                       volume
time                         
2018-10-14 12:00:00  6.000000
2018-10-14 12:00:10  5.053333
2018-10-14 12:00:20  5.120000
2018-10-14 12:00:30  5.186667
2018-10-14 12:00:40  5.253333
2018-10-14 12:00:50  5.320000
2018-10-14 12:01:00  5.386667
2018-10-14 12:01:10  5.453333
2018-10-14 12:01:20  5.520000
2018-10-14 12:01:30  5.586667
2018-10-14 12:01:40  5.653333
2018-10-14 12:01:50  5.720000
2018-10-14 12:02:00  5.786667
2018-10-14 12:02:10  5.853333
2018-10-14 12:02:20  5.920000
2018-10-14 12:02:30  5.986667
2018-10-14 12:02:40  5.680000
2018-10-14 12:02:50  5.280000
2018-10-14 12:03:00  5.120000
2018-10-14 12:03:10  5.520000