Skip to content

Conversation

Tarun2605
Copy link


What does this PR fix?

This pull request updates pandas' logical operations to use Kleene’s three-valued logic (handling True, False, and NA) for boolean arrays, ensuring that missing values (None, np.nan, pd.NA) propagate correctly in logical operations.

Previously, None inside a dtype=bool Series was coerced incorrectly, producing results inconsistent with three-valued logic. With this PR, logical operators now follow Kleene semantics consistently.


Core logic changes

  • Introduced new helper functions in pandas/core/ops/array_ops.py:
    • alignOutputWithKleene — aligns logical operation results with Kleene semantics.
    • is_nullable_bool — detects nullable boolean operands.
    • safe_is_true — ensures truthiness checks handle NA values safely.
  • Modified logical_op in array_ops.py to use Kleene logic for elementwise operations when operands are nullable booleans.

Test updates

  • Updated multiple tests in:
    • pandas/tests/frame/test_logical_ops.py
    • pandas/tests/series/test_logical_ops.py
  • Adjusted expected outputs to reflect NA propagation in and, or, and xor operations.
  • Covered cases with:
    • Missing values (None, np.nan, pd.NA)
    • Empty arrays
    • Label-based alignment
    • Explicit casting to boolean for object dtype containing NA

These changes ensure pandas’ logical operations with nullable booleans now conform to standard three-valued logic, improving correctness and consistency when handling missing data.

@jbrockmendel
Copy link
Member

Is this AI? We don’t use Kleenex logic except in opt-in dtypes.

@Tarun2605
Copy link
Author

Is this AI? We don’t use Kleenex logic except in opt-in dtypes.

Yes i did use AI to make my work easier and more efficient. Though i have tested it on my end.
So shouldnt it be mentioned in docs or somewhere that to get kleene's logic you must use proper opt-in dtype? because in that way we will be producing false results when using custom indexes with mismatches in them?

@jbrockmendel
Copy link
Member

Yes i did use AI to make my work easier and more efficient

Please don't. They are incredibly verbose and in general low quality. It creates more work for the maintainers.

So shouldnt it be mentioned in docs or somewhere that to get kleene's logic you must use proper opt-in dtype?

If you find somewhere in the docs that suggests otherwise, we can change that.

@mroeschke
Copy link
Member

The default, numpy bool dtype, intentionally doesn't not use Kleene logic and that probably won't change so closing.

@mroeschke mroeschke closed this Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: None in dtype=bool Series does not behave like 3-valued logic

3 participants