Skip to content

enh: expressify offset_by #2799

Open

Description

@MarcoGorelli

follow-up from #2573

I think it should be possible to support

In [1]: import narwhals as nw

In [2]: import polars as pl

In [3]: from datetime import datetime, date, timedelta

In [4]: df = pl.DataFrame({'a': [datetime(2020,1,1), datetime(2020,4,6)], 'b': ['1d', '1m']})

In [5]: df.with_columns(c=pl.col('a').dt.offset_by(pl.col('b')))
Out[5]:
shape: (2, 3)
┌─────────────────────┬─────┬─────────────────────┐
│ a                   ┆ b   ┆ c                   │
│ ---                 ┆ --- ┆ ---                 │
│ datetime[μs]        ┆ str ┆ datetime[μs]        │
╞═════════════════════╪═════╪═════════════════════╡
│ 2020-01-01 00:00:00 ┆ 1d  ┆ 2020-01-02 00:00:00 │
│ 2020-04-06 00:00:00 ┆ 1m  ┆ 2020-04-06 00:01:00 │
└─────────────────────┴─────┴─────────────────────┘

at least for:

  • polars
  • duckdb
  • pyspark

which I think is good-enough to allow it 😄

Activity

  1. dangotbanned commented on Jul 7, 2025

    @dangotbanned
    Member

    I think pyarrow would also work, provided we can convert the b column to:

    pa.ChunkedArray[pa.DurationScalar[Any]]

    https://github.com/zen-xu/pyarrow-stubs/blob/d4fbd25850207531ff26902d8d4bc134ee5ccd79/pyarrow-stubs/__lib_pxi/table.pyi#L1496-L1500

    We seem to just be using pc.add, which supports those operands

    https://github.com/zen-xu/pyarrow-stubs/blob/d4fbd25850207531ff26902d8d4bc134ee5ccd79/pyarrow-stubs/compute.pyi#L1077-L1084

  2. 29antonioac commented on Aug 15, 2025

    @29antonioac

    In Pandas it could work, but only if the offset is a timedelta 😅.

    Would be great to see it in Narwhals!

  3. MarcoGorelli commented on Aug 15, 2025

    @MarcoGorelli
    MemberAuthor

    yup, PRs welcome!

  4. 555aaditya commented on Oct 28, 2026

    @555aaditya
    Contributor

    I'd like to pick this up. Two quick questions before I start:

    1. For DuckDB and Spark-like, the offset strings have to be parsed per row in SQL (regex for <multiple><unit>, then a CASE on the unit). An invalid string, or ns on DuckDB, would give null there, while Polars raises. Is null acceptable for those backends, or would you rather they raise up front?
    2. Should pandas-like, PyArrow, Dask and Ibis raise NotImplementedError for an expression by (keeping the existing str path unchanged), as the issue suggests?
  5. MarcoGorelli commented on Oct 28, 2026

    @MarcoGorelli
    MemberAuthor

    thanks for looking into this!

    An invalid string, or ns on DuckDB, would give null there, while Polars raises. Is null acceptable for those backends, or would you rather they raise up front?

    if that's the only thing they can do, then sure, I guess we'll just have to document the difference

    Should pandas-like, PyArrow, Dask and Ibis raise NotImplementedError for an expression by (keeping the existing str path unchanged), as the issue suggests?

    Yup! Until (if they ever do) offer an expressified way to do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions