W3docs

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

The truth value of a Series in pandas can be ambiguous, as it can contain multiple values.

The truth value of a Series in pandas can be ambiguous, as it can contain multiple values. To check if a Series is empty, you can use the .empty attribute:

Checking if a Series is empty

import pandas as pd

s = pd.Series([1, 2, 3])
print(s.empty) # False

s = pd.Series([])
print(s.empty) # True

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

To check if any of the values in a Series are true, you can use the .any() method:

Checking if any values are true

import pandas as pd

s = pd.Series([True, False, False])
print(s.any()) # True

s = pd.Series([False, False, False])
print(s.any()) # False

To check if all of the values in a Series are true, you can use the .all() method:

Checking if all values are true

import pandas as pd

s = pd.Series([True, True, True])
print(s.all()) # True

s = pd.Series([True, False, True])
print(s.all()) # False

To extract a single element from a Series, you can use the .item() method:

Extracting a single element

import pandas as pd

s = pd.Series([1])
print(s.item()) # 1

To convert a Series to a single boolean value, you can use the .bool() method:

Converting to a boolean value

import pandas as pd

s = pd.Series([1])
print(s.bool()) # True

Note: Direct boolean evaluation (e.g., if series:) raises the ambiguity error. Use .any() or .all() to safely evaluate truthiness, as they handle NaN values predictably without raising exceptions.