jeudi 31 octobre 2019

How to include a string being equal to itself shifted as a coniditon in a function definition?

I'm defining a simple if xxxx return y - else return NaN function that I want the if condition to be true when the input string is equal to input offset by 8 records

I've tried calling the record and setting it equal to itself offset by 8 using == and .shift(8)

def Growth (X):
    if X['Product'] == X['Product'].shift(8):
        return (1+ X['Sales'].shift(4)) / (1+ X['Sales'].shift(8) - 1)
    else:
        return 'NaN'

I expect the output to be NaN for the first 8 records, and then to have numbers at record 9, but I receive the error instead.

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

Aucun commentaire:

Enregistrer un commentaire