finding conditional local minima values in time series python

后端 未结 1 1996
南笙
南笙 2021-01-24 00:24

For a time series dataset:

A, How do I find the local minima (nadir values) for each ID? (local mins)

B, How do I find any subsequent values that are 2 greater

相关标签:
1条回答
  • 2021-01-24 00:58

    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)
    

    Answer for A:

    df[df['if_A']==True]
    

    Answer for B:

    df[df['if_B']==True]
    
    0 讨论(0)
提交回复
热议问题