Skip to content Skip to sidebar Skip to footer

Ambiguous Truth Value With Boolean Logic

I am trying to use some boolean logic in a function on a dataframe, but get an error: In [4]: data={'level':[20,19,20,21,25,29,30,31,30,29,31]} frame=DataFrame(data) frame Out[4]:

Solution 1:

Inadequate use of the function max. np.maximum (perhaps np.ma.max as well as per numpy documentation) works. Apparently regular max can not deal with arrays (easily). Replacing

baseline=max(frame['level'],frame['level'].shift(1))#doesnt work

with

baseline=np.maximum(frame['level'],frame['level'].shift(1))

does the trick. I removed the other part to make it easier to read:

In [23]:
#q 1 analysis
def calculate_rowise(x):
    baseline=np.maximum(frame['level'],frame['level'].shift(1))#works
    return baseline
frame.apply(calculate_rowise)

Out[23]:
level
0   NaN
1202203214255296307318319301031

PS the original problem is hiding another issue that shows up when taking out the shift portion of the function. The return shape doesn't match, but thats another problem, just mentioning it here for full disclosure

Post a Comment for "Ambiguous Truth Value With Boolean Logic"