Skip to content Skip to sidebar Skip to footer

Finding Conditional Local Minima Values In Time Series Python

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 than each local min

Solution 1:

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]

Post a Comment for "Finding Conditional Local Minima Values In Time Series Python"