1

I am trying to find the beginning of a recession in this list, this recession occurs when two consecutive quartiles are descending (-1). How can I buy this? I'm trying to do a double if to check the row 'row' and the next row, 'row' +1, but I can't find the key. Furthermore, this quartile is always the first one in the recession group.

   Quarterly  GDP change

  2007q4         1.0

  2008q1        -1.0

  2008q2         1.0

  2008q3        -1.0 <---This value is the recession start

  2008q4        -1.0

  2009q1        -1.0

  2009q2        -1.0

2 Answers 2

2

assuming your Quarterly column is sorted.

If a recession is two consecutive periods of negative GDP growth, then we are only looking for values that are smaller than 0,

mask = df[(df['GDP'].eq(-1) & df['GDP'].eq(-1).shift())].index.min() -1 

df.loc[mask,'change'] = 'recession_start'

  Quarterly  GDP           change
0    2007q4  1.0              NaN
1    2008q1 -1.0              NaN
2    2008q2  1.0              NaN
3    2008q3 -1.0  recession_start
4    2008q4 -1.0              NaN
5    2009q1 -1.0              NaN
6    2009q2 -1.0              NaN
Sign up to request clarification or add additional context in comments.

Comments

1

Taking inspiration from Datanovice's answer (which I think is nearly there but will not work if there have been a lot of positive values before the two consecutive negative ones): if you're looking for two consecutive cells with negative values, what we can do is a forward rolling sum with a window of two cells. To do a forward rolling sum, we reverse the order of the dataframe, do the rolling sum, then flip it back around. In this case:

df.loc[:, "Recession"] = df.iloc[::-1].loc[:,"GDP"].rolling(window=2).sum().iloc[::-1].lt(0)

This will only output "True" for the first period of two consecutive values where the GDP value is negative.

1 Comment

nice catch, just edited my post to handle the consecutive positive months.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.