I have a dataframe index_crisis
and and want to create a new column that contains a 1 when the index reached a local peak and zero else.
I don't know how to go on in my code. The list peak locations
is:
[ 2 7 9 13 16 18 21] but with month[peak_locations]
I get the month of the peaks.
Date Index
38 2007-06-01 -0.56
39 2007-07-01 -0.36
40 2007-08-01 0.68
41 2007-09-01 0.24
42 2007-10-01 0.22
43 2007-11-01 0.89
44 2007-12-01 0.95
45 2008-01-01 1.53
46 2008-02-01 1.01
47 2008-03-01 1.73
48 2008-04-01 1.39
49 2008-05-01 0.96
50 2008-06-01 1.26
51 2008-07-01 2.37
52 2008-08-01 1.57
53 2008-09-01 2.95
54 2008-10-01 5.7
55 2008-11-01 5.29
56 2008-12-01 5.42
57 2009-01-01 4.99
58 2009-02-01 4.45
59 2009-03-01 4.59
60 2009-04-01 4.2
61 2009-05-01 3.12
62 2009-06-01 1.85
My expected output is a column dummy
that looks like:
0
0
1
0
0
0
0
1
0
1
0
0
0
1
0
0
1
0
1
0
0
1
0
0
0
df = pd.read_csv("index_crisis.csv", parse_dates=True)
df['Date'] = pd.to_datetime(df['Date'])
df['Date'] = pd.PeriodIndex(df.Date, freq='M').strftime("%b %Y")
data = df['Index'].values
doublediff = np.diff(np.sign(np.diff(data)))
peak_locations = np.where(doublediff == -2)[0] + 1