mardi 27 octobre 2015

My python loop is out of control. No clue how to solve this

I have a panda's dataframe with df.shape = (36, 17). The dataframe contains 17 economic and fiscal indicators from the Netherlands, measured over 36 years.

I am trying to replicate a rules-based European agreement that provides an European memberstate with exact instructions how big a Required annual fiscal adjustment should be. This required fiscal adjustment depends on the level of government debt and on the output gap (which is an economic indicator for the business cycle). The exact instructions are provided in the table below.

SGP flexibility

The relevant indicators from the table above have the following names in my dataframe:

  • Debt = gov_debt_perct_mev (60% = 60)
  • Output gap = output_gap_pf_sf
  • Difference between potential and growth = diff_pg_ag
  • Required annual fiscal adjustment = reqsb

I am now trying to replicate the table with Python code as to calculate what the required annual fiscal adjustment should be for all the years in my dataframe.

Sadly, if I run this code below I get len(reqsb) = 21456. But it should be equal to the amount of years which is 36.

Question: I cannot find the bug. Anybody any tips to get the correct length?

Sorry for this long story btw, but wanted to provide enough info :-)

reqsb = []

for debt in df.gov_debt_perct_mev:
    if (debt <= 60.0):
        for og in df.output_gap_pf_sf:
            if (og < -4.0):
                reqsb.append(0)
            if (og >= -4.0) & (og < -3.0):
                reqsb.append(0)
            if (og >= -3.0) & (og < -1.5):
                for diff in df.diff_pg_ag:
                    if (diff > 0):
                        reqsb.append(0.25)
                    else:
                        reqsb.append(0)
            if (og >= -1.5) & (og < 1.5):
                reqsb.append(0.5)
            if (og >= 1.5):
                for diff in df.diff_pg_ag:
                    if (diff > 0):
                        reqsb.append(0.76)
                    else:
                        reqsb.append(0.51)
    else:
        for og in df.output_gap_pf_sf:
            if (og < -4.0) :
                reqsb.append(0)
            if (og >= -4.0) & (og < -3.0):
                reqsb.append(0.25)
            if (og >= -3.0) & (og < -1.5):
                for diff in df.diff_pg_ag:
                    if (diff > 0):
                        reqsb.append(0.5)
                    else:
                        reqsb.append(0.25)
            if (og >= -1.5) & (og < 1.5):
                reqsb.append(0.51)
            if (og >= 1.5):
                for diff in df.diff_pg_ag:
                    if (diff > 0):
                        reqsb.append(1.0)
                    else:
                        reqsb.append(0.76)

Aucun commentaire:

Enregistrer un commentaire