2

I am writing a script in python pandas, where I have to find the first fall point of the value and date, then where it reached the max point, and before falling down the value and date. Then again it's falling point value and date. In the graph I presented below, I marked the red circle from where I want to get the dates and values. I have a script but I need to mention the date for getting the value, but I want to extract the date and value, any help will be appreciated.

Code:

import pandas as pd

df = pd.read_csv(r"D:\Data\2015_20.csv", parse_dates=["Date"])
df = df[["Date", "Mean"]]
df = df.set_index("Date")
z1 = df['2016-04-28' : '2017-02-22'].min()
z2 = df['2017-05-13' : '2018-02-02'].max()
z3 = df['2018-03-19' : '2019-03-04'].max() 
print("2016", '%.2f'%z1)
print("2017", '%.2f'%z2)
print("2018", '%.2f'%z3)

enter image description here

enter image description here

user286076
  • 131
  • 3
  • 14

1 Answers1

3

You can use argrelextrema for find local min and max:

from scipy.signal import argrelextrema

np.random.seed(0)
rs = np.random.randn(200)
xs = [0]
for r in rs:
    xs.append(xs[-1] * 0.9 + r)
df = pd.DataFrame(xs, columns=['data'], index=pd.date_range('2000-01-01',periods=len(xs)))

n = 5  # number of points to be checked before and after

# Find local peaks

df['min'] = df.iloc[argrelextrema(df.data.values, np.less_equal,
                    order=n)[0]]['data']
df['max'] = df.iloc[argrelextrema(df.data.values, np.greater_equal,
                    order=n)[0]]['data']

df['min_date'] = df.index.where(df['min'].notna())
df['max_date'] = df.index.where(df['max'].notna())

print (df.head(15))
                data       min       max   min_date   max_date
2000-01-01  0.000000  0.000000       NaN 2000-01-01        NaT
2000-01-02  1.764052       NaN       NaN        NaT        NaT
2000-01-03  1.987804       NaN       NaN        NaT        NaT
2000-01-04  2.767762       NaN       NaN        NaT        NaT
2000-01-05  4.731879       NaN       NaN        NaT        NaT
2000-01-06  6.126249       NaN  6.126249        NaT 2000-01-06
2000-01-07  4.536346       NaN       NaN        NaT        NaT
2000-01-08  5.032800       NaN       NaN        NaT        NaT
2000-01-09  4.378163       NaN       NaN        NaT        NaT
2000-01-10  3.837128       NaN       NaN        NaT        NaT
2000-01-11  3.864013       NaN       NaN        NaT        NaT
2000-01-12  3.621656  3.621656       NaN 2000-01-12        NaT
2000-01-13  4.713764       NaN       NaN        NaT        NaT
2000-01-14  5.003425       NaN       NaN        NaT        NaT
2000-01-15  4.624757       NaN       NaN        NaT        NaT

EDIT:

Solution from real data:

df['Date'] = pd.to_datetime(df['Date'])

df = df.set_index('Date')

from scipy.signal import argrelextrema
n = 5
s1 = df.iloc[argrelextrema(df.Mean.values, np.less_equal,
                          order=n)[0]]['Mean']
s2 = df.iloc[argrelextrema(df.Mean.values, np.greater_equal,
                          order=n)[0]]['Mean']

s = s1.append(s2).sort_index()
print (s)
Date
2016-05-18    0.293171
2016-11-04    0.692509
2017-05-13    0.232963
2017-09-10    0.675797
2017-11-09    0.528592
2018-04-03    0.189523
2018-11-09    0.713351
Name: Mean, dtype: float64

s.to_csv('out.csc')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • @jezreal, As I am working on CSV data as I mentioned in my post. How to give CSV as an input here. – user286076 Feb 26 '21 at 06:30
  • @user286076 - Use your solution `df = pd.read_csv(r"D:\Data\2015_20.csv", parse_dates=["Date"]) df = df[["Date", "Mean"]] df = df.set_index("Date")`, there is some problem? – jezrael Feb 26 '21 at 06:31
  • i am getting date errors in several places, plz can you design the code based on my input data which I posted above. Thanks – user286076 Feb 26 '21 at 06:37
  • @user286076 - What are errors? [Please don't post images of code/data (or links to them)](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question) – jezrael Feb 26 '21 at 06:42
  • @user286076 - Also what is expected ouput from sample data? How looks new DataFrame? – jezrael Feb 26 '21 at 06:43
  • yes, I am expecting the output from my sample data which is in CSV format. – user286076 Feb 26 '21 at 06:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229234/discussion-between-user286076-and-jezrael). – user286076 Feb 26 '21 at 06:45