Skip to content

Commit 419fab8

Browse files
committed
BUG: Add logical operators to pd.col Expression class
Add __and__, __rand__, __or__, __ror__, __xor__, __rxor__, __invert__ methods to Expression class to support combining conditions with &, |, ^, ~ operators in pd.col expressions. Closes #63322
1 parent 944c527 commit 419fab8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

pandas/core/col.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
"__lt__": "<",
3838
"__eq__": "==",
3939
"__ne__": "!=",
40+
"__and__": "&",
41+
"__rand__": "&",
42+
"__or__": "|",
43+
"__ror__": "|",
44+
"__xor__": "^",
45+
"__rxor__": "^",
4046
}
4147

4248

@@ -157,6 +163,28 @@ def __mod__(self, other: Any) -> Expression:
157163
def __rmod__(self, other: Any) -> Expression:
158164
return self._with_binary_op("__rmod__", other)
159165

166+
# Logical ops
167+
def __and__(self, other: Any) -> Expression:
168+
return self._with_binary_op("__and__", other)
169+
170+
def __rand__(self, other: Any) -> Expression:
171+
return self._with_binary_op("__rand__", other)
172+
173+
def __or__(self, other: Any) -> Expression:
174+
return self._with_binary_op("__or__", other)
175+
176+
def __ror__(self, other: Any) -> Expression:
177+
return self._with_binary_op("__ror__", other)
178+
179+
def __xor__(self, other: Any) -> Expression:
180+
return self._with_binary_op("__xor__", other)
181+
182+
def __rxor__(self, other: Any) -> Expression:
183+
return self._with_binary_op("__rxor__", other)
184+
185+
def __invert__(self) -> Expression:
186+
return Expression(lambda df: ~self(df), f"(~{self._repr_str})")
187+
160188
def __array_ufunc__(
161189
self, ufunc: Callable[..., Any], method: str, *inputs: Any, **kwargs: Any
162190
) -> Expression:

0 commit comments

Comments
 (0)