From b69af4d2a5054819d4e8b09653356956b49edfb2 Mon Sep 17 00:00:00 2001 From: Harini Krishnamurthy Date: Mon, 29 Sep 2025 15:55:59 -0700 Subject: [PATCH 1/4] Add tests for date parsing when display.date_dayfirst and display.date_yearfirst are set --- pandas/tests/tslibs/test_parsing.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index 72515c030757b..f2b8b4b18f2e6 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -10,6 +10,8 @@ import numpy as np import pytest +from pandas._config import set_option + from pandas._libs.tslibs import ( parsing, strptime, @@ -421,3 +423,39 @@ def test_hypothesis_delimited_date( assert except_out_dateutil == except_in_dateutil assert result == expected + + +@pytest.mark.parametrize("input", ["21-01-01", "01-01-21"]) +@pytest.mark.parametrize("dayfirst", [True, False]) +def test_parse_datetime_string_with_reso_dayfirst(dayfirst, input): + set_option("display.date_dayfirst", dayfirst) + except_out_dateutil, result = _helper_hypothesis_delimited_date( + parsing.parse_datetime_string_with_reso, input + ) + except_in_dateutil, expected = _helper_hypothesis_delimited_date( + du_parse, + input, + default=datetime(1, 1, 1), + dayfirst=dayfirst, + yearfirst=False, + ) + assert except_out_dateutil == except_in_dateutil + assert result[0] == expected + + +@pytest.mark.parametrize("input", ["21-01-01", "01-01-21"]) +@pytest.mark.parametrize("yearfirst", [True, False]) +def test_parse_datetime_string_with_reso_yearfirst(yearfirst, input): + set_option("display.date_yearfirst", yearfirst) + except_out_dateutil, result = _helper_hypothesis_delimited_date( + parsing.parse_datetime_string_with_reso, input + ) + except_in_dateutil, expected = _helper_hypothesis_delimited_date( + du_parse, + input, + default=datetime(1, 1, 1), + dayfirst=False, + yearfirst=yearfirst, + ) + assert except_out_dateutil == except_in_dateutil + assert result[0] == expected From 7f8ca4d55334dad6a1fdb1f4ed647a6f7e3720f9 Mon Sep 17 00:00:00 2001 From: Harini Krishnamurthy Date: Fri, 3 Oct 2025 13:43:55 -0700 Subject: [PATCH 2/4] use option_context to set options in test --- pandas/tests/tslibs/test_parsing.py | 55 +++++++++++++++-------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index f2b8b4b18f2e6..989d1ac986adc 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -10,7 +10,7 @@ import numpy as np import pytest -from pandas._config import set_option +from pandas import option_context from pandas._libs.tslibs import ( parsing, @@ -428,34 +428,35 @@ def test_hypothesis_delimited_date( @pytest.mark.parametrize("input", ["21-01-01", "01-01-21"]) @pytest.mark.parametrize("dayfirst", [True, False]) def test_parse_datetime_string_with_reso_dayfirst(dayfirst, input): - set_option("display.date_dayfirst", dayfirst) - except_out_dateutil, result = _helper_hypothesis_delimited_date( - parsing.parse_datetime_string_with_reso, input - ) - except_in_dateutil, expected = _helper_hypothesis_delimited_date( - du_parse, - input, - default=datetime(1, 1, 1), - dayfirst=dayfirst, - yearfirst=False, - ) - assert except_out_dateutil == except_in_dateutil - assert result[0] == expected + with option_context("display.date_dayfirst", dayfirst): + except_out_dateutil, result = _helper_hypothesis_delimited_date( + parsing.parse_datetime_string_with_reso, input + ) + + except_in_dateutil, expected = _helper_hypothesis_delimited_date( + du_parse, + input, + default=datetime(1, 1, 1), + dayfirst=dayfirst, + yearfirst=False, + ) + assert except_out_dateutil == except_in_dateutil + assert result[0] == expected @pytest.mark.parametrize("input", ["21-01-01", "01-01-21"]) @pytest.mark.parametrize("yearfirst", [True, False]) def test_parse_datetime_string_with_reso_yearfirst(yearfirst, input): - set_option("display.date_yearfirst", yearfirst) - except_out_dateutil, result = _helper_hypothesis_delimited_date( - parsing.parse_datetime_string_with_reso, input - ) - except_in_dateutil, expected = _helper_hypothesis_delimited_date( - du_parse, - input, - default=datetime(1, 1, 1), - dayfirst=False, - yearfirst=yearfirst, - ) - assert except_out_dateutil == except_in_dateutil - assert result[0] == expected + with option_context("display.date_yearfirst", yearfirst): + except_out_dateutil, result = _helper_hypothesis_delimited_date( + parsing.parse_datetime_string_with_reso, input + ) + except_in_dateutil, expected = _helper_hypothesis_delimited_date( + du_parse, + input, + default=datetime(1, 1, 1), + dayfirst=False, + yearfirst=yearfirst, + ) + assert except_out_dateutil == except_in_dateutil + assert result[0] == expected From 419af7b395205a4f193224984074b7d00cfd9fa1 Mon Sep 17 00:00:00 2001 From: Harini Krishnamurthy Date: Fri, 3 Oct 2025 13:55:13 -0700 Subject: [PATCH 3/4] lint --- pandas/tests/tslibs/test_parsing.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index 989d1ac986adc..fcbfb716ad413 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -10,8 +10,6 @@ import numpy as np import pytest -from pandas import option_context - from pandas._libs.tslibs import ( parsing, strptime, @@ -27,7 +25,10 @@ # Usually we wouldn't want this import in this test file (which is targeted at # tslibs.parsing), but it is convenient to test the Timestamp constructor at # the same time as the other parsing functions. -from pandas import Timestamp +from pandas import ( + Timestamp, + option_context, +) import pandas._testing as tm from pandas._testing._hypothesis import DATETIME_NO_TZ @@ -432,7 +433,7 @@ def test_parse_datetime_string_with_reso_dayfirst(dayfirst, input): except_out_dateutil, result = _helper_hypothesis_delimited_date( parsing.parse_datetime_string_with_reso, input ) - + except_in_dateutil, expected = _helper_hypothesis_delimited_date( du_parse, input, From 41f35c5f232b995a347203579d1d629ac88b76c7 Mon Sep 17 00:00:00 2001 From: Harini Krishnamurthy Date: Fri, 3 Oct 2025 14:05:00 -0700 Subject: [PATCH 4/4] updated user