'If statement is receiving "False" yet still executing?

I'm using Python 3.10 and pandas to read an excel file.

I want to look through a column named "Tissue Type" for specific substrings. For this I'm using "series.str.contains("substring")" from pandas. This returns a boolean.

When I print this boolean, the value returns false. Yet when I use it as the condition for an if statement, the if statement executes as if it were true. Here's my test code:

import pandas as pd

df = pd.read_excel("test.xlsx")

tissueType = pd.Series(['Tissue Type'])

#test before if

print ([tissueType.str.contains("testing false random noise", case = False)])

#test if

if [tissueType.str.contains("testing false random noise", case = False)]:
    print("test = true")

#test after if

print ([tissueType.str.contains("testing false random noise", case = False)])

And here is what is printed.

[0    False
dtype: bool]
test = true
[0    False
dtype: bool]

Process finished with exit code 0

So before and after the if statement, the test boolean is printed as false. Yet when used as a conditional, the if statement is executed. I'm so confused by this! Does anyone see my error? I'm guessing it's a syntax error to do with the if statement.

Thanks



Solution 1:[1]

Yeah, @John Gordon is right on this one. It returns an index/series and not just a simple boolean. Even if you only have one item in the series you're testing it on, it'll return as a list containing a single element. And lists passed through conditional statements are considered True if they have at least one element. Try

if [tissueType.str.contains("testing false random noise", case = False)[0]]:

or try

my_var = tissueType.str.contains("testing false random noise", case = False)

and then

if my_var[0]:
    foo bar

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aceinfurno