From c0b99cc4546665e8487bbc6a28ffc08b538a57eb Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 23 Jan 2023 16:04:07 -0800 Subject: [PATCH 1/6] DEPR: parsing to tzlocal --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/_libs/tslibs/parsing.pyx | 8 ++++++++ pandas/tests/tslibs/test_parsing.py | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index dc05745c8c0e5..657f1d1fb84e6 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -611,6 +611,7 @@ Other API changes Deprecations ~~~~~~~~~~~~ +- Deprecated parsing datetime strings with system-local timezone to ``tzlocal``, pass a ``tz`` keyword or explicitly call ``tz_localize`` instead (:issue:`50791`) - Deprecated argument ``infer_datetime_format`` in :func:`to_datetime` and :func:`read_csv`, as a strict version of it is now the default (:issue:`48621`) - Deprecated :func:`pandas.io.sql.execute` (:issue:`50185`) - :meth:`Index.is_boolean` has been deprecated. Use :func:`pandas.api.types.is_bool_dtype` instead (:issue:`50042`) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index aa53f8d813874..fe64402a9d9fe 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -653,6 +653,14 @@ cdef dateutil_parse( ret = ret + relativedelta.relativedelta(weekday=res.weekday) if not ignoretz: if res.tzname and res.tzname in time.tzname: + # GH#50791 + warnings.warn( + "Parsing '{res.tzname}' as tzlocal (dependent on system timezone) " + "is deprecated and will raise in a future version. Pass the 'tz' " + "keyword or call tz_localize after construction instead", + FutureWarning, + stacklevel=find_stack_level() + ) ret = ret.replace(tzinfo=_dateutil_tzlocal()) elif res.tzoffset == 0: ret = ret.replace(tzinfo=_dateutil_tzutc()) diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index 33fce7b351513..0c9ae7f689a6e 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -5,6 +5,7 @@ import re from dateutil.parser import parse as du_parse +from dateutil.tz import tzlocal import numpy as np import pytest @@ -18,6 +19,22 @@ import pandas._testing as tm +def test_parsing_tzlocal_deprecated(): + # GH#50791 + msg = "Pass the 'tz' keyword or call tz_localize after construction instead" + dtstr = "Jan 15 2004 03:00 EST" + + with tm.set_timezone("US/Eastern"): + with tm.assert_produces_warning(FutureWarning, match=msg): + res, _ = parse_datetime_string_with_reso(dtstr) + + assert isinstance(res.tzinfo, tzlocal) + + with tm.assert_produces_warning(FutureWarning, match=msg): + res = parsing.parse_datetime_string(dtstr) + assert isinstance(res.tzinfo, tzlocal) + + def test_parse_datetime_string_with_reso(): (parsed, reso) = parse_datetime_string_with_reso("4Q1984") (parsed_lower, reso_lower) = parse_datetime_string_with_reso("4q1984") From dfef22cb0c646ee02ecb525da76e2801ec83592c Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 24 Jan 2023 12:41:49 -0800 Subject: [PATCH 2/6] skip on windows --- pandas/tests/tslibs/test_parsing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index 0c9ae7f689a6e..b0f7f755518a4 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -19,6 +19,7 @@ import pandas._testing as tm +@td.skip_if_windows def test_parsing_tzlocal_deprecated(): # GH#50791 msg = "Pass the 'tz' keyword or call tz_localize after construction instead" From 0bd83b46ff1fead0de016f14063a7039db031b0c Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 24 Jan 2023 17:01:40 -0800 Subject: [PATCH 3/6] ignore warning specific to systems in utc --- pandas/tests/tslibs/test_array_to_datetime.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/tslibs/test_array_to_datetime.py b/pandas/tests/tslibs/test_array_to_datetime.py index 63adb8427969d..b652385c2ec6b 100644 --- a/pandas/tests/tslibs/test_array_to_datetime.py +++ b/pandas/tests/tslibs/test_array_to_datetime.py @@ -67,6 +67,7 @@ def test_parsing_timezone_offsets(dt_string, expected_tz): assert result_tz == timezone(timedelta(minutes=expected_tz)) +@pytest.mark.filterwarnings("ignore:.*dependent on system timezone.*:FutureWarning") def test_parsing_non_iso_timezone_offset(): dt_string = "01-01-2013T00:00:00.000000000+0000" arr = np.array([dt_string], dtype=object) From a5aae34ade8fe8f6223d5a0cbd83f56bb5b1c7bd Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 26 Jan 2023 14:24:55 -0800 Subject: [PATCH 4/6] troubleshoot warning --- pandas/tests/tslibs/test_array_to_datetime.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/tslibs/test_array_to_datetime.py b/pandas/tests/tslibs/test_array_to_datetime.py index b652385c2ec6b..ba188c3182f57 100644 --- a/pandas/tests/tslibs/test_array_to_datetime.py +++ b/pandas/tests/tslibs/test_array_to_datetime.py @@ -67,12 +67,13 @@ def test_parsing_timezone_offsets(dt_string, expected_tz): assert result_tz == timezone(timedelta(minutes=expected_tz)) -@pytest.mark.filterwarnings("ignore:.*dependent on system timezone.*:FutureWarning") def test_parsing_non_iso_timezone_offset(): dt_string = "01-01-2013T00:00:00.000000000+0000" arr = np.array([dt_string], dtype=object) - result, result_tz = tslib.array_to_datetime(arr) + with tm.assert_produces_warning(None): + # GH#50949 should not get tzlocal-deprecation warning here + result, result_tz = tslib.array_to_datetime(arr) expected = np.array([np.datetime64("2013-01-01 00:00:00.000000000")]) tm.assert_numpy_array_equal(result, expected) From a8e25deaa8b9c66f8081e4a8b9d09c4827214bde Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 26 Jan 2023 15:46:04 -0800 Subject: [PATCH 5/6] troubleshoot --- pandas/_libs/tslibs/parsing.pyx | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index fe64402a9d9fe..ecc5a687fe5bf 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -654,6 +654,7 @@ cdef dateutil_parse( if not ignoretz: if res.tzname and res.tzname in time.tzname: # GH#50791 + assert False, (res.tzname, time.tzname) warnings.warn( "Parsing '{res.tzname}' as tzlocal (dependent on system timezone) " "is deprecated and will raise in a future version. Pass the 'tz' " From 3bdb6e5aa625da96a83c7f0ed413064bdd6bedc3 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 27 Jan 2023 07:57:09 -0800 Subject: [PATCH 6/6] update warning --- pandas/_libs/tslibs/parsing.pyx | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index ecc5a687fe5bf..485813a634f20 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -654,14 +654,17 @@ cdef dateutil_parse( if not ignoretz: if res.tzname and res.tzname in time.tzname: # GH#50791 - assert False, (res.tzname, time.tzname) - warnings.warn( - "Parsing '{res.tzname}' as tzlocal (dependent on system timezone) " - "is deprecated and will raise in a future version. Pass the 'tz' " - "keyword or call tz_localize after construction instead", - FutureWarning, - stacklevel=find_stack_level() - ) + if res.tzname != "UTC": + # If the system is localized in UTC (as many CI runs are) + # we get tzlocal, once the deprecation is enforced will get + # timezone.utc, not raise. + warnings.warn( + "Parsing '{res.tzname}' as tzlocal (dependent on system timezone) " + "is deprecated and will raise in a future version. Pass the 'tz' " + "keyword or call tz_localize after construction instead", + FutureWarning, + stacklevel=find_stack_level() + ) ret = ret.replace(tzinfo=_dateutil_tzlocal()) elif res.tzoffset == 0: ret = ret.replace(tzinfo=_dateutil_tzutc())