Skip to content

Commit

Permalink
fix(rust): microsecond noise on date >> time cast (add 00:00:00
Browse files Browse the repository at this point in the history
… fast-path) (#5149)
  • Loading branch information
alexander-beedie committed Oct 10, 2022
1 parent 608898a commit eea3709
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion polars/polars-core/src/chunked_array/logical/date.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::*;
use crate::prelude::*;

pub type DateChunked = Logical<DateType, Int32Type>;

impl From<Int32Chunked> for DateChunked {
Expand Down Expand Up @@ -40,6 +39,9 @@ impl LogicalType for DateChunked {
.into_datetime(*tu, tz.clone())
.into_series())
}
(Date, Time) => Ok(Int64Chunked::full(self.name(), 0i64, self.len())
.into_time()
.into_series()),
_ => self.0.cast(dtype),
}
}
Expand Down
16 changes: 16 additions & 0 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,22 @@ def test_time_microseconds_3843() -> None:
assert s.to_list() == in_val


def test_date_to_time_cast_5111() -> None:
# check date -> time casts (fast-path: always 00:00:00)
df = pl.DataFrame(
{
"xyz": [
date(1969, 1, 1),
date(1990, 3, 8),
date(2000, 6, 16),
date(2010, 9, 24),
date(2022, 12, 31),
]
}
).with_column(pl.col("xyz").cast(pl.Time))
assert df["xyz"].to_list() == [time(0), time(0), time(0), time(0), time(0)]


def test_year_empty_df() -> None:
df = pl.DataFrame(pl.Series(name="date", dtype=pl.Date))
assert df.select(pl.col("date").dt.year()).dtypes == [pl.Int32]
Expand Down

0 comments on commit eea3709

Please sign in to comment.