2016-03-07 90 views
0

我想插入(高档)非空闲时间序列以获得等间隔时间序列。插入(上采样)非等间隔时间序列与Pandas版本等间隔18.0rc1

目前我做这件事的方式如下:

  1. 采取原来的时间序列。
  2. 创建新的时间序列与NaN的在各30秒的时间间隔值(使用重采样(“30S”)。asfreq())
  3. 的concat原始时间序列和新的时间序列
  4. 时间序列,以恢复的时间的顺序排序(这我不喜欢 - 分拣具有复杂度O = N的log(n))
  5. 插值
  6. 从时间序列

有与熊猫版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() 

image

回答

0

已经有在期望值插值(我将在链接编辑后,但搜索插值问题问题跟踪器),一个简单的方法几个请求。所以将来会有更简单的方法。

现在你可以多一点干净的

In [9]: (ts.reindex(ts.index | newFreq.index) 
      .interpolate(method='time') 
      .loc[newFreq.index]) 
Out[9]: 
2015-01-04 08:29:00    NaN 
2015-01-04 08:30:00 277996.070686 
2015-01-04 08:31:00 285236.860707 
2015-01-04 08:32:00 292477.650728 
2015-01-04 08:33:00 299718.440748 
          ... 
2015-01-04 08:45:00 261362.402778 
2015-01-04 08:46:00 261937.569444 
2015-01-04 08:47:00 262512.736111 
2015-01-04 08:48:00 263087.902778 
2015-01-04 08:49:00 263663.069444 
Freq: 60S, dtype: float64 

这仍然涉及到你上面列出的所有步骤写的选项,但指标的unioning比concating和下滴清洁剂。