ValueError: Use a.empty, a.bool(), a.item(), a.any() or a.all() & AttributeError: 'bool' object has no attribute 'bool'

I would like to create a function which could convert some concentrations into nanomolar concentration. So I wrote this function:

def convert_molars(df, field_values, field_units):

    if (df[field_units].str.contains('uM')): # 1 uM = 1000 nM
        df[field_values] *= 1000
    
    elif (df[field_units].str.contains('M')): # 1 M = 1000000000 nM
        df[field_values] *= 1000000000
    
    else: # 1 mM = 1000000 nM
        df[field_values] *= 1000000
    
    return df

And I started it like this:

standard_units = convert_molars(IC50_nonan_units, 'Standard Value', 'Standard Units')
standard_units.to_csv("standard_units.csv")
standard_units.head()

But I got this error:

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

I tried one more option like this:

def convert_molars_bool(df, field_values, field_units):

    if (df[field_units].str == 'uM').bool(): # 1 uM = 1000 nM
        df[field_values] *= 1000
    
    elif (df[field_units].str == 'M').bool(): # 1 M = 1000000000 nM
        df[field_values] *= 1000000000
    
    else: # 1 mM = 1000000 nM
        df[field_values] *= 1000000
    
    return df

But I got this:

AttributeError: 'bool' object has no attribute 'bool'

Could someone please explain me that I did wrong?

>Solution :

the == operator returns a bool value. In your if and elif conditions you are trying to run the bool() method on a bool.

def convert_molars_bool(df, field_values, field_units):

    if (df[field_units].str == 'uM'): # 1 uM = 1000 nM
        df[field_values] *= 1000
    
    elif (df[field_units].str == 'M'): # 1 M = 1000000000 nM
        df[field_values] *= 1000000000
    
    else: # 1 mM = 1000000 nM
        df[field_values] *= 1000000
    
    return df

This should work

Leave a Reply