Skip to content

Commit

Permalink
fix(rust, python): fix time to duration cast (#5932)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 29, 2022
1 parent 7163747 commit 6db2311
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 11 additions & 1 deletion polars/polars-core/src/chunked_array/logical/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ impl LogicalType for TimeChunked {
}

fn cast(&self, dtype: &DataType) -> PolarsResult<Series> {
self.0.cast(dtype)
match dtype {
DataType::Duration(tu) => {
let out = self.0.cast(&DataType::Duration(TimeUnit::Nanoseconds));
if !matches!(tu, TimeUnit::Nanoseconds) {
out?.cast(dtype)
} else {
out
}
}
_ => self.0.cast(dtype),
}
}
}
6 changes: 6 additions & 0 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2300,3 +2300,9 @@ def test_round_by_week() -> None:
"7d": [date(1998, 4, 9), date(2022, 12, 1)],
"1w": [date(1998, 4, 13), date(2022, 11, 28)],
}


def test_cast_time_to_duration() -> None:
assert pl.Series([time(hour=0, minute=0, second=2)]).cast(
pl.Duration
).item() == timedelta(seconds=2)

0 comments on commit 6db2311

Please sign in to comment.