在时间序列python中查找条件局部最小值 [英] finding conditional local minima values in time series python
问题描述
对于时间序列数据集:
A,如何找到每个ID的局部最小值(最低值)? (本地分钟)
A, How do I find the local minima (nadir values) for each ID? (local mins)
B,我如何找到比每个局部最小值大2的任何后续值. (本地分钟+ 2)
B, How do I find any subsequent values that are 2 greater than each local minima. (local mins + 2)
import pandas as pd
df = pd.DataFrame({'id': [1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2], 'value': [8,5,3,2,1,2,3,5, 1.5, 3, 1, 1.5, 2, 3, 4, 0.4]})
对于A,我能够使用以下代码从数据集中找到所有的最低/局部最小值,但未将其按每个ID分组.我该如何修改以按ID分组?
For A, I was able to use the following code to find all the nadir/local minimum values from the dataset, but they are not grouped by each id. How do I modify this to group them by id?
nadir_min = df.value[(df.value.shift(1) > df.value) & (df.value.shift(-1) > df.value)]
nadir_min
Out[1]:
4 1.0
8 1.5
10 1.0
Name: value, dtype: float64
对于B,我想在最低点/局部最小值之后返回比其最低点/局部最小值大两个值的后续值.对于上面的示例数据,我会回来:
For B, I would like to get back the subsequent values after the nadir/local minimums that are two greater than the nadir/local minimums. For the example data above I would get back:
index id value
6 1 3.0
13 2 3.0
14 2 4.0
也许条件循环可以解决问题,因为它可以存储每个局部最小值并比较随后的值(如果它们比其大2).但是,工作数据集非常庞大,运行时间太长,因此我正在尝试以下操作:
Perhaps a conditional loop would do the trick as it can store each local min and compare the subsequent values if they are 2 greater than it. However, the working dataset is MASSIVE and would take too long to run so I am trying something like this:
df['min_plus2'] = (df['value'] >= nadir_min + 2) & (df.index > nadir_min_index)
推荐答案
您可以使用下一个代码完成此操作:
You do it with next code:
import pandas as pd
import numpy as np
df = pd.DataFrame({'id': [1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2], 'value': [8,5,3,2,1,2,3,5, 1.5, 3, 1, 1.5, 2, 3, 4, 0.4]})
df['loc_min'] = df.value[(df.value.shift(1) > df.value) & (df.value.shift(-1) > df.value)]
df['if_A'] = np.where(df['loc_min'].isna(), False, True)
df['loc_min'].fillna(method='ffill', inplace=True)
df['if_B'] = np.where(df['value'] - df['loc_min'] >= 2, True, False)
回答A:
df[df['if_A']==True]
B的答案:
df[df['if_B']==True]
这篇关于在时间序列python中查找条件局部最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!