I have encountered an issue when calling a user defined function when using Pandas DataFrames. Stock market data is read in from a SQLite3 database in the following form:
date high low close
The following code sums high, low and close values for each row and adds a new column 'Sum' to df:
def Sum(h, l, c):
return h+l+c
df.loc[:, 'Sum'] = Sum(df['high'], df['low'], df['close'])
high low close Sum
date
2018-01-23 80.65 78.25 79.45 238.35
2018-01-24 81.65 79.50 80.50 241.65
2018-01-25 81.70 80.25 81.10 243.05
2018-01-26 81.25 78.25 78.75 238.25
2018-01-29 70.95 62.25 64.15 197.35
However, if the function is changed to return the maximum value of high, low, close for each row in df an error ("ValueError: The truth value of a Series is ambiguous.") is generated.
def Max(h, l, c):
return max(h, l, c)
df.loc[:, 'Max'] = Max(df['high'], df['low'], df['close'])
What is the issue with the Max function?