5

writing a function that should meet a condition on a row basis and return the expected results

def bt_quantity(df):
    df = bt_level(df)
    df['Marker_change'] = df['Marker'] - df['Marker'].shift(1).fillna(0).round(0).astype(int)
    df['Action'] = np.where(df['Marker_change'] > 0, "BUY", "")

    def turtle_split(row):
        if df['Action'] == 'Buy':
            return baseQ * (turtle ** row['Marker'] - 1) // (turtle - 1)
        else:
            return 0
    df['Traded_q'] = df.apply(turtle_split, axis=1).round(0).astype(int)
    df['Net_q'] = df['Traded_q'].cumsum().round(0).astype(int)
    print(df.head(39))
    return df

This is a common issue, and I am not using any "and" or "or" in the code. still getting the below error

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

I tried changing str to int(BUY >> 1), no progress. P.S. the data set is huge and I am using multiple modules and functions to work on this project.

1 Answers1

5

As you said, this is really a common problem, you will find the answers from Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

But to address your particular case, this is not a very efficient way of doing it as the dataset is huge. You should use Numpy. That will shorten the runtime drastically.

I see two issues with your snippet

  1. There is a Typo. You used "BUY" and then used "Buy". Python is case-sensitive.
  2. The col Action is getting tested for "BUY" entirely. The fix is to use row (Not a pythonic way, but a small fix)
def bt_quantity(df):
    df = bt_level(df)
    df['Marker_change'] = df['Marker'] - df['Marker'].shift(1).fillna(0).round(0).astype(int)
    df['Action'] = np.where(df['Marker_change'] > 0, "BUY", "")

    def turtle_split(row):
        if row['Action'] == 'BUY':
            return baseQ * (turtle ** row['Marker'] - 1) // (turtle - 1)
        else:
            return 0
    df['Traded_q'] = df.apply(turtle_split, axis=1).round(0).astype(int)
    df['Net_q'] = df['Traded_q'].cumsum().round(0).astype(int)
    print(df.head(39))
    return df
Sudipto Ghosh
  • 416
  • 2
  • 8