<class 'IndexError'>: list index out of range

Solved!
darioromero
Level 2
<class 'IndexError'>: list index out of range

This instruction runs fine in the notebook but when I go back to the recipe and execute it it throws this error: 

datatype = df.to_dict('records')[0]['datatype']

<class 'IndexError'>: list index out of range

Is there a sort of custom pandas in the recipe that is not the same as the one in the notebook version? 

Thanks in advanced for your help.

Dario


Operating system used: linux AWS

0 Kudos
2 Solutions
AlexT
Dataiker

Hi @darioromero ,
The error may suggest the df you have in your recipe is empty. You could try to handle this case :

if not df.empty:
    data_list = df.to_dict('records')
    datatype = data_list[0]['datatype']
    # Perform operations with datatype
else:
    # Handle the case when the DataFrame is empty

 

View solution in original post

darioromero
Level 2
Author

Thanks Alex. dataframe was empty. Appreciate it.

View solution in original post

0 Kudos
2 Replies
AlexT
Dataiker

Hi @darioromero ,
The error may suggest the df you have in your recipe is empty. You could try to handle this case :

if not df.empty:
    data_list = df.to_dict('records')
    datatype = data_list[0]['datatype']
    # Perform operations with datatype
else:
    # Handle the case when the DataFrame is empty

 

darioromero
Level 2
Author

Thanks Alex. dataframe was empty. Appreciate it.

0 Kudos