Skip to content

feat: Custom namespace registration #2721

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Excidion
Copy link

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 ability to register new namespaces like so (I was surprised narwhals didn't have ceil and floor yet):

import narwhals as nw
import pandas as pd

@nw.api.register_expr_namespace("rounding")
class Rounding:
    def __init__(self, expr: nw.Expr) -> None:
        self._expr = expr

    def ceil(self, dtype=nw.Int64) -> nw.Expr:
        return (self._expr + 1).cast(dtype=dtype)   # i know this doesn't actually work for all cases

    def floor(self, dtype=nw.Int64) -> nw.Expr:
        return self._expr.cast(dtype=dtype)


df = nw.from_native(pd.DataFrame([1.4, 24.3, 55.7, 64.001], columns=["n"]))
print(df.select(
    nw.col("n"),
    nw.col("n").rounding.floor().alias("floor"),
    nw.col("n").rounding.ceil().alias("ceil"),
).to_native().to_markdown())
"""
|    |      n |   floor |   ceil |
|---:|-------:|--------:|-------:|
|  0 |  1.4   |       1 |      2 |
|  1 | 24.3   |      24 |     25 |
|  2 | 55     |      55 |     56 |
|  3 | 64.001 |      64 |     65 |
"""

Heavily "inspired" by the polars implementation. Still needs tests and also updates to the docstrings. Just wanted to know if this is even going in the right direction before proceeding?

Checklist:

  • Tested for Expr
  • Tested for Series ?
  • Tested for DataFrame ?
  • Tested for LazyFrame ?

This is my first time contributing to FOSS, so please forgive if I missed anything. :)

@Excidion Excidion marked this pull request as draft June 22, 2025 12:59
@Excidion Excidion changed the title Custom namespace registration feat: Custom namespace registration Jun 22, 2025
@MarcoGorelli
Copy link
Member

thanks for the pr!

it's not clear to me why this is needed - could you clarify why this is more desirable than just

import narwhals as nw
import pandas as pd

def ceil(expr, dtype=nw.Int64) -> nw.Expr:
    return (expr + 1).cast(dtype=dtype)

def floor(expr, dtype=nw.Int64) -> nw.Expr:
    return expr.cast(dtype=dtype)


df = nw.from_native(pd.DataFrame([1.4, 24.3, 55.7, 64.001], columns=["n"]))
print(df.select(
    nw.col("n"),
    floor(nw.col("n")).alias("floor"),
    ceil(nw.col("n")).alias("ceil"),
).to_native().to_markdown())

please?

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.

2 participants