Skip to content

PERF: Enable %z in parsing datetime #32984

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 9 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion asv_bench/benchmarks/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,33 @@ def time_infer_quarter(self):

class ToDatetimeFormat:
def setup(self):
self.s = Series(["19MAY11", "19MAY11:00:00:00"] * 100000)
N = 100000
self.s = Series(["19MAY11", "19MAY11:00:00:00"] * N)
self.s2 = self.s.str.replace(":\\S+$", "")

self.same_offset = ["10/11/2018 00:00:00.045-07:00"] * N
self.diff_offset = [
f"10/11/2018 00:00:00.045-0{offset}:00" for offset in range(10)
] * int(N / 10)

def time_exact(self):
to_datetime(self.s2, format="%d%b%y")

def time_no_exact(self):
to_datetime(self.s, format="%d%b%y", exact=False)

def time_same_offset(self):
to_datetime(self.same_offset, format="%m/%d/%Y %H:%M:%S.%f%z")

def time_different_offset(self):
to_datetime(self.diff_offset, format="%m/%d/%Y %H:%M:%S.%f%z")

def time_same_offset_to_utc(self):
to_datetime(self.same_offset, format="%m/%d/%Y %H:%M:%S.%f%z", utc=True)

def time_different_offset_to_utc(self):
to_datetime(self.diff_offset, format="%m/%d/%Y %H:%M:%S.%f%z", utc=True)


class ToDatetimeCache:

Expand Down
16 changes: 16 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ For example:

For more on working with fold, see :ref:`Fold subsection <timeseries.fold>` in the user guide.

.. _whatsnew_110.to_datetime_multiple_tzname_tzoffset_support:

Parsing timezone-aware format with different timezones in to_datetime
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:func:`to_datetime` now supports parsing formats containing timezone names (``%Z``) and UTC offsets (``%z``) from different timezones then converting them to UTC by setting ``utc=True``. This would return a :class:`DatetimeIndex` with timezone at UTC as opposed to an :class:`Index` with ``object`` dtype if ``utc=True`` is not set (:issue:`32792`).

For example:

.. ipython:: python

tz_strs = ["2010-01-01 12:00:00 +0100", "2010-01-01 12:00:00 -0100",
"2010-01-01 12:00:00 +0300", "2010-01-01 12:00:00 +0400"]
pd.to_datetime(tz_strs, format='%Y-%m-%d %H:%M:%S %z', utc=True)
pd.to_datetime(tz_strs, format='%Y-%m-%d %H:%M:%S %z')

.. _whatsnew_110.enhancements.other:

Other enhancements
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,12 @@ def _return_parsed_timezone_results(result, timezones, tz, name):
-------
tz_result : Index-like of parsed dates with timezone
"""
if tz is not None:
raise ValueError(
"Cannot pass a tz argument when parsing strings with timezone information."
)
tz_results = np.array(
[Timestamp(res).tz_localize(zone) for res, zone in zip(result, timezones)]
)
if tz is not None:
# Convert to the same tz
tz_results = np.array([tz_result.tz_convert(tz) for tz_result in tz_results])
from pandas import Index

return Index(tz_results, name=name)
Expand Down
21 changes: 19 additions & 2 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,25 @@ def test_to_datetime_parse_tzname_or_tzoffset(self, fmt, dates, expected_dates):
expected = pd.Index(expected_dates)
tm.assert_equal(result, expected)

with pytest.raises(ValueError):
pd.to_datetime(dates, format=fmt, utc=True)
def test_to_datetime_parse_tzname_or_tzoffset_different_tz_to_utc(self):
# GH 32792
dates = [
"2010-01-01 12:00:00 +0100",
"2010-01-01 12:00:00 -0100",
"2010-01-01 12:00:00 +0300",
"2010-01-01 12:00:00 +0400",
]
expected_dates = [
"2010-01-01 11:00:00+00:00",
"2010-01-01 13:00:00+00:00",
"2010-01-01 09:00:00+00:00",
"2010-01-01 08:00:00+00:00",
]
fmt = "%Y-%m-%d %H:%M:%S %z"

result = pd.to_datetime(dates, format=fmt, utc=True)
expected = pd.DatetimeIndex(expected_dates)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"offset", ["+0", "-1foo", "UTCbar", ":10", "+01:000:01", ""]
Expand Down