0

I'm defining a simple if xxxx return y - else return NaN function. If the record, ['Product'], equals ['Product'] offset by 8 then the if condition is true.

I've tried calling the record and setting it equal to itself offset by 8 using == and .shift(8). ['Product'] is a string and ['Sales'] is an integer.

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().

Dave
  • 37
  • 5

2 Answers2

0

Firstly a general comment from StackOverflow's Truth value of a Series is ambiguous...:

The or and and python statements require truth-values. For pandas these are considered ambiguous so you should use "bitwise" | (or) or & (and) operations.

Secondly, you use == on Series objects. For this Pandas tries to convert the first object to a truth value - and fails, because this is ambiguous.

halloleo
  • 9,216
  • 13
  • 64
  • 122
0

use X['Product'].equals(X['Product'].shift(8))

  • Using this, the function does complete when run `X['Growth_Sales'] = Growth(X)` but the results come up `NaN`; even when `['Product']` is equal to `['Product']` shifted by 8 – Dave Nov 01 '19 at 05:10
  • You need to look into how shift works, it shifts right and in the beginning it adds NaNs instead of circling the last values back to the front... 0,1,2,3,4 if shifted by 1 will be Nan,0,1,2,3 and not 4,0,1,2,3 – Rahasya Prabhakar Nov 01 '19 at 05:15
  • You should explore np.roll for cyclic shift – Rahasya Prabhakar Nov 01 '19 at 05:17
  • I am still getting NaN for each value in the new `['Growth_Sales']` column. I did not originally state the types. That has been added to the original question, my apologies. – Dave Nov 01 '19 at 05:48