Skip to content

Commit

Permalink
fix[rust]: parse date when day is omitted (#4495)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 19, 2022
1 parent edd0a1d commit 3496e33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion polars/polars-time/src/chunkedarray/utf8/strptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub(super) unsafe fn parse(val: &[u8], fmt: &[u8], fmt_len: u16) -> Option<Naive
}
let mut year: i32 = 0;
let mut month: u32 = 0;
let mut day: u32 = 0;
// minimal day is always 1
// otherwise chrono may panic.
let mut day: u32 = 1;
let mut hour: u32 = 0;
let mut min: u32 = 0;
let mut sec: u32 = 0;
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1293,3 +1293,13 @@ def test_from_dict_tu_consistency() -> None:
from_dicts = pl.from_dicts([{"dt": dt}])

assert from_dict.dtypes == from_dicts.dtypes


def test_date_parse_omit_day() -> None:
df = pl.DataFrame({"month": ["2022-01"]})
assert df.select(pl.col("month").str.strptime(pl.Date, fmt="%Y-%m"))[0, 0] == date(
2022, 1, 1
)
assert df.select(pl.col("month").str.strptime(pl.Datetime, fmt="%Y-%m"))[
0, 0
] == datetime(2022, 1, 1)

0 comments on commit 3496e33

Please sign in to comment.