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

fix(rust, python): fix time to duration cast #5932

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)