Skip to content

Commit

Permalink
add iso strptime patterns (#3265)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Apr 30, 2022
1 parent c5b1a7a commit aabc5dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
14 changes: 14 additions & 0 deletions polars/polars-time/src/chunkedarray/utf8/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ pub(super) static DATE_Y_M_D: &[&str] = &[
/// NOTE: don't use single letter dates like %F
/// polars parsers does not support them, so it will be slower
pub(super) static DATETIME_D_M_Y: &[&str] = &[
// --
// supported by polars' parser
// ---
// 31/12/21 12:54:98
"%d/%m/%y %H:%M:%S",
// 31-12-2021 24:58:01
"%d-%m-%Y %H:%M:%S",
// 31-04-2021T02:45:55.555000000
// milliseconds
"%d-%m-%YT%H:%M:%S.%3f",
"%d-%m-%yT%H:%M:%S.%3f",
// microseconds
"%d-%m-%YT%H:%M:%S.%6f",
// 31-04-21T02:45:55.555000000
Expand Down Expand Up @@ -67,9 +73,17 @@ pub(super) static DATETIME_Y_M_D: &[&str] = &[
"%Y-%m-%dT%H:%M:%S.%6f",
// nanoseconds
"%Y-%m-%dT%H:%M:%S.%9f",
"%Y-%m-%dT%H:%M:%S.%3f",
// no times
"%Y-%m-%d",
"%Y/%m/%d",
// --
// not supported by polars' parser
// ---
"%+",
// we cannot know this one, because polars needs to know
// the length of the parsed fmt
"%FT%H:%M:%S%.f",
];

#[derive(Eq, Hash, PartialEq, Clone, Copy, Debug)]
Expand Down
26 changes: 26 additions & 0 deletions py-polars/tests/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,29 @@ def test_from_time_arrow() -> None:
time(0, 0, 20),
time(0, 0, 30),
]


def test_datetime_strptime_patterns() -> None:
# note that all should be year first
df = pl.Series(
"date",
[
"09-05-2019" "2018-09-05",
"2018-09-05T04:05:01",
"2018-09-05T04:24:01.9",
"2018-09-05T04:24:02.11",
"2018-09-05T14:24:02.123",
"2018-09-05T14:24:02.123Z",
"2019-04-18T02:45:55.555000000",
"2019-04-18T22:45:55.555123",
],
).to_frame()
s = df.with_columns(
[
pl.col("date")
.str.strptime(pl.Datetime, fmt=None, strict=False)
.alias("parsed"),
]
)["parsed"]
assert s.null_count() == 1
assert s[0] is None

0 comments on commit aabc5dc

Please sign in to comment.