From dddcb83a074e7f0088565cf591fab72be4b64215 Mon Sep 17 00:00:00 2001 From: zhangbowen-coder <2439796518@qq.com> Date: Thu, 11 Dec 2025 12:15:28 +0800 Subject: [PATCH 1/4] Add support for operator & in class Expression. --- pandas/core/col.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/core/col.py b/pandas/core/col.py index 39c4a7fd016c2..be90658ed54ba 100644 --- a/pandas/core/col.py +++ b/pandas/core/col.py @@ -37,6 +37,8 @@ "__lt__": "<", "__eq__": "==", "__ne__": "!=", + "__and__" : "&", + "__rand__" : "&", } @@ -156,6 +158,10 @@ def __mod__(self, other: Any) -> Expression: def __rmod__(self, other: Any) -> Expression: return self._with_binary_op("__rmod__", other) + def __and__(self, other: Any) -> Expression: + return self._with_binary_op("__and__", other) + def __rand__(self, other: Any) -> Expression: + return self._with_binary_op("__rand__", other) def __array_ufunc__( self, ufunc: Callable[..., Any], method: str, *inputs: Any, **kwargs: Any From fb6415ddd4231ab6a7a4e0993471d20f5d139dd6 Mon Sep 17 00:00:00 2001 From: zhangbowen-coder <2439796518@qq.com> Date: Thu, 11 Dec 2025 13:28:27 +0800 Subject: [PATCH 2/4] update --- pandas/core/col.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/col.py b/pandas/core/col.py index be90658ed54ba..814b714bdbe2f 100644 --- a/pandas/core/col.py +++ b/pandas/core/col.py @@ -37,8 +37,8 @@ "__lt__": "<", "__eq__": "==", "__ne__": "!=", - "__and__" : "&", - "__rand__" : "&", + "__and__": "&", + "__rand__": "&", } @@ -158,8 +158,10 @@ def __mod__(self, other: Any) -> Expression: def __rmod__(self, other: Any) -> Expression: return self._with_binary_op("__rmod__", other) + def __and__(self, other: Any) -> Expression: return self._with_binary_op("__and__", other) + def __rand__(self, other: Any) -> Expression: return self._with_binary_op("__rand__", other) From 0319203c23314b94e7077fa36da219d328cc8b10 Mon Sep 17 00:00:00 2001 From: zhangbowen-coder <2439796518@qq.com> Date: Sat, 13 Dec 2025 21:37:34 +0800 Subject: [PATCH 3/4] Tests added. --- pandas/tests/test_col.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pandas/tests/test_col.py b/pandas/tests/test_col.py index c884540abfed0..6883532432465 100644 --- a/pandas/tests/test_col.py +++ b/pandas/tests/test_col.py @@ -46,6 +46,36 @@ def test_col_simple( assert str(expr) == expected_str +@pytest.mark.parametrize( + ("expr", "expected_values", "expected_str"), + [ + ( + (pd.col("a") >= 3) & (pd.col("a") <= 5), + [3, 4, 5], + "((col('a') >= 3) & (col('a') <= 5))", + ), + ( + (pd.col("b") >= 20) & (pd.col("a") <= 5), + [1, 2, 3], + "((col('b') >= 20) & (col('a') <= 5))", + ), + ( + (pd.col("b") >= 20) & (pd.col("a") <= 5) & (pd.col("b") < 22), + [2, 3], + "(((col('b') >= 20) & (col('a') <= 5)) & (col('b') < 22))", + ), + ], +) +def test_col_bool( + expr: Expression, expected_values: list[object], expected_str: str +) -> None: + df = pd.DataFrame({"a": list(range(1, 21)), "b": list(range(22, 2, -1))}) + result = df.loc[expr] + ls = result["a"].tolist() + assert ls == expected_values + assert str(expr) == expected_str + + @pytest.mark.parametrize( ("expr", "expected_values", "expected_str"), [ From ecba02a77af21397ffe95640f795e4f47ae6a0ff Mon Sep 17 00:00:00 2001 From: zhangbowen-coder <2439796518@qq.com> Date: Sat, 13 Dec 2025 21:46:25 +0800 Subject: [PATCH 4/4] .rst entry added --- doc/source/whatsnew/v3.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index bebd928924214..45cd176e6e7dd 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -1423,6 +1423,7 @@ Other array if the array shares data with the original DataFrame or Series (:ref:`copy_on_write_read_only_na`). This logic is expanded to accessing the underlying pandas ExtensionArray through ``.array`` (or ``.values`` depending on the dtype) as well (:issue:`61925`). +- Fixed bug that pd.col does not support & for combining conditions in .loc (:issue:`63322`) .. ***DO NOT USE THIS SECTION***