Skip to content

Conversation

@majiayu000
Copy link

Summary

Add __and__, __rand__, __or__, __ror__, __xor__, __rxor__, __invert__ methods to Expression class to support combining conditions with &, |, ^, ~ operators in pd.col expressions.

Example

import pandas as pd
import io

data = '''datetime,cfs
2018-02-28,350
2018-03-15,420
2019-04-10,380
2019-06-01,450
'''
dd = pd.read_csv(io.StringIO(data), parse_dates=['datetime']).set_index('datetime')

# This now works with & operator
result = (dd
 .reset_index()
 .sort_values('datetime')
 .loc[(pd.col('datetime') > '2018-03-01') & (pd.col('datetime') <= '2019-05-31')]
 .assign(cfs=pd.col('cfs').clip(upper=400))
)

Closes #63322

Add __and__, __rand__, __or__, __ror__, __xor__, __rxor__, __invert__
methods to Expression class to support combining conditions with
&, |, ^, ~ operators in pd.col expressions.

Closes pandas-dev#63322
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Can you add tests.

Add comprehensive tests for the new logical operators (__and__, __or__,
__xor__, __invert__) in the Expression class:

- test_col_logical_ops: Parametrized tests for &, |, ^, ~ operators
  with both column-to-column and column-to-scalar operations
- test_col_logical_ops_combined: Integration test demonstrating
  real-world usage in DataFrame.loc[] with combined conditions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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: pd.col does not support & for combining conditions in .loc

2 participants