Boolean series bug @ Erwin | 2021-08-24T16:18:36+08:00 | 1 minute read | Update at 2023-12-06T00:27:08Z

We can’t use “in” operator to check if False or True in a boolean series.

Problem

Current version: pandas 1.3.2 with python 3.8.5

Example:

a = [False, True, True, True]
b = [True, True]
c = [False, False]

s_a = pd.Series(a)
s_b = pd.Series(b)
s_c = pd.Series(c)

Test if “in” can work in list:

True in a, True in b, True in c
(True, True, False)
False in a, False in b, False in c
(True, False, True)

Test if “in” can work in Series:

True in s_a, True in s_b, True in s_c
(True, True, True)
False in s_a, False in s_b, False in s_c
(True, True, True)

We can find when we use “in” to judge whether True of False in a boolean Series, it must return True.

Test whether “in” is normal for other Series:

1 in pd.Series([1,2,3]), 4 in pd.Series([1,2,3]), 
(True, False)

So the “in” is normal in integer Series.

How

We can use all() and any function to check boolean value in Series.

s_a.all(), s_a.any()
(False, True)

Summary

We can’t use “in” to check whether a boolean value in a Series, we can use all() and any() function replace it.

© 2020 - 2024 Li Yuanhao's Blog

Powered by Hugo with theme Dream.

avatar

Li Yuanhao's BlogJust do it

Social Links