Skip to content

Commit 38e802c

Browse files
authored
DEPR: parsing to tzlocal (#50949)
* DEPR: parsing to tzlocal * skip on windows * ignore warning specific to systems in utc * troubleshoot warning * troubleshoot * update warning
1 parent e754d18 commit 38e802c

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ Other API changes
665665

666666
Deprecations
667667
~~~~~~~~~~~~
668+
- Deprecated parsing datetime strings with system-local timezone to ``tzlocal``, pass a ``tz`` keyword or explicitly call ``tz_localize`` instead (:issue:`50791`)
668669
- 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`)
669670
- Deprecated behavior of :func:`to_datetime` with ``unit`` when parsing strings, in a future version these will be parsed as datetimes (matching unit-less behavior) instead of cast to floats. To retain the old behavior, cast strings to numeric types before calling :func:`to_datetime` (:issue:`50735`)
670671
- Deprecated :func:`pandas.io.sql.execute` (:issue:`50185`)

pandas/_libs/tslibs/parsing.pyx

+12
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,18 @@ cdef datetime dateutil_parse(
686686
ret = ret + relativedelta.relativedelta(weekday=res.weekday)
687687
if not ignoretz:
688688
if res.tzname and res.tzname in time.tzname:
689+
# GH#50791
690+
if res.tzname != "UTC":
691+
# If the system is localized in UTC (as many CI runs are)
692+
# we get tzlocal, once the deprecation is enforced will get
693+
# timezone.utc, not raise.
694+
warnings.warn(
695+
"Parsing '{res.tzname}' as tzlocal (dependent on system timezone) "
696+
"is deprecated and will raise in a future version. Pass the 'tz' "
697+
"keyword or call tz_localize after construction instead",
698+
FutureWarning,
699+
stacklevel=find_stack_level()
700+
)
689701
ret = ret.replace(tzinfo=_dateutil_tzlocal())
690702
elif res.tzoffset == 0:
691703
ret = ret.replace(tzinfo=_dateutil_tzutc())

pandas/tests/tslibs/test_array_to_datetime.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def test_parsing_non_iso_timezone_offset():
7171
dt_string = "01-01-2013T00:00:00.000000000+0000"
7272
arr = np.array([dt_string], dtype=object)
7373

74-
result, result_tz = tslib.array_to_datetime(arr)
74+
with tm.assert_produces_warning(None):
75+
# GH#50949 should not get tzlocal-deprecation warning here
76+
result, result_tz = tslib.array_to_datetime(arr)
7577
expected = np.array([np.datetime64("2013-01-01 00:00:00.000000000")])
7678

7779
tm.assert_numpy_array_equal(result, expected)

pandas/tests/tslibs/test_parsing.py

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66

77
from dateutil.parser import parse as du_parse
8+
from dateutil.tz import tzlocal
89
import numpy as np
910
import pytest
1011

@@ -18,6 +19,23 @@
1819
import pandas._testing as tm
1920

2021

22+
@td.skip_if_windows
23+
def test_parsing_tzlocal_deprecated():
24+
# GH#50791
25+
msg = "Pass the 'tz' keyword or call tz_localize after construction instead"
26+
dtstr = "Jan 15 2004 03:00 EST"
27+
28+
with tm.set_timezone("US/Eastern"):
29+
with tm.assert_produces_warning(FutureWarning, match=msg):
30+
res, _ = parse_datetime_string_with_reso(dtstr)
31+
32+
assert isinstance(res.tzinfo, tzlocal)
33+
34+
with tm.assert_produces_warning(FutureWarning, match=msg):
35+
res = parsing.parse_datetime_string(dtstr)
36+
assert isinstance(res.tzinfo, tzlocal)
37+
38+
2139
def test_parse_datetime_string_with_reso():
2240
(parsed, reso) = parse_datetime_string_with_reso("4Q1984")
2341
(parsed_lower, reso_lower) = parse_datetime_string_with_reso("4q1984")

0 commit comments

Comments
 (0)