From 3897a370060a2df7619bb0e4c2bd0e2b69e1cb3d Mon Sep 17 00:00:00 2001 From: Marco Edward Gorelli Date: Tue, 16 Jul 2024 14:55:55 +0100 Subject: [PATCH] fix: Interpret %y consistently with Chrono in to_date/to_datetime/strptime (#17661) --- .../src/chunkedarray/string/strptime.rs | 2 +- .../namespaces/temporal/test_to_datetime.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/polars-time/src/chunkedarray/string/strptime.rs b/crates/polars-time/src/chunkedarray/string/strptime.rs index c161f3bb27de..85242c4956ec 100644 --- a/crates/polars-time/src/chunkedarray/string/strptime.rs +++ b/crates/polars-time/src/chunkedarray/string/strptime.rs @@ -160,7 +160,7 @@ impl StrpTimeState { return None; } - if decade < 50 { + if decade < 70 { year = 2000 + decade; } else { year = 1900 + decade; diff --git a/py-polars/tests/unit/operations/namespaces/temporal/test_to_datetime.py b/py-polars/tests/unit/operations/namespaces/temporal/test_to_datetime.py index 12f4c94b1cc6..08ab045467ca 100644 --- a/py-polars/tests/unit/operations/namespaces/temporal/test_to_datetime.py +++ b/py-polars/tests/unit/operations/namespaces/temporal/test_to_datetime.py @@ -1,7 +1,7 @@ from __future__ import annotations import sys -from datetime import datetime +from datetime import date, datetime from typing import TYPE_CHECKING import hypothesis.strategies as st @@ -187,3 +187,19 @@ def test_to_datetime_aware_values_aware_dtype() -> None: dtype=pl.Datetime("us", "Asia/Kathmandu"), ) assert_series_equal(result, expected) + + +@pytest.mark.parametrize( + ("inputs", "format", "expected"), + [ + ("01-01-69", "%d-%m-%y", date(2069, 1, 1)), # Polars' parser + ("01-01-70", "%d-%m-%y", date(1970, 1, 1)), # Polars' parser + ("01-January-69", "%d-%B-%y", date(2069, 1, 1)), # Chrono + ("01-January-70", "%d-%B-%y", date(1970, 1, 1)), # Chrono + ], +) +def test_to_datetime_two_digit_year_17213( + inputs: str, format: str, expected: date +) -> None: + result = pl.Series([inputs]).str.to_date(format=format).item() + assert result == expected