Skip to content
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

Upcast date + duration and date - duration operations to datetime #20397

Open
henryharbeck opened this issue Dec 21, 2024 · 2 comments
Open

Upcast date + duration and date - duration operations to datetime #20397

henryharbeck opened this issue Dec 21, 2024 · 2 comments
Labels
A-temporal Area: date/time functionality enhancement New feature or an improvement of an existing feature

Comments

@henryharbeck
Copy link
Contributor

Description

It seems to be generally accepted in databases that date + duration or date - duration should return a datetime (or timestamp).
I have tested BigQuery, DuckDB and PostgreSQL and found them all to follow this.

This feature request is to implement the same in Polars.

I'm unsure whether this is best categorised as a bug fix due to silent loss of precision when (for example) adding 1 hour to a date (as below), or a (potentially breaking) feature.

from datetime import date
import duckdb
import polars as pl
print(
    pl.DataFrame({"d": date(2024, 1, 1)})
    .with_columns(
        dt=pl.col("d") + pl.duration(hours=1),
        dt2=pl.col("d") + pl.duration(days=1),
    )
)
# shape: (1, 3)
# ┌────────────┬────────────┬────────────┐
# │ d          ┆ dt         ┆ dt2        │
# │ ---        ┆ ---        ┆ ---        │
# │ date       ┆ date       ┆ date       │
# ╞════════════╪════════════╪════════════╡
# │ 2024-01-01 ┆ 2024-01-01 ┆ 2024-01-02 │
# └────────────┴────────────┴────────────┘

print(duckdb.sql(
    """
    FROM (VALUES (DATE '2024-01-01')) df(d)
    SELECT
        *,
        d + INTERVAL '1 HOUR' AS dt,
        d + INTERVAL '1 DAY' AS dt2,
    """
))
# ┌────────────┬─────────────────────┬─────────────────────┐
# │     d      │         dt          │         dt2         │
# │    date    │      timestamp      │      timestamp      │
# ├────────────┼─────────────────────┼─────────────────────┤
# │ 2024-01-01 │ 2024-01-01 01:00:00 │ 2024-01-02 00:00:00 │
# └────────────┴─────────────────────┴─────────────────────┘
@henryharbeck henryharbeck added the enhancement New feature or an improvement of an existing feature label Dec 21, 2024
@MarcoGorelli MarcoGorelli added the A-temporal Area: date/time functionality label Dec 21, 2024
@MarcoGorelli
Copy link
Collaborator

MarcoGorelli commented Dec 22, 2024

thanks for opening the issue!

This is similar to what happens for datetime / duration of different time_units:

  • datetime[us] + duration[ms] => datetime[ms]

and the loss of information can already happen there:

In [4]: df
Out[4]:
shape: (1, 1)
┌────────────────────────────┐
│ a                          │
│ ---                        │
│ datetime[μs]               │
╞════════════════════════════╡
│ 2020-01-01 00:00:00.123456 │
└────────────────────────────┘

In [5]: df.with_columns(b=pl.col('a') + pl.duration(hours=1, time_unit='ms'))
Out[5]:
shape: (1, 2)
┌────────────────────────────┬─────────────────────────┐
│ ab                       │
│ ------                     │
│ datetime[μs]               ┆ datetime[ms]            │
╞════════════════════════════╪═════════════════════════╡
│ 2020-01-01 00:00:00.1234562020-01-01 01:00:00.123 │
└────────────────────────────┴─────────────────────────┘

This was discussed previously, in #12023

So I guess the remaining question is - should date behave differently, and just raise instead of losing information?

@mcrumiller
Copy link
Contributor

Recall we updated the mean/median of a date array to return a datetime. The analogue here was treating date/datetime like int/float, which is fairly natural. For this reason, I would think that just like 1 + 1.5 = 2.5, date(2025, 1, 1) + timedelta(hours=12) should return datetime(2025, 1, 1, 12), although that's not what happens for python's date/datetime package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-temporal Area: date/time functionality enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

3 participants