Skip to content

add coalesce for all backends #2647

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

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

Adds a narwhals.coalesce function for use with Narwhals Expressions.

@camriddell
Copy link
Member Author

I'll probably need to write more tests, at the very least this should serve as a functional MVP.

@camriddell camriddell marked this pull request as ready for review June 6, 2025 15:08
@camriddell
Copy link
Member Author

Hold off on merging for now, I need some tests for coalescing across disparate datatypes.

@camriddell
Copy link
Member Author

camriddell commented Jun 6, 2025

@MarcoGorelli thoughts on the following considerations for coalesce-ing across different datatypes:

  1. Enforce a type promotion consistent with Polars
    • This will be tricky for pandas since it naturally promotes to object dtype, not to mention standard pandas str dtype is non-nullable and casting a numeric type to str type results in 'nan'.
    • I can't think of issues with other backends, but this would likely involve a fair bit more code.
  2. Restrict input types: you can only use coalesce if all of the datatypes fall within the same subtype (e.g. numeric, string, datetime, ...)
  3. Don't worry about it we accept that cross type promotion will break and will mark those backends to xfail this type of test.

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 !

i think what you've done is fine and we shouldn't do any casting on behalf of the user, let the backends handle that as they wish

Comment on lines 1823 to 1828
flat_exprs = flatten(
[
expr if isinstance(expr, (Expr, str)) else lit(expr)
for expr in chain(flatten([exprs]), more_exprs)
]
)
Copy link
Member

Choose a reason for hiding this comment

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

this looks a bit suspicious, can we reuse anything? check concat_str or any of the other functions in this file

Copy link
Member Author

Choose a reason for hiding this comment

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

The main idea was to coerce any inputs that are not strings/expressions into literals. Though this would also mean that any valid nw.Series objects would be inappropriately coerced as well.

Is there a helper somewhere that does this? If not, I've made the change to remove this capability- reverting is easy.

Comment on lines 210 to 221
def coalesce(self, *exprs: DuckDBExpr) -> DuckDBExpr:
def func(df: DuckDBLazyFrame) -> list[Expression]:
cols = (c for _expr in exprs for c in _expr(df))
return [CoalesceOperator(*cols)]

return self._expr(
call=func,
evaluate_output_names=combine_evaluate_output_names(*exprs),
alias_output_names=combine_alias_output_names(*exprs),
backend_version=self._backend_version,
version=self._version,
)
Copy link
Member

Choose a reason for hiding this comment

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

this should be elementwise, right? can we use the new _with_elementwise here for duckdb and pyspark?

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.

[Enh]: add coalesce top level function
2 participants