Hi
I am trying to write single line statement in Python as mentioned below
data['xyz'] = data['Close'].shift(1).isna if data['high'] else data['low']
but I am getting ValueError as mentioned below, kindly advice
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Hi Ahmed,
Can you elaborate what are you trying to calculate in the column 'xyz' and share your entire code, so that we can look into it.
Thanks
Hi Satyapriya,
Thanks for the reply. Pleaes find the below code. Basically, what I am trying to do is that if the previous cell of the series is NAN then xyz columne will be populated with LOW else it will be populated with HIGH
import datetime as dt
import yfinance as yf
import pandas as pd
import numpy as np
import math
import numpy as np
data = yf.download("MSFT",period="6mo")
data['xyz'] = (data['Adj Close'].shift(1).isna() if data['Low'] else data['High'])
Thanks
Ahmed
Hi Ahmed,
You can use the below line of code to do the same
data = data.assign(xyz=np.where(data['Adj Close'].isnull(), data['Low'], data['High']))
Hope this would help you.
Hi SSatyapriay,
Million thanks to you
Hi Ahmed,
There was an overlook from my end. Since you are checking the previous term, we should also use the shift operator. Please modify the above code as
data = data.assign(xyz=np.where(data['Adj Close'].shift(1).isnull(), data['Low'], data['High']))
Apologies for the inconvenience caused. Thanks.
Pandas follows the numpy convention of raising an error when you try to convert something to a bool. This happens in a if or when using the boolean operations, and, or, or not. It is not clear what the result of.
example
5 == pd.Series([12,2,5,10])
The result you get is a Series of booleans, equal in size to the pd.Series in the right hand side of the expression. So, you get an error. The problem here is that you are comparing a pandas pd.Series with a value, so you'll have multiple True and multiple False values, as in the case above. This of course is ambiguous, since the condition is neither True or False. You need to further aggregate the result so that a single boolean value results from the operation. For that you'll have to use either any or all depending on whether you want at least one (any) or all values to satisfy the condition.
(5 == pd.Series([12,2,5,10])).all()
# False
or
(5 == pd.Series([12,2,5,10])).any()
# True