Finding local minimum values in pandas Finding local minimum values in pandas pandas pandas

Finding local minimum values in pandas


You can first find all downward runs and get the end points of this runs. Assuming your data is in file filename.csv.

import pandas as pdimport numpy as npdf = pd.read_csv('filename.csv', sep='\s+', parse_dates=True)down = df.Date.diff().values < 0df['test'] = np.r_[down[1:] != down[:-1], False] & down

Plotting the data with parse_dates=True in pd.read_csv yields nicer diagrams

import matplotlib.pyplot as pltplt.figure(figsize=(14,5))plt.plot(df.Date)plt.plot(df.Date[df.test], 'o');

Resultall local minima

To find all local minima < -80 you can add this condition

plt.figure(figsize=(14,5))plt.plot(df.Date)plt.plot(df.Date[(df.test) & (df.Date < -80)], 'o');

Resultlower -80 minima


Assuming df is the dataframe and data is the column name:

import numpy as npfrom scipy.signal import argrelextremadf["lmin"] = Falsedf.iloc[argrelextrema(df["data"].to_numpy(), np.less)[0], list(df.columns).index("lmin")] = True


If I understand correctly, local minima are where the values are both less than their previous and next values. Therefore, condition is

(data < data.shift(1)) & (data < data.shift(-1))

Adding the condition less than -80, the conditions become

conds = (data < -80) & (data < data.shift(1)) & (data < data.shift(-1))

Use this condtion to slice

data_minima = data[conds]Out[29]:2020-07-27   -97.9328482020-07-29   -96.3017742020-08-03   -99.1215142020-09-22   -89.451473Name: 1, dtype: float64