-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
BUG: Add logical operators to pd.col Expression class #63330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
419fab8
640dad1
d9bcbcf
7e12a38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -97,3 +97,93 @@ def mean(self): | |||||||||||
| result = df.assign(b=pd.col("a").xyz.mean()) | ||||||||||||
| expected = pd.DataFrame({"a": [1, 2, 3], "b": [2.0, 2.0, 2.0]}) | ||||||||||||
| tm.assert_frame_equal(result, expected) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| @pytest.mark.parametrize( | ||||||||||||
| ("expr", "expected_values", "expected_str"), | ||||||||||||
| [ | ||||||||||||
| # __and__ and __rand__ | ||||||||||||
| # a = [True, False, True, False], b = [False, True, True, True] | ||||||||||||
| # a & b = [False, False, True, False] | ||||||||||||
| ( | ||||||||||||
| pd.col("a") & pd.col("b"), | ||||||||||||
| [False, False, True, False], | ||||||||||||
| "(col('a') & col('b'))", | ||||||||||||
| ), | ||||||||||||
| ( | ||||||||||||
| pd.col("a") & True, | ||||||||||||
| [True, False, True, False], | ||||||||||||
| "(col('a') & True)", | ||||||||||||
| ), | ||||||||||||
| # __or__ and __ror__ | ||||||||||||
| ( | ||||||||||||
| pd.col("a") | pd.col("b"), | ||||||||||||
| [True, True, True, True], | ||||||||||||
| "(col('a') | col('b'))", | ||||||||||||
| ), | ||||||||||||
| ( | ||||||||||||
| pd.col("a") | False, | ||||||||||||
| [True, False, True, False], | ||||||||||||
| "(col('a') | False)", | ||||||||||||
| ), | ||||||||||||
| # __xor__ and __rxor__ | ||||||||||||
| # a ^ b = [True, True, False, True] | ||||||||||||
| ( | ||||||||||||
| pd.col("a") ^ pd.col("b"), | ||||||||||||
| [True, True, False, True], | ||||||||||||
| "(col('a') ^ col('b'))", | ||||||||||||
| ), | ||||||||||||
| ( | ||||||||||||
| pd.col("a") ^ True, | ||||||||||||
| [False, True, False, True], | ||||||||||||
| "(col('a') ^ True)", | ||||||||||||
| ), | ||||||||||||
| # __invert__ | ||||||||||||
| ( | ||||||||||||
| ~pd.col("a"), | ||||||||||||
| [False, True, False, True], | ||||||||||||
| "(~col('a'))", | ||||||||||||
| ), | ||||||||||||
| ], | ||||||||||||
| ) | ||||||||||||
| def test_col_logical_ops( | ||||||||||||
| expr: Expression, expected_values: list[object], expected_str: str | ||||||||||||
|
||||||||||||
| ) -> None: | ||||||||||||
| df = pd.DataFrame({"a": [True, False, True, False], "b": [False, True, True, True]}) | ||||||||||||
|
Comment on lines
144
to
146
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| result = df.assign(c=expr) | ||||||||||||
| expected = pd.DataFrame( | ||||||||||||
| { | ||||||||||||
| "a": [True, False, True, False], | ||||||||||||
| "b": [False, True, True, True], | ||||||||||||
| "c": expected_values, | ||||||||||||
| } | ||||||||||||
| ) | ||||||||||||
| tm.assert_frame_equal(result, expected) | ||||||||||||
| assert str(expr) == expected_str | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def test_col_logical_ops_combined() -> None: | ||||||||||||
|
||||||||||||
| """Test combining multiple logical operators like in DataFrame.loc[].""" | ||||||||||||
| df = pd.DataFrame( | ||||||||||||
| { | ||||||||||||
| "x": [1, 2, 3, 4, 5], | ||||||||||||
| "y": [10, 20, 30, 40, 50], | ||||||||||||
| } | ||||||||||||
| ) | ||||||||||||
| # Test combining conditions with & operator | ||||||||||||
| expr = (pd.col("x") > 2) & (pd.col("y") < 45) | ||||||||||||
| result = df.loc[expr] | ||||||||||||
| expected = df.loc[(df["x"] > 2) & (df["y"] < 45)] | ||||||||||||
| tm.assert_frame_equal(result, expected) | ||||||||||||
|
|
||||||||||||
| # Test combining conditions with | operator | ||||||||||||
| expr = (pd.col("x") == 1) | (pd.col("x") == 5) | ||||||||||||
| result = df.loc[expr] | ||||||||||||
| expected = df.loc[(df["x"] == 1) | (df["x"] == 5)] | ||||||||||||
| tm.assert_frame_equal(result, expected) | ||||||||||||
|
|
||||||||||||
| # Test negation with ~ | ||||||||||||
| expr = ~(pd.col("x") > 3) | ||||||||||||
| result = df.loc[expr] | ||||||||||||
| expected = df.loc[~(df["x"] > 3)] | ||||||||||||
| tm.assert_frame_equal(result, expected) | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove the comments throughout the parametrization.