Skip to content

BUG: The DataFrame object's element bool check can NOT work with empty list  #41582

Open
@TigerZhao007

Description

@TigerZhao007
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

df = pd.DataFrame({'A': [1, [], 3],'B': [4, 5, '']})
df == ''  # element bool check work well with empty str
df == []  # want to find empty list
# raise ValueError(
# ValueError: Unable to coerce to Series, length must be 2: given 0

Problem description

[this should explain why the current behaviour is a problem and why the expected output is a better solution]
It is a very common requirement to check whether the elements in the DataFrame are empty list.

Pandas.DataFrame's "element bool check" works well with empty str '', as illustrated. But did not work with empyt list.

Although user could use : df.astype(bool), this method could NOT tell whether 'False' is a empty list or a empty str.

Expected Output

df == [] # want to find empty list

Expected Output as follows:

‘’‘
A B
0 False False
1 True False
2 False False
’‘’

Output of pd.show_versions()

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

INSTALLED VERSIONS

commit : 2cb9652
python : 3.9.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Chinese (Simplified)_China.936
pandas : 1.2.4
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.1
setuptools : 56.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNested DataData where the values are collections (lists, sets, dicts, objects, etc.).Numeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions