Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a function parameter exists with Python?

Tags:

python

My code is:

def load_data(datafile, categories=None, cat_columns=None):
    ohe_categories = 'auto'
    if categories and len(categories) > 0:
        ohe_categories = categories
    ohe = OneHotEncoder(handle_unknown='ignore', categories=ohe_categories)

When categories is None, it works fine. But if I pass something, I get an error:

ValueError: The truth value of a Index is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

I am calling the function with:

training_x, training_y, categories, cat_columns = loader.load_data(
    'data/training.csv')

test_x, test_y = loader.load_data(
    'data/test.csv', categories=categories, cat_columns=cat_columns)

How can I check properly?

like image 826
Shamoon Avatar asked Oct 17 '25 19:10

Shamoon


1 Answers

You are passing a value which doesn't support conversion to a bool. In this case, you need to explicitly check if the value is not None:

if categories is not None:
like image 195
Code-Apprentice Avatar answered Oct 19 '25 11:10

Code-Apprentice