关于pandas:Python:如果语句返回ValueError

Python: If statement return ValueError

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

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

我的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>> 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):引起的。


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

1
if df_1['timestamp'].iat[0] != df_o['timestamp'].iat[0]:
1
if df_1['timestamp'].values[0] != df_o['timestamp'].values[0]:
1
if df_1['timestamp'].item() != df_o['timestamp'].item():

Sample:

1
2
3
4
5
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: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

1
2
3
4
print (df_1['timestamp']!= df_o['timestamp'].tail(1))

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

但是,如果需要将Series列与scalar进行比较,则获取另一个True, False系列,并且不能使用if只使用scalars:

1
2
3
4
5
6
7
8
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

对于scalar,使用2种方法-any进行检查,至少使用一个Trueall检查所有值是否都是Trues:

1
2
3
4
5
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