Signup/Sign In
PUBLISHED ON: MAY 1, 2021

Python Pandas Series.bool() Method

In this tutorial, we will learn the python pandas Series.bool() method. Using this method we check whether the given Series consisting of a single bool as an element or not. The element must be a boolean scalar value, either True or False. It returns the bool, the same value present in the Series. The Series.bool() method raises a ValueError, if the Series contains more than one element and if the element is not boolean.

The below shows the syntax of the Series.bool() method.

Syntax

Series.bool()

Example: Checking a Series using Series.bool() Method

Here, in this example, we have created two Series which consist of a single boolean element. Let's check these two Series with the Series.bool() method. As the two series consist of a single boolean element, the Series.bool() method returns the bool values present in the Series. See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
series_1 = pd.Series([True])
series_2 = pd.Series([False])
print("The element present in the Series_1 is",series_1)
print("The element present in the Series_2 is",series_2)
print("-------------------------------------------------")
print("The Series_1 contains a single bool value:",series_1.bool())
print("The Series_2 contains a single bool value:",series_2.bool())


The element present in the Series_1 is 0 True
dtype: bool
The element present in the Series_2 is 0 False
dtype: bool
-------------------------------------------------
The Series_1 contains a single bool value: True
The Series_2 contains a single bool value: False

Example: Checking a Series using Series.bool() Method

Here, in this example, we have created two Series which consist of a boolean element more than one. When we apply Series.bool() method to these Series, we will get ValueError. See the below example.

The Series.bool() method raises a ValueError, if the Series contains more than one boolean element.

#importing pandas as pd
import pandas as pd
#creating Series
series_1 = pd.Series([True,False])
series_2 = pd.Series([True,True])
print("Does the Series_1 contains single bool value:",series_1.bool())
print("Does the Series_2 contains single bool value:",series_2.bool())


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

Example: Checking a Series using Series.bool() Method

Here, in this example, we have created two Series which consist of a single integer values. The Series.bool() method raises a ValueError, as the Series consists of non-boolean values (integers). See the below example.

#importing pandas as pd
import pandas as pd
#creating Series
series_1 = pd.Series([0])
series_2 = pd.Series([1])
print("Does the Series_1 contains single bool value:",series_1.bool())
print("Does the Series_2 contains single bool value:",series_2.bool())


ValueError: bool cannot act on a non-boolean single element Series

Conclusion

In this tutorial, we learned the python pandas Series.bool() method. By solving different examples we understood how this method works.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.