Home > front end >  Python Boolean based formula is processing false as if true
Python Boolean based formula is processing false as if true

Time:12-08

I have some logic which validates a row in a dataframe.

It is simply, if the value (which is 6) is less than the min_value (which is 1) then min_value_fail = true.

Then if min_value_fail = true then append row to validation_failures dataframe.

As per the screenshot below, notice the payload shows min_value_fail as false yet it deals with it as if true

When I print(min_values_data['min_value_fail']) it shows as false

Can anyone else spot the mistake? I've been through this countless times.

enter image description here

Script

# validate where min_value higher than value
min_values_data = df.loc[df['min_value'] > 0].copy()
min_values_data['min_value_fail'] = pd.to_numeric(min_values_data['value'], errors='coerce') < pd.to_numeric(min_values_data['min_value'], errors='coerce')
display(HTML(min_values_data.to_html()))
if [min_values_data[min_values_data['min_value_fail'].values == True]]:
    print('failed')
    min_category = min_values_data['category']
    min_type = min_values_data['type']
    min_error = 'value is less than the minimum required'
    validation_failures = validation_failures.append({"category": min_category.values, "type": min_type.values, "error_message": min_error}, ignore_index=True)
else:
    print('passed')

CodePudding user response:

[min_values_data[min_values_data['min_value_fail'].values == True]] is a list and not a boolean. The truth value of a list is True if the list is non empty and is False if it is empty.

CodePudding user response:

The problem is that you're packing your check into a list!

>>> bool([pd.DataFrame().values == True])
True
>>> bool(pd.DataFrame().values == True)
False
  • Related