Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

python - ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() no idea

I have two dataframes:

last_signal:

Unnamed: 0  coresym    Lots         open_orders    direction
0   0       AUDUSD     110.39       0.0             9.0
1   1       EURUSD     88.19        0.0             9.0
2   2       GBPUSD     -87.65       0.0             9.0

signal:

    coresym Lots    open_orders direction
0   AUDUSD  250.00  0.0         1.0
1   EURUSD  112.50  0.0         0.0
2   GBPUSD  -97.11  0.0         0.0

I want to change the signal's value based on last_signal:

if ((last_signal[last_signal['coresym']=='AUDUSD']['direction'] == 9) & (signal[signal['coresym']=='AUDUSD']['Lots'] >100)):
    signal[signal['coresym']=='AUDUSD']['direction'] = 9

I got the error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-36-0b88b27047e9> in <module>
----> 1 if ((last_signal[last_signal['coresym']=='AUDUSD']['direction'] == 9) & (signal[signal['coresym']=='AUDUSD']['Lots'] >100)):
      2     signal[signal['coresym']=='AUDUSD']['direction'] = 9

~anaconda3libsite-packagespandascoregeneric.py in __nonzero__(self)
   1327 
   1328     def __nonzero__(self):
-> 1329         raise ValueError(
   1330             f"The truth value of a {type(self).__name__} is ambiguous. "
   1331             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried to debug myself:

last_signal[last_signal['coresym']=='AUDUSD']['direction'] == 9
0    True
Name: direction, dtype: bool
signal[signal['coresym']=='AUDUSD']['Lots'] >100
0    True
Name: Lots, dtype: bool
((last_signal[last_signal['coresym']=='AUDUSD']['direction'] == 9) & (signal[signal['coresym']=='AUDUSD']['Lots'] >100))
0    True
dtype: bool

I really dont understand why, I assume others may have the same problem, so I post it here

question from:https://stackoverflow.com/questions/65599711/valueerror-the-truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you notice from the final output, you get a series after applying all your operations, hence you can't get its truth value like this:

condition = ((last_signal[last_signal['coresym']=='AUDUSD']['direction'] == 9) & (signal[signal['coresym']=='AUDUSD']['Lots'] >100))
truth_value = bool(condition) # will give you the current error

hence giving you the issue with your if statement. To overcome it, do this

if condition.any():
    # your code

.any() for a series containing a single True (at-least one True is enough for .any() to evaluate as True) will evaluate to be True. So basically do this:

if ((last_signal[last_signal['coresym']=='AUDUSD']['direction'] == 9) & (signal[signal['coresym']=='AUDUSD']['Lots'] >100)).any():
    signal[signal['coresym']=='AUDUSD']['direction'] = 9

Since the series you get after using & contains only one boolean, you can use .bool() too:

((last_signal[last_signal['coresym']=='AUDUSD']['direction'] == 9) & (signal[signal['coresym']=='AUDUSD']['Lots'] >100)).bool()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...