pandas 系列的真值是不明确的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32699034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:54:45  来源:igfitidea点击:

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

pythonpandas

提问by Ron

my inputs are, data input at top

我的输入是,顶部的数据输入

Date        Time    Open    High    Low    Close    Volume
02/01/2015  14:30   79.52   79.73   79.52   79.71   10841
02/01/2015  14:31   79.6999 79.6999 79.61   79.67   1426
02/01/2015  14:32   79.69   79.7    79.69   79.7    800
02/01/2015  14:33   79.7    79.759  79.7    79.759  1158
02/01/2015  14:34   79.747  79.747  79.73   79.73   1125
02/01/2015  14:35   79.73   79.73   79.71   79.71   301
02/01/2015  14:36   79.8    79.82   79.75   79.82   7818
02/01/2015  14:37   79.82   79.83   79.778  79.82   6985
02/01/2015  14:38   79.8099 79.8099 79.72   79.72   1026
02/01/2015  14:39   79.77   79.78   79.7396 79.78   870
02/01/2015  14:40   79.79   79.79   79.73   79.73   12031
02/01/2015  14:41   79.76   79.76   79.6901 79.736  4642
02/01/2015  14:42   79.7301 79.744  79.7301 79.744  1099


numRow = 1
endRow = 50
dHaGreen0 = {}
for ticker in Index_tickers.ticker:
    index_data = pd.read_csv('C:\Users\me\Dropbox\MktData\Index_list\' +     ticker + '_' + str(timeFrame) + '.csv')
    open0 = index_data.Open
    close0 = index_data.Close
    high0 = index_data.High
    low0 = index_data.Low
    dHaClose0 = (open0 + high0 + low0 + close0) / 4 
    dHaOpen0 = (dHaClose1 + dHaOpen1) / 2
    dHaGreen0 = dHaClose0 >= dHaOpen0

        if numRow < endRow:
            dOpen0 = index_data.ix[numRow, index_data.Open]

            if dHaGreen0 == True & dHaGreen1 == True:
                dPosition = "Long"
            numRow = numRow + 1  

when I print dHaGreen0, it returns a list of bool, and "dtype: bool". But when I go to use dHaGreen0 in the if statement , I get the error ValueError:

当我打印 dHaGreen0 时,它返回一个 bool 列表和“dtype:bool”。但是当我在 if 语句中使用 dHaGreen0 时,出现错误 ValueError:

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

系列的真值是不明确的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

I have checked many of the suggested answers from previous questions but can't get read across. All responses are greatly appreciated.

我已经检查了以前问题中的许多建议答案,但无法阅读。非常感谢所有的回应。

回答by Chad Kennedy

The error you're seeing is correct. dHaGreen0 is an array of bool values. When you ask "dHaGreen0 == True", you need to decide on one of the two following interpretations:

您看到的错误是正确的。dHaGreen0 是一个布尔值数组。当您询问“dHaGreen0 == True”时,您需要决定以下两种解释之一:

Do you mean if anyvalue is dHaGreen0 is True? If so, replace "dHaGreen0 == True" with "any(dHaGreen0) == True".

你的意思是如果有任何值是 dHaGreen0 是真的吗?如果是这样,请将“dHaGreen0 == True”替换为“any(dHaGreen0) == True”。

Or do you mean if allvalues in dHaGreen0 are True? If so, replace "dHaGreen0 == True" with "all(dHaGreen0) == True".

或者您的意思是dHaGreen0 中的所有值是否为 True?如果是这样,请将“dHaGreen0 == True”替换为“all(dHaGreen0) == True”。

Similarly for dHaGreen1

同样对于 dHaGreen1

If you're trying to do this for each pair:

如果您尝试为每一对执行此操作:

dPosition = dHaGreen0 & dHaGreen1

This will then give you an array of booleans giving the result for each pair.

然后这会给你一个布尔数组,给出每对的结果。