首页 文章

Python:如果语句返回ValueError

提问于
浏览
-1

我用pandas创建一个if语句并返回错误,如下所示:

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

我的代码如下:

>>> df_1
    timestamp  open  high   low  close  adjusted_close  volume  \
0  2017-09-01  1.17  1.24  1.16    1.2             1.2   47932   
   dividend_amount  split_coefficient  
0              0.0                1.0  
>>> df_o
   timestamp  open  high   low  close  adjusted_close  volume  \
0  2017-08-31  1.15  1.27  1.06    1.29             1.29   97932   
   dividend_amount  split_coefficient  
0              0.0                1.0  
>>>if df_1['timestamp']!= df_o['timestamp'].tail(1):
....    print "different date"
>>>>Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vinus/.local/lib/python2.7/site-packages/pandas/core/generic.py", line 892, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

如何避免错误?这是由 if df_1['timestamp']!= df_o['timestamp'].tail(1): 造成的

1 回答

  • 0

    如果总是将一行 DataFrames 转换为标量,然后比较:

    if df_1['timestamp'].iat[0] != df_o['timestamp'].iat[0]:
    

    if df_1['timestamp'].values[0] != df_o['timestamp'].values[0]:
    

    if df_1['timestamp'].item() != df_o['timestamp'].item():
    

    Sample

    df_1 = pd.DataFrame({'timestamp':['2017-09-01']})
    df_o = pd.DataFrame({'timestamp':['2017-08-31']})
    
    if df_1['timestamp']!= df_o['timestamp'].tail(1):
        print ('not equal')
    

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

    print (df_1['timestamp']!= df_o['timestamp'].tail(1))
    
    0    True
    Name: timestamp, dtype: bool
    

    print ((df_1['timestamp']!= df_o['timestamp'].tail(1)).values[0])
    True
    

    但是如果需要比较 Series (列)和标量得到另一个 True, False 系列,并且不能仅使用带标量的 if

    df_1 = pd.DataFrame({'timestamp':['2017-09-01']})
    df_o = pd.DataFrame({'timestamp':['2017-08-31', '2017-09-01', '2017-06-10', '2017-08-12']})
    
    print (df_o['timestamp'].tail(3) != df_1['timestamp'].iat[0])
    1    False
    2     True
    3     True
    Name: timestamp, dtype: bool
    

    并且对于标量使用2种方法 - any用于检查至少一个 Trueall以检查是否所有值都是 True

    print ((df_o['timestamp'].tail(3) != df_1['timestamp'].iat[0]).any())
    True
    
    print ((df_o['timestamp'].tail(3) != df_1['timestamp'].iat[0]).all())
    False
    

相关问题