Repository navigation
816 logic based assignment using pandas case when - #822
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements a new case_when wrangle that enables conditional value assignment based on pandas-style conditions, similar to SQL's CASE WHEN statement. The implementation uses numpy.select for efficient evaluation and includes comprehensive test coverage.
Key Changes:
- Added
case_whenfunction to main.py supporting conditional logic with multiple cases and optional default values - Implemented 12 comprehensive unit tests covering basic functionality, edge cases, and integration with existing wrangle features (where, if, variables)
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| wrangles/recipe_wrangles/main.py | Implements the case_when function with condition evaluation using df.eval and numpy.select |
| tests/recipes/wrangles/test_main.py | Adds TestCaseWhen class with 12 tests covering various scenarios including numeric/string conditions, empty dataframes, and variable substitution |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Just one small thing, but can you move the function to be in alphabetical order in main.py? In general, we keep all of the wrangles in alphabetical order for organization purposes. Otherwise, it looks good, love the tests. |
|
@thomasstvr I moved case_when() to the correct position |
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
a413c99 to
06d1b1b
Compare
|
2 things:
|
This pull request introduces comprehensive unit tests for the case_when wrangle and implements the case_when function in the main wrangles module. The tests cover a wide range of scenarios, ensuring the robustness and correctness of the new functionality.
New case_when wrangle implementation:
Adds the case_when function to main.py, allowing users to assign values to a column based on conditional logic using a list of cases and an optional default value. The function leverages numpy.select for efficient evaluation.
Extensive test coverage for case_when:
Adds the TestCaseWhen class to test_main.py with tests for:
Basic conditional assignments, numeric output, multiple and complex conditions, and string comparisons.
Special behaviors such as handling of where and if clauses, empty dataframes, missing defaults, and variable substitution in conditions.
Ensures that condition order is respected (first match wins), and that the wrangle is skipped if the if condition is false.
example of usage: `
"""
wrangles:
- case_when:
output: Match
default: Weak
cases:
- condition: (Score > 0.84) & (Type == 'Code')
value: Strong
- condition: (Score > 0.60) & (Type == 'Code')
value: Moderate
"""