2016-09-15 19 views
1

我有一个熊猫系列,索引是日期时间。过滤/平滑步进功能以检索最大增量

我可以用step()函数绘制我的函数,该函数绘制系列的每个点相对于时间(x是时间)。

我想要一个不太精确的进化方法。所以我需要减少步骤的数量,并忽略最小的增量。 enter image description here 我发现的唯一方法是使用numpy的poly1d()函数将点插值为多项式,然后执行该函数。不幸的是我在转换过程中丢失了时间索引,因为多项式的索引是x。

是否有一种方法可以简化我的函数,使其只获取y轴上最大变化的日期(x值),而不是将所有日期更改为任何更改? 正如我上面写的,我想只有最大的增量而不是小的变化。

这里是确切的数据:

2016-01-02 -5.418440 
2016-01-09 -9.137942 
2016-01-16 -9.137942 
2016-01-23 -9.137942 
2016-01-30 -9.137942 
2016-02-06 -11.795107 
2016-02-13 -11.795107 
2016-02-20 -11.795107 
2016-02-27 -11.795107 
2016-03-05 -11.795107 
2016-03-12 -13.106988 
2016-03-19 -13.106988 
2016-03-26 -13.106988 
2016-04-02 -13.106988 
2016-04-09 -13.106988 
2016-04-16 -13.106988 
2016-04-23 -13.106988 
2016-04-30 -11.458878 
2016-05-07  0.051123 
2016-05-14  2.010179 
2016-05-21 -3.210870 
2016-05-28 -0.726291 
2016-06-04  5.841818 
2016-06-11  5.067061 
2016-06-18  5.789375 
2016-06-25 16.455159 
2016-07-02 22.518294 
2016-07-09 39.834977 
2016-07-16 54.685965 
2016-07-23 54.685965 
2016-07-30 55.169290 
2016-08-06 55.169290 
2016-08-13 55.169290 
2016-08-20 53.366569 
2016-08-27 45.758675 
2016-09-03 10.976592 
2016-09-10 -0.554887 
2016-09-17 -8.653451 
2016-09-24 -18.198305 
2016-10-01 -22.218711 
2016-10-08 -21.158434 
2016-10-15 -11.723798 
2016-10-22 -9.928957 
2016-10-29 -17.498315 
2016-11-05 -22.850454 
2016-11-12 -25.190656 
2016-11-19 -27.250960 
2016-11-26 -27.250960 
2016-12-03 -27.250960 
2016-12-10 -27.250960 
+0

您可以运行在该系列中的差异,然后通过阈值过滤器/屏蔽。这样,你保持索引 – Will

+1

如果你可以分享数据,我们可以尝试帮助你更好... –

+0

@RiccardoPetraglia谢谢我编辑了问题 –

回答

0

一种方式是从原始的系列赛里从系列的前值值的绝对差异对比你的敏感阈值进行比较创建模板。掩码只是一个布尔选择数组(矩阵),用于过滤原始序列。

#my_series is your Series 
threshold = 10.0 
diff_series = my_series.diff.abs() 
mask = diff_series > threshold 
#now plot the masked values only or create new series from it etc. 
my_series[mask].plot() 
+1

尽管此代码可能会回答这个问题,但提供关于此代码为何和/或如何回答此问题的其他上下文会提高其长期价值。不鼓励使用仅有代码的答案。 – Ajean

-1

您可以使用pandas resample function

导入数据并将列设置为'日期'和'值'。剩下的将日期列解析为日期时间。

import pandas as pd 
from datetime import datetime 

df.columns = ['Date','Values'] 
df.Date = df.Date.map(lambda x: datetime.strptime(x,'%Y-%m-%d')) 
df.set_index('Date',inplace=True) 

您现在可以重新采样时间序列。 E.g按月:

resampled_df = df.resample('M').mean() 
resampled_df.head() 

最后,绘制它。

resampled_df.plot() 
+0

没有真正回答这个问题。 OP没有要求定期的时间间隔的平均值 – Will

1

,所以这是我的想法:

# Load the data 
a = load_table('<your_data_file>', delim_whitespace=True, names=['value'], index_col=0) 

# Create and additional column containing the difference 
#+between two consecutive values: 
a['diff'] = a.value.diff() 

# select only the value of the 'diff' column higher than a certain threshold 
#+and copy them to a new frame: 
b = a[abs(a['diff']) > .5] # The threshold (.5) could be what you think is the best 

# Plot your new graph 
b.value.plot() 

希望这是有益...