4

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?

3
  • 1
    Can add the line of code you use to call the function? Commented Jun 28, 2019 at 15:29
  • It appears to be an Index Commented Jun 28, 2019 at 15:30
  • Some values aren't considered Truthy just because they exist (or have something in it). You are passing an Index which does not behave like you expect for if categories.... Try if categories != None and ... Commented Jun 28, 2019 at 15:31

2 Answers 2

7

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:
Sign up to request clarification or add additional context in comments.

Comments

7

I suggest this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.