: list index out of range

Options
darioromero
darioromero Registered Posts: 9 ✭✭✭✭

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

Best Answers

  • Alexandru
    Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,209 Dataiker
    edited July 17 Answer ✓
    Options

    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
    darioromero Registered Posts: 9 ✭✭✭✭
    Answer ✓
    Options

    Thanks Alex. dataframe was empty. Appreciate it.

Setup Info
    Tags
      Help me…