-
-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: cannot filter an empty DataFrame by a non-numeric condition #59922
Comments
I understand that the errors is caused by Barring that, how do I create an empty frame that can be filtered by |
do I really need to type
instead of
|
Thanks for the report. I think pandas should consider dtype compatibility even on empty objects. Otherwise you end up with cases where it appears that code works, but only because the objects are empty, and then will fail in cases where they are not empty. |
I would like to propose potentially two solutions, to handle this:
(2) Add internal checks to automatically convert the index to a compatible type (e.g., Personally, I prefer the first option for its clarity. This would be especially helpful for beginner users. |
Isn't this immediately implied by saying
I'm opposed here. I do not think pandas should be silently changing dtypes on the user for the comparison. That could be surprising. |
So are you saying the implementation should be to return an empty dataframe as suggested by OP and not the Error? |
No - I think the current behavior of raising is the desired one. |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
filtering an empty
DataFrame
by a non-numeric filter fails withExpected Behavior
an empty frame should be returned, just like with a numeric condition
Installed Versions
INSTALLED VERSIONS
commit : 0691c5c
python : 3.12.6
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 7, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United States.1252
pandas : 2.2.3
numpy : 2.1.1
pytz : 2024.1
dateutil : 2.8.2
pip : 24.2
Cython : None
sphinx : None
IPython : 8.27.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.9.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : 5.3.0
matplotlib : 3.9.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : 0.23.0
tzdata : 2024.2
qtpy : 2.4.1
pyqt5 : None
The text was updated successfully, but these errors were encountered: