For an assignment, I am identifying the first quarter of the 2008 recession in the United States. The Excel data I'm using can be downloaded here: gdplev.xls. How can I improve this pandas code to make it more idiomatic or optimized?
def get_recession_start():
'''Returns the year and quarter of the recession start time as a
string value in a format such as 2005q3'''
GDP_df = pd.read_excel("gdplev.xls",
names=["Quarter", "GDP in 2009 dollars"],
parse_cols = "E,G",
skiprows = 7)
GDP_df = GDP_df.query("Quarter >= '2000q1'")
GDP_df["Growth"] = GDP_df["GDP in 2009 dollars"].pct_change()
GDP_df = GDP_df.reset_index(drop=True)
# recession defined as two consecutive quarters of negative growth
GDP_df["Recession"] = (GDP_df.Growth < 0) & (GDP_df.Growth.shift(-1) < 0)
return GDP_df.iloc[GDP_df["Recession"].idxmax()]["Quarter"]
get_recession_start()