It seems I can apply some functions without problems to a DataFrame, but other give a Value Error.
dates = pd.date_range('20130101',periods=6)
data = np.random.randn(6,4)
df = pd.DataFrame(data,index=dates,columns=list('ABCD'))
def my_max(y):
return max(y,0)
def times_ten(y):
return 10*y
df.apply(lambda x:times_ten(x)) # Works fine
df.apply(lambda x:my_max(x)) # Doesn't work
The first apply works fine, the second one generates a:
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index A')
I know I can generate the "max(df,0)" in other ways (e.g. by df[df<0]=0), so I'm not looking for a solution to this particular problem. Rather, I'm interested in why the apply above doesn't work.
With a single iterable argument, return its largest item. With two or more arguments, return the largest argument.so it doesn't understand pandas Series or numpy arrays in this context which is why you should use something that does