Skip to content

BUG: sometimes when using ~ and & operators for indexing it evaluated incorrectly #61052

@adamszelestey-se

Description

@adamszelestey-se

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

Edit [rhshadrach]: The code below does not reproduce the issue.

# No idea how to exactly reproduce it, but it occurs sometimes. Logic is this:
import pandas as pd
bool_df = pd.DataFrame([
    {"first": True, "second": False, "third": True},
    {"first": True, "second": True, "third": True},
    {"first": True, "second": False, "third": True},
    {"first": True, "second": True, "third": True},
    {"first": True, "second": True, "third": True},
    {"first": True, "second": False, "third": True},
    {"first": True, "second": True, "third": True},
    {"first": False, "second": False, "third": True},
    {"first": True, "second": True, "third": True},
    {"first": True, "second": False, "third": True},
])
bool_df = bool_df[bool_df["third"]][["first", "second"]]
# In some cases, this line prints the length of the DataFrame (10)
print(len(bool_df[(~bool_df["first"]) & (~bool_df["second"])])) # Sometimes prints 10

# This line prints the expected output (1)
print(len(bool_df[(bool_df["first"] == False) & (bool_df["second"] == False)])) # Prints 1

# Using De Morgan's law also returned with the expected output
print(len(bool_df[~((bool_df["first"]) | (bool_df["second"]))])) # Prints: 1

Issue Description

We don't know when and why this occurs. We werre looking for any rational explanation for hours. Anyone else experienced similar? How could this be possible?
(Environment: MacBook Pro 2023, Sequoia 15.3)

Expected Behavior

print(len(bool_df[(~bool_df["first"]) & (~bool_df["second"])])) # Print 1

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.13.1
python-bits : 64
OS : Darwin
OS-release : 24.3.0
Version : Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6031
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 2.2.3
numpy : 2.2.3
pytz : 2025.1
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : None
IPython : 9.0.1
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.5
lxml.etree : 5.3.1
matplotlib : 3.10.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 19.0.1
pyreadstat : None
pytest : 8.3.5
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.2
sqlalchemy : 2.0.38
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : 0.23.0
tzdata : 2025.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds InfoClarification about behavior needed to assess issueNumeric 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