-
Notifications
You must be signed in to change notification settings - Fork 40
feat: add does_not_start_with and does_not_end_with operators #1791
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Collaborator
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. Could you please add cases for when 'value_is_literal=True'?
Author
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. Added in f865a42: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,6 +134,121 @@ def test_ends_with(data, comparator, dataset_type, expected_result): | |
| assert result.equals(df.convert_to_series(expected_result)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "data,comparator,dataset_type,expected_result", | ||
| [ | ||
| ( | ||
| {"target": ["Att", "Btt", "Ctt"], "VAR2": ["A", "B", "D"]}, | ||
| "VAR2", | ||
| PandasDataset, | ||
| [False, False, True], | ||
| ), | ||
| ( | ||
| {"target": ["Att", "Btt", "Ctt"], "VAR2": ["A", "B", "D"]}, | ||
| "VAR2", | ||
| DaskDataset, | ||
| [False, False, True], | ||
| ), | ||
| ], | ||
| ) | ||
| def test_does_not_start_with(data, comparator, dataset_type, expected_result): | ||
| df = dataset_type.from_dict(data) | ||
| dataframe_type = DataframeType({"value": df}) | ||
| result = dataframe_type.does_not_start_with( | ||
| {"target": "target", "comparator": comparator} | ||
| ) | ||
| assert result.equals(df.convert_to_series(expected_result)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "data,comparator,dataset_type,expected_result", | ||
| [ | ||
| ( | ||
| {"target": ["Att", "Btt", "Ctt"], "VAR2": ["tt", "B", "D"]}, | ||
| "VAR2", | ||
| PandasDataset, | ||
| [False, False, False], | ||
|
Collaborator
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. Am I misunderstanding, or shouldn't these be |
||
| ), | ||
| ( | ||
| {"target": ["Att", "Btt", "Ctt"], "VAR2": ["tt", "B", "D"]}, | ||
| "VAR2", | ||
| DaskDataset, | ||
| [False, False, False], | ||
| ), | ||
| ], | ||
| ) | ||
| def test_does_not_end_with(data, comparator, dataset_type, expected_result): | ||
| df = dataset_type.from_dict(data) | ||
| dataframe_type = DataframeType({"value": df}) | ||
| result = dataframe_type.does_not_end_with( | ||
| {"target": "target", "comparator": comparator} | ||
| ) | ||
| assert result.equals(df.convert_to_series(expected_result)) | ||
|
|
||
|
|
||
| # value_is_literal=True: comparator is a literal string, not a column name. | ||
| @pytest.mark.parametrize( | ||
| "data,comparator,dataset_type,expected", | ||
| [ | ||
| ({"target": ["Att", "Btt", "Axx"]}, "A", PandasDataset, [True, False, True]), | ||
| ({"target": ["Att", "Btt", "Axx"]}, "A", DaskDataset, [True, False, True]), | ||
| ], | ||
| ) | ||
| def test_starts_with_literal(data, comparator, dataset_type, expected): | ||
| df = dataset_type.from_dict(data) | ||
| dataframe_type = DataframeType({"value": df}) | ||
| opts = {"target": "target", "comparator": comparator, "value_is_literal": True} | ||
| assert dataframe_type.starts_with(opts).equals(df.convert_to_series(expected)) | ||
| assert dataframe_type.does_not_start_with(opts).equals( | ||
| df.convert_to_series([not b for b in expected]) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "data,comparator,dataset_type,expected", | ||
| [ | ||
| ({"target": ["Att", "Btt", "Cxx"]}, "tt", PandasDataset, [True, True, False]), | ||
| ({"target": ["Att", "Btt", "Cxx"]}, "tt", DaskDataset, [True, True, False]), | ||
| ], | ||
| ) | ||
| def test_ends_with_literal(data, comparator, dataset_type, expected): | ||
| df = dataset_type.from_dict(data) | ||
| dataframe_type = DataframeType({"value": df}) | ||
| opts = {"target": "target", "comparator": comparator, "value_is_literal": True} | ||
| assert dataframe_type.ends_with(opts).equals(df.convert_to_series(expected)) | ||
| assert dataframe_type.does_not_end_with(opts).equals( | ||
| df.convert_to_series([not b for b in expected]) | ||
| ) | ||
|
|
||
|
|
||
| # Missing target values must stay null-safe: starts_with/ends_with return a clean | ||
| # boolean Series (missing -> False) so the negating operators don't crash on `~`. | ||
| @pytest.mark.parametrize("dataset_type", [PandasDataset, DaskDataset]) | ||
| def test_does_not_start_with_is_null_safe(dataset_type): | ||
| df = dataset_type.from_dict({"target": ["Att", None, "Ctt"]}) | ||
| dataframe_type = DataframeType({"value": df}) | ||
| opts = {"target": "target", "comparator": "A", "value_is_literal": True} | ||
| assert dataframe_type.starts_with(opts).equals( | ||
| df.convert_to_series([True, False, False]) | ||
| ) | ||
| assert dataframe_type.does_not_start_with(opts).equals( | ||
| df.convert_to_series([False, True, True]) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dataset_type", [PandasDataset, DaskDataset]) | ||
| def test_does_not_end_with_is_null_safe(dataset_type): | ||
| df = dataset_type.from_dict({"target": ["Att", None, "Cxx"]}) | ||
| dataframe_type = DataframeType({"value": df}) | ||
| opts = {"target": "target", "comparator": "tt", "value_is_literal": True} | ||
| assert dataframe_type.ends_with(opts).equals( | ||
| df.convert_to_series([True, False, False]) | ||
| ) | ||
| assert dataframe_type.does_not_end_with(opts).equals( | ||
| df.convert_to_series([False, True, True]) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "data,comparator,dataset_type,expected_result", | ||
| [ | ||
|
|
||
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.
Starts_with and ends_with currently returns raw result of pandas string methods. These methods can produce nullable/objects results when target values is missing. This can mix badly with use of '
'. Other operators safely use '' when positive has already normalized its output to boolean series. Please make starts_with and ends_with null safe.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.
Fixed in f865a42. Confirmed: with a missing target value,
starts_withreturned[True, None, False](object dtype) anddoes_not_start_withthen raisedTypeError: bad operand type for unary ~: 'NoneType'. Bothstarts_withandends_withnow passna=Falsetostr.startswith/str.endswith, so the result is always a clean boolean Series (missing -> False) that negates safely. Verified on PandasDataset and DaskDataset.