Home > Blockchain >  Using min() and max() in vectorized operations
Using min() and max() in vectorized operations

Time:12-29

I am attempting to make a given condition result in a choice that is the min of 2 possibilities. Both of these possibilities involve vectorized operations down the entire column. I am not sure if this is what is causing the error.

I continue to get this error:

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


conditions = [
    (df['SALES'] > 0) & (df['DELTA_ACOS'] > 0),
    (df['SALES'] > 0) & (df['DELTA_ACOS'] < 0),
    (df['SALES'] == 0) & (df['SPEND'] > df['AST'] * 0.5) & (df['SPEND'] < df['AST']),
    (df['SALES'] == 0) & (df['SPEND'] >= df['AST']) & (df['SPEND'] < df['AST'] * 1.5),
    (df['SALES'] == 0) & (df['SPEND'] >= df['AST'] * 1.5),
]

choices = [
    min(
        df['BID'] * 1.25,
        1   df['DELTA_ACOS'] * df[BID],
    ),
    min(
        1.15 * df['SPEND'] * (df['SALES'] / df['CLICKS']),
        df['BID'] * 0.95
    ),
    df['BID'] * 0.25,
    df['BID'] * 0.5,
    df['BID'] * 0.75,
]

df['NEW_BID'] = np.select(conditions, choices)

CodePudding user response:

Using a Pandas Series object as a boolean value, which is not allowed. This can happen when you try to use a Series object in a boolean context, such as an if statement or a logical operator like & or |.

In your code, the conditions list contains boolean Series objects that are created using the logical operators & and |. For example, the first element of conditions is (df['SALES'] > 0) & (df['DELTA_ACOS'] > 0). This is a boolean Series that has a value of True for each row where both SALES is greater than 0 and DELTA_ACOS is greater than 0.

To fix the error, you can use the .all() or .any() method to reduce the Series to a single boolean value. For example, you can change (df['SALES'] > 0) & (df['DELTA_ACOS'] > 0) to (df['SALES'] > 0).all() & (df['DELTA_ACOS'] > 0).all(). This will return a single boolean value that represents whether all values in the SALES and DELTA_ACOS columns are greater than 0.

Alternatively, you can use the .loc[] indexer to select the rows that you want to apply the condition to, and then use a boolean array as the condition. For example:

conditions = [    df.loc[df['SALES'] > 0, 'DELTA_ACOS'] > 0,
    df.loc[df['SALES'] > 0, 'DELTA_ACOS'] < 0,
    (df['SALES'] == 0) & (df['SPEND'] > df['AST'] * 0.5) & (df['SPEND'] < df['AST']),
    (df['SALES'] == 0) & (df['SPEND'] >= df['AST']) & (df['SPEND'] < df['AST'] * 1.5),
    (df['SALES'] == 0) & (df['SPEND'] >= df['AST'] * 1.5),
]

CodePudding user response:

I found a solution to this by restructuring the choices to use np.where() instead of min().

  • Related