Skip to content

Commit

Permalink
fix(rust): Fix datetime cast behavior for pre-epoch times (#19949)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptiza authored Nov 29, 2024
1 parent 44ddbc2 commit 09f2a31
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions crates/polars-core/src/chunked_array/temporal/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ impl DatetimeChunked {
use TimeUnit::*;
match (current_unit, tu) {
(Nanoseconds, Microseconds) => {
let ca = (&self.0).wrapping_trunc_div_scalar(1_000);
let ca = (&self.0).wrapping_floor_div_scalar(1_000);
out.0 = ca;
out
},
(Nanoseconds, Milliseconds) => {
let ca = (&self.0).wrapping_trunc_div_scalar(1_000_000);
let ca = (&self.0).wrapping_floor_div_scalar(1_000_000);
out.0 = ca;
out
},
Expand All @@ -138,7 +138,7 @@ impl DatetimeChunked {
out
},
(Microseconds, Milliseconds) => {
let ca = (&self.0).wrapping_trunc_div_scalar(1_000);
let ca = (&self.0).wrapping_floor_div_scalar(1_000);
out.0 = ca;
out
},
Expand Down
20 changes: 20 additions & 0 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2402,3 +2402,23 @@ def test_weekday_vs_stdlib_date(value: date) -> None:
result = pl.Series([value]).dt.weekday().item()
expected = value.isoweekday()
assert result == expected


def test_temporal_downcast_construction_19309() -> None:
# implicit cast from us to ms upon construction
s = pl.Series(
[
datetime(1969, 1, 1, 0, 0, 0, 1),
datetime(1969, 12, 31, 23, 59, 59, 999999),
datetime(1970, 1, 1, 0, 0, 0, 0),
datetime(1970, 1, 1, 0, 0, 0, 1),
],
dtype=pl.Datetime("ms"),
)

assert s.to_list() == [
datetime(1969, 1, 1),
datetime(1969, 12, 31, 23, 59, 59, 999000),
datetime(1970, 1, 1),
datetime(1970, 1, 1),
]

0 comments on commit 09f2a31

Please sign in to comment.