Skip to content

DEPR: parsing to tzlocal #50949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
8 changes: 8 additions & 0 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/tslibs/test_array_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't a UTC offset like this be okay?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why but this triggered the tzlocal warning on one the builds. best guess is that build had the system timezone set to gmt

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I thought the spirit of the original deprecation is to forbid the 3 letter abbreviated timezones?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the spirit is to not give results that depend on your system timezone. so if there is a system-config that results in getting tzlocal in this case, that user will in the future get the same non-tzlocal tzinfo that everyone else gets. i.e. this is not going to start raising for anybody.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay that makes sense. I am just concerned about the seemingly false positive for this test case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. im open to suggestions on how to avoid it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like many CI runs have time.tzname == ("UTC", "UTC"), end up getting tzlocal instead of a tzutc(). Updated to avoid warning in that case.

arr = np.array([dt_string], dtype=object)
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/tslibs/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -18,6 +19,23 @@
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"
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")
Expand Down