Skip to content

Commit

Permalink
feat: raise better error message for .dt.time on Date column (#13932)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Jan 23, 2024
1 parent f427592 commit 6a8a90d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 1 addition & 2 deletions crates/polars-plan/src/dsl/function_expr/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ pub(super) fn time(s: &Series) -> PolarsResult<Series> {
)?
.cast(&DataType::Time),
DataType::Datetime(_, _) => s.datetime().unwrap().cast(&DataType::Time),
DataType::Date => s.datetime().unwrap().cast(&DataType::Time),
DataType::Time => Ok(s.clone()),
dtype => polars_bail!(ComputeError: "expected Datetime, Date, or Time, got {}", dtype),
dtype => polars_bail!(ComputeError: "expected Datetime or Time, got {}", dtype),
}
}
pub(super) fn date(s: &Series) -> PolarsResult<Series> {
Expand Down
4 changes: 3 additions & 1 deletion py-polars/tests/unit/namespaces/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ def test_dt_datetime_date_time_invalid() -> None:
pl.Series([time(23)]).dt.date()
with pytest.raises(ComputeError, match="expected Datetime or Date"):
pl.Series([timedelta(1)]).dt.date()
with pytest.raises(ComputeError, match="expected Datetime, Date, or Time"):
with pytest.raises(ComputeError, match="expected Datetime or Time"):
pl.Series([timedelta(1)]).dt.time()
with pytest.raises(ComputeError, match="expected Datetime or Time"):
pl.Series([date(2020, 1, 1)]).dt.time()


@pytest.mark.parametrize(
Expand Down

0 comments on commit 6a8a90d

Please sign in to comment.