Skip to content

816 logic based assignment using pandas case when - #822

Merged
mborodii-prog merged 11 commits into
mainfrom
816-logic-based-assignment-using-pandas-case_when
Dec 3, 2025
Merged

mborodii-prog merged 11 commits into
mainfrom
816-logic-based-assignment-using-pandas-case_when

Conversation

@mborodii-prog

Copy link
Copy Markdown
Contributor

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
"""

@mborodii-prog mborodii-prog linked an issue Nov 18, 2025 that may be closed by this pull request
@thomasstvr
thomasstvr requested a review from Copilot November 19, 2025 14:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_when function 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.

Comment thread tests/recipes/wrangles/test_main.py Outdated
@thomasstvr

Copy link
Copy Markdown
Collaborator

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.

@mborodii-prog

Copy link
Copy Markdown
Contributor Author

@thomasstvr I moved case_when() to the correct position

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread wrangles/recipe_wrangles/main.py Outdated
Comment thread wrangles/recipe_wrangles/main.py Outdated
Comment thread tests/recipes/wrangles/test_main.py Outdated
Comment thread tests/recipes/wrangles/test_main.py Outdated
@mborodii-prog
mborodii-prog force-pushed the 816-logic-based-assignment-using-pandas-case_when branch from a413c99 to 06d1b1b Compare November 20, 2025 12:36
@ebhills

ebhills commented Nov 24, 2025 •

Copy link
Copy Markdown
Collaborator

2 things:

  • Column names with spaces is common. Need to ensure these are handled. In out python wrangle, we require the user to put underscores in place of the spaces, which is OK. Let's stick with that unless you can come up with a better idea.
  • We are going to add / modify the wrangle types. Let's nest this wrangle under a new one called compute. So you need to put it in it's own script compute.py and the name with thus be compute.case_when.

@mborodii-prog
mborodii-prog merged commit 34a5ed0 into main Dec 3, 2025
58 of 66 checks passed
@mborodii-prog
mborodii-prog deleted the 816-logic-based-assignment-using-pandas-case_when branch January 5, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logic-based Assignment (using pandas case_when)

4 participants