Skip to content

feat: when-then broadcasting #2663

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

Merged
merged 14 commits into from
Jun 12, 2025

Conversation

camriddell
Copy link
Member

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

This adds broadcasting behavior to the when/then pattern similar to Polars (see #2645) for our eager backends and dask.
The implementation for the Lazy backends will require #2652 unless we want to introduce some hacky workarounds (e.g. we may be able to do this by joining after an aggregation to "broadcast" results; however I haven't fully thought through how this would look in the codebase).

@camriddell camriddell force-pushed the feat-whenthen-broadcast branch from 023a936 to 9607f7b Compare June 10, 2025 23:02
@camriddell
Copy link
Member Author

Rebased on top of #2662 do not merge until that PR lands in main.
We should be able to merge once that one receives the green light (provided all tests pass here).

@camriddell camriddell marked this pull request as ready for review June 10, 2025 23:12
@MarcoGorelli
Copy link
Member

thanks @camriddell ! could you rebase again please?

@camriddell camriddell force-pushed the feat-whenthen-broadcast branch from 9607f7b to 2f9edfc Compare June 11, 2025 14:16
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

wow!!!!! well done!


if self._otherwise_value is None:
return [then_series.where(condition)]
otherwise_value = get_dask_expr()._expr.Where._defaults["other"]
Copy link
Member

Choose a reason for hiding this comment

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

this is the only part that jumps out to me as suspicious πŸ€”

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

thanks @camriddell ! really solid work here! really impressed you figured out all the details, and I'm excited that we can support nw.when(nw.col('a').count()>0).then(nw.col('a').sum()) now πŸ₯³

I just made two updates:

  • not using private methods from Dask. In general, doing that kind of thing gives a very high chance of future failures, i'd advising against doing that whenever possible
  • replace a couple of if x is True with just if x (unless there was some reason i missed for using if x is True?

@MarcoGorelli MarcoGorelli added the enhancement New feature or request label Jun 12, 2025
@camriddell camriddell merged commit 5464f4d into narwhals-dev:main Jun 12, 2025
34 checks passed
@camriddell
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants