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

feat!: Change str.to_datetime to default to microsecond precision for format specifiers "%f" and "%.f" #13597

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 1 addition & 5 deletions crates/polars-plan/src/dsl/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,7 @@ impl StringNameSpace {
let time_unit = match (&options.format, time_unit) {
(_, Some(time_unit)) => time_unit,
(Some(format), None) => {
if format.contains("%.9f")
|| format.contains("%9f")
|| format.contains("%f")
|| format.contains("%.f")
{
if format.contains("%.9f") || format.contains("%9f") {
TimeUnit::Nanoseconds
} else if format.contains("%.3f") || format.contains("%3f") {
TimeUnit::Milliseconds
Expand Down
13 changes: 13 additions & 0 deletions py-polars/tests/unit/operations/namespaces/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,16 @@ def test_to_datetime_out_of_range_13401(time_unit: TimeUnit) -> None:
s.str.to_datetime("%Y-%B-%d %H:%M:%S", strict=False, time_unit=time_unit).item()
is None
)


def test_out_of_ns_range_no_tu_specified_13592() -> None:
df = pl.DataFrame({"dates": ["2022-08-31 00:00:00.0", "0920-09-18 00:00:00.0"]})
result = df.select(pl.col("dates").str.to_datetime(format="%Y-%m-%d %H:%M:%S%.f"))[
"dates"
]
expected = pl.Series(
"dates",
[datetime(2022, 8, 31, 0, 0), datetime(920, 9, 18, 0, 0)],
dtype=pl.Datetime("us"),
)
assert_series_equal(result, expected)