Skip to content

Commit

Permalink
fix[rust]: strptime should not panic on invalid dates (#4914)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 21, 2022
1 parent da25686 commit da2b06d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion polars/polars-time/src/chunkedarray/utf8/strptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ pub(super) unsafe fn parse(val: &[u8], fmt: &[u8], fmt_len: u16) -> Option<Naive
}
// all values processed
if offset == val.len() {
Some(NaiveDate::from_ymd(year, month, day).and_hms_nano(hour, min, sec, nano))
NaiveDate::from_ymd_opt(year, month, day)
.and_then(|nd| nd.and_hms_nano_opt(hour, min, sec, nano))
}
// remaining values did not match pattern
else {
Expand Down
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 @@ -1500,3 +1500,9 @@ def test_short_formats() -> None:
def test_iso_year() -> None:
dt = datetime(2022, 1, 1, 7, 8, 40)
assert pl.Series([dt]).dt.iso_year()[0] == 2021


def test_invalid_date_parsing_4898() -> None:
assert pl.Series(["2022-09-18", "2022-09-50"]).str.strptime(
pl.Date, "%Y-%m-%d", strict=False
).to_list() == [date(2022, 9, 18), None]

0 comments on commit da2b06d

Please sign in to comment.