0
我想插入(高档)非空闲时间序列以获得等间隔时间序列。插入(上采样)非等间隔时间序列与Pandas版本等间隔18.0rc1
目前我做这件事的方式如下:
- 采取原来的时间序列。
- 创建新的时间序列与NaN的在各30秒的时间间隔值(使用重采样(“30S”)。asfreq())
- 的concat原始时间序列和新的时间序列
- 时间序列,以恢复的时间的顺序排序(这我不喜欢 - 分拣具有复杂度O = N的log(n))
- 插值
- 从时间序列
有与熊猫版18.0rc1一个更简单的方法删除原来的点?就像在matlab中一样,您有原始时间序列,并将新时间作为参数传递给interpolation()函数以在所需时间接收值。
我说过原始时间序列的时间可能不是所需时间序列的时间子集。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
values = [271238, 329285, 50, 260260, 263711]
timestamps = pd.to_datetime(['2015-01-04 08:29:4',
'2015-01-04 08:37:05',
'2015-01-04 08:41:07',
'2015-01-04 08:43:05',
'2015-01-04 08:49:05'])
ts = pd.Series(values, index=timestamps)
ts
ts[ts==-1] = np.nan
newFreq=ts.resample('60S').asfreq()
new=pd.concat([ts,newFreq]).sort_index()
new=new.interpolate(method='time')
ts.plot(marker='o')
new.plot(marker='+',markersize=15)
new[newFreq.index].plot(marker='.')
lines, labels = plt.gca().get_legend_handles_labels()
labels = ['original values (nonequispaced)', 'original + interpolated at new frequency (nonequispaced)', 'interpolated values without original values (equispaced!)']
plt.legend(lines, labels, loc='best')
plt.show()