From ab13f7fca35eed2f25d67e1a1519c415f453161f Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 25 Mar 2024 13:45:15 -0700 Subject: [PATCH] DEPR: Enforce deprecation of parsing to tzlocal --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/_libs/tslibs/parsing.pyx | 12 +++--------- pandas/tests/tslibs/test_parsing.py | 20 ++++++++++++-------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 4d2381ae1e5e4..bb856936cd96d 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -202,6 +202,7 @@ Removal of prior version deprecations/changes - Enforced deprecation of :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` allowing the ``name`` argument to be a non-tuple when grouping by a list of length 1 (:issue:`54155`) - Enforced deprecation of :meth:`Series.interpolate` and :meth:`DataFrame.interpolate` for object-dtype (:issue:`57820`) - Enforced deprecation of ``axis=None`` acting the same as ``axis=0`` in the DataFrame reductions ``sum``, ``prod``, ``std``, ``var``, and ``sem``, passing ``axis=None`` will now reduce over both axes; this is particularly the case when doing e.g. ``numpy.sum(df)`` (:issue:`21597`) +- Enforced deprecation of parsing system timezone strings to ``tzlocal``, which depended on system timezone, pass the 'tz' keyword instead (:issue:`50791`) - Enforced deprecation of passing a dictionary to :meth:`SeriesGroupBy.agg` (:issue:`52268`) - Enforced deprecation of string ``AS`` denoting frequency in :class:`YearBegin` and strings ``AS-DEC``, ``AS-JAN``, etc. denoting annual frequencies with various fiscal year starts (:issue:`57793`) - Enforced deprecation of string ``A`` denoting frequency in :class:`YearEnd` and strings ``A-DEC``, ``A-JAN``, etc. denoting annual frequencies with various fiscal year ends (:issue:`57699`) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index 94c549cbd3db0..384df1cac95eb 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -45,7 +45,6 @@ from decimal import InvalidOperation from dateutil.parser import DEFAULTPARSER from dateutil.tz import ( - tzlocal as _dateutil_tzlocal, tzoffset, tzutc as _dateutil_tzutc, ) @@ -703,17 +702,12 @@ cdef datetime dateutil_parse( if res.tzname and res.tzname in time.tzname: # GH#50791 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( + raise ValueError( f"Parsing '{res.tzname}' as tzlocal (dependent on system timezone) " - "is deprecated and will raise in a future version. Pass the 'tz' " + "is no longer supported. Pass the 'tz' " "keyword or call tz_localize after construction instead", - FutureWarning, - stacklevel=find_stack_level() ) - ret = ret.replace(tzinfo=_dateutil_tzlocal()) + ret = ret.replace(tzinfo=timezone.utc) elif res.tzoffset == 0: ret = ret.replace(tzinfo=_dateutil_tzutc()) elif res.tzoffset: diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index d1b0595dd50e6..52af5adb686a7 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -6,7 +6,6 @@ import re from dateutil.parser import parse as du_parse -from dateutil.tz import tzlocal from hypothesis import given import numpy as np import pytest @@ -22,6 +21,10 @@ ) import pandas.util._test_decorators as td +# 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 import pandas._testing as tm from pandas._testing._hypothesis import DATETIME_NO_TZ @@ -33,20 +36,21 @@ def test_parsing_tzlocal_deprecated(): # GH#50791 msg = ( - "Parsing 'EST' as tzlocal.*" + r"Parsing 'EST' as tzlocal \(dependent on system timezone\) " + r"is no longer supported\. " "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) + with pytest.raises(ValueError, match=msg): + parse_datetime_string_with_reso(dtstr) - assert isinstance(res.tzinfo, tzlocal) + with pytest.raises(ValueError, match=msg): + parsing.py_parse_datetime_string(dtstr) - with tm.assert_produces_warning(FutureWarning, match=msg): - res = parsing.py_parse_datetime_string(dtstr) - assert isinstance(res.tzinfo, tzlocal) + with pytest.raises(ValueError, match=msg): + Timestamp(dtstr) def test_parse_datetime_string_with_reso():