jeudi 31 mai 2018

IndexError: single positional indexer is out-of-bounds and if condition

I have a dataframe that looks like this:

    Repo
    Out[624]: 

1             Instrument    Term Code  WTD Rate
2    GC_AUSTRIA_SUB_10YR          T-N     -0.49
3    GC_AUSTRIA_SUB_10YR            O -0.467643
4      R_RAGB_1.15_10/18          S-N -0.520299
5      R_RAGB_4.35_03/19          S-N -0.497759
6      R_RAGB_4.35_03/19          T-N      -0.5
7      R_RAGB_1.95_06/19          S-N -0.501478
8      R_RAGB_0.25_10/19          S-N -0.497765

I have an if condition which is dependent on the column "Instrument"

if condition:
   return Repo.loc[(Repo['Instrument']=='GC_LCH_BELGIUM') & (Repo['Term Code']=='T-N'),'WTD Rate'].iloc[0]

The issue is that the Instrument name sometimes does not exist and get an error IndexError: single positional indexer is out-of-bounds

How can I say in the "if condition" that if the instrument does exist (or if there is an error) reverts back to default value of say 10.

I should point out that when Instrument does not exist there is no row in the dataframe. therefore code that looks something like "is empty" would not work

1 commentaire: