@@ -127,3 +127,65 @@ def mean(self):
127127 result = df .assign (b = pd .col ("a" ).xyz .mean ())
128128 expected = pd .DataFrame ({"a" : [1 , 2 , 3 ], "b" : [2.0 , 2.0 , 2.0 ]})
129129 tm .assert_frame_equal (result , expected )
130+
131+
132+ @pytest .mark .parametrize (
133+ ("expr" , "expected_values" , "expected_str" ),
134+ [
135+ (
136+ pd .col ("a" ) & pd .col ("b" ),
137+ [False , False , True , False ],
138+ "(col('a') & col('b'))" ,
139+ ),
140+ (
141+ pd .col ("a" ) & True ,
142+ [True , False , True , False ],
143+ "(col('a') & True)" ,
144+ ),
145+ (
146+ pd .col ("a" ) | pd .col ("b" ),
147+ [True , True , True , True ],
148+ "(col('a') | col('b'))" ,
149+ ),
150+ (
151+ pd .col ("a" ) | False ,
152+ [True , False , True , False ],
153+ "(col('a') | False)" ,
154+ ),
155+ (
156+ pd .col ("a" ) ^ pd .col ("b" ),
157+ [True , True , False , True ],
158+ "(col('a') ^ col('b'))" ,
159+ ),
160+ (
161+ pd .col ("a" ) ^ True ,
162+ [False , True , False , True ],
163+ "(col('a') ^ True)" ,
164+ ),
165+ (
166+ ~ pd .col ("a" ),
167+ [False , True , False , True ],
168+ "(~col('a'))" ,
169+ ),
170+ ],
171+ )
172+ def test_col_logical_ops (
173+ expr : Expression , expected_values : list [bool ], expected_str : str
174+ ) -> None :
175+ # https://github.com/pandas-dev/pandas/issues/63322
176+ df = pd .DataFrame ({"a" : [True , False , True , False ], "b" : [False , True , True , True ]})
177+ result = df .assign (c = expr )
178+ expected = pd .DataFrame (
179+ {
180+ "a" : [True , False , True , False ],
181+ "b" : [False , True , True , True ],
182+ "c" : expected_values ,
183+ }
184+ )
185+ tm .assert_frame_equal (result , expected )
186+ assert str (expr ) == expected_str
187+
188+ # Test that the expression works with .loc
189+ result = df .loc [expr ]
190+ expected = df [expected_values ]
191+ tm .assert_frame_equal (result , expected )
0 commit comments