Skip to content

Commit

Permalink
fix: Allow sub on date/datetime (#17207)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Jun 26, 2024
1 parent 30e4d63 commit fcd4dbb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/polars-plan/src/plans/aexpr/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ fn get_arithmetic_field(
| (Date, Duration(_))
| (Duration(_), Time)
| (Time, Duration(_)) => try_get_supertype(left_field.data_type(), &right_type)?,
(Datetime(tu, _), Date) | (Date, Datetime(tu, _)) => Duration(*tu),
// T - T != T if T is a datetime / date
(Datetime(tul, _), Datetime(tur, _)) => Duration(get_time_units(tul, tur)),
(_, Datetime(_, _)) | (Datetime(_, _), _) => {
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/unit/operations/arithmetic/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,15 @@ def test_invalid_shapes_err() -> None:
match=r"cannot do arithmetic operation on series of different lengths: got 2 and 3",
):
pl.Series([1, 2]) + pl.Series([1, 2, 3])


def test_date_datetime_sub() -> None:
df = pl.DataFrame({"foo": [date(2020, 1, 1)], "bar": [datetime(2020, 1, 5)]})

assert df.select(
pl.col("foo") - pl.col("bar"),
pl.col("bar") - pl.col("foo"),
).to_dict(as_series=False) == {
"foo": [timedelta(days=-4)],
"bar": [timedelta(days=4)],
}

0 comments on commit fcd4dbb

Please sign in to comment.