Python throws ValueError: The truth value of a series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() when you try to get the bool of Pandas.Series
. This issue occurs when you try to use OR
, AND
, IF
, WHILE
over two series. These operators require truth values in order to decide the outcome.
True OR True = True, but [1] OR [] = ?
Series comparison is not defined in Pandas.Series
. The solution is to use bitwise operators (|, &) which will do element wise comparison.
Code Example
Suppose you have a series and you want the bool type of it, the code for it will be –
>>> import pandas as pd >>> series_x = pd.Series([1]) >>> bool(series_x) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
You see, it raised the valueerror: truth value of series in ambiguous. Pandas do not support this. Similarly, for other operators like OR, AND, IF, WHILE it will raise error.
>>> series_x or series_x ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). >>> series_x and series_x ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). >>> if series_x: ... print('Series not empty') ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). >>> while series_x: ... print('Series looped') ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Solution
The solution to this error is to use bitwise and/or (|, &) to compare element wise in series. Also, you can use functions provided by numpy like np.logical_or
and np.logical_and
. Check this code –
>>> import pandas as pd >>> import numpy as np >>> series_x = pd.Series([1, 2, 3, 4]) >>> series_y = pd.Series([5, 6, 7, 8]) >>> print(np.logical_or(series_x, series_y)) OUTPUT: 0 True 1 True 2 True 3 True dtype: bool >>> print(series_x | series_y) OUTPUT: 0 5 1 6 2 7 3 12 dtype: int64 >>> print(np.logical_and(series_x, series_y)) OUTPUT: 0 True 1 True 2 True 3 True dtype: bool >>> print(series_x & series_y) OUTPUT: 0 1 1 2 2 3 3 0 dtype: int64
The error message includes few functions like a.empty
, a.bool()
, a.item()
etc. which could be helpful in loops and conditions. For example, if you just want to check if series contains more than zero elements, then you can use .empty
.
>>> series_x = pd.Series([]) >>> print(series_x.empty) True >>> series_y = pd.Series([1]) >>> print(series_y.empty) False
For if
conditions and loops, you can either use .emtpy
or you can get the size of series using .size
and decide if its empty. The .all()
function will return a bool value depending on the items of series. It acts as AND between the items. So, if any item is 0 or False, then the output by .all()
will be False. On the other hand, .any()
works as OR, so if at least one item is True, it will return True.
>>> series_x = pd.Series([1, 2, 3, 4, 5]) >>> series_y = pd.Series([0, 1, 2, 3]) >>> series_z = pd.Series([0, 0, 0]) >>> print(series_x.all()) Output: True >>> print(series_y.all()) Output: False >>> print(series_z.all()) Output: False >>> print(series_x.any()) Output: True >>> print(series_y.any()) Output: True >>> print(series_z.any()) Output: False
.bool()
is used when there is only one element in the series and in boolean type. .item()
return the only element of single item series.
>>> series_x = pd.Series([0]) >>> series_y = pd.Series([1]) >>> series_z = pd.Series([True]) >>> series_u = pd.Series([False]) >>> series_v = pd.Series([50]) >>> series_w = pd.Series([50, 60]) >>> print(series_x.bool()) Output: Error! Works with boolean values only >>> print(series_z.bool()) True >>> print(series_u.bool()) False >>> print(series_x.item()) 0 >>> print(series_w.item()) Output: Error! Works with single value series only >>> print(series_z.item()) True