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(python): strptime now respects pl.Datetime's time_unit #6231

Merged
merged 5 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion py-polars/polars/internals/expr/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def strptime(
elif datatype == Datetime:
tu = datatype.tu # type: ignore[union-attr]
dtcol = pli.wrap_expr(
self._pyexpr.str_parse_datetime(fmt, strict, exact, cache, tz_aware)
self._pyexpr.str_parse_datetime(fmt, strict, exact, cache, tz_aware, tu)
)
return dtcol if (tu is None) else dtcol.dt.cast_time_unit(tu)
elif datatype == Time:
Expand Down
33 changes: 19 additions & 14 deletions py-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,28 +562,33 @@ impl PyExpr {
exact: bool,
cache: bool,
tz_aware: bool,
tu: Option<Wrap<TimeUnit>>,
) -> PyExpr {
let tu = match fmt {
Some(ref fmt) => {
if fmt.contains("%.9f")
|| fmt.contains("%9f")
|| fmt.contains("%f")
|| fmt.contains("%.f")
{
TimeUnit::Nanoseconds
} else if fmt.contains("%.3f") || fmt.contains("%3f") {
TimeUnit::Milliseconds
} else {
TimeUnit::Microseconds
let result_tu = if let Some(value) = tu {
value.0
} else {
match fmt {
Some(ref fmt) => {
if fmt.contains("%.9f")
|| fmt.contains("%9f")
|| fmt.contains("%f")
|| fmt.contains("%.f")
{
TimeUnit::Nanoseconds
} else if fmt.contains("%.3f") || fmt.contains("%3f") {
TimeUnit::Milliseconds
} else {
TimeUnit::Microseconds
}
}
None => TimeUnit::Microseconds,
}
None => TimeUnit::Microseconds,
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
};
self.inner
.clone()
.str()
.strptime(StrpTimeOptions {
date_dtype: DataType::Datetime(tu, None),
date_dtype: DataType::Datetime(result_tu, None),
fmt,
strict,
exact,
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/unit/test_datelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,16 @@ def test_strptime_precision() -> None:
assert ds.dt.nanosecond().to_list() == expected_values


@pytest.mark.parametrize(
("unit", "expected"),
[("ms", "123000000"), ("us", "123456000"), ("ns", "123456789")],
)
def test_strptime_precision_with_time_unit(unit: TimeUnit, expected: str) -> None:
ser = pl.Series(["2020-01-01 00:00:00.123456789"])
result = ser.str.strptime(pl.Datetime(unit)).dt.strftime("%f")[0]
assert result == expected


def test_asof_join_tolerance_grouper() -> None:
from datetime import date

Expand Down