Skip to content

ENH: Support datetime.timezone objects #25065

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 31 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8ff8024
ENH: Support datetime.timezone objects
Jan 30, 2019
4ade114
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Jan 31, 2019
8ff047b
add comment and simplify check
Jan 31, 2019
5076045
flake8 and modify check
Feb 1, 2019
f819b26
Remove check
Feb 3, 2019
98c3fd2
cdef timezone.utc
Feb 3, 2019
224fcc9
cannot cdef tzinfo
Feb 3, 2019
b3634f4
PY3.5 support
Feb 3, 2019
2a082ed
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 3, 2019
bfb2fd3
Add whatsnew
Feb 3, 2019
35b475e
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 4, 2019
a8f163e
clarify whatsnew
Feb 4, 2019
07e9b57
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 8, 2019
fcf84ae
update note in timeseries.rst
Feb 8, 2019
6d9af30
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 8, 2019
af64d24
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 9, 2019
f052721
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 9, 2019
ac34a1b
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 11, 2019
12e163d
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 13, 2019
a98d1fa
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 14, 2019
c3d01bf
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 16, 2019
9f44011
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Feb 17, 2019
6649396
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Mar 4, 2019
b8354c2
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Mar 5, 2019
d74673e
Address comments
Mar 5, 2019
c0ad4b4
Remove extra or
Mar 5, 2019
96e068b
Add versionadded tag
Mar 5, 2019
67de9c0
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Mar 6, 2019
956822d
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Mar 13, 2019
a843ae1
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Mar 16, 2019
1c6165b
Merge remote-tracking branch 'upstream/master' into datetime_timezone…
Mar 17, 2019
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
7 changes: 2 additions & 5 deletions doc/source/user_guide/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2149,12 +2149,9 @@ Time Zone Handling
------------------

pandas provides rich support for working with timestamps in different time
zones using the ``pytz`` and ``dateutil`` libraries.
zones using the ``pytz`` and ``dateutil`` libraries or ``datetime.timezone``
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that :class:datetime.timezone will work.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like there are a few more mentions of what's supported

  • Reword L272 to include datetime.timezone
  • Add a daetetime.timezone example in the example ~L2181

objects from the standard library.

.. note::

pandas does not yet support ``datetime.timezone`` objects from the standard
library.

Working with Time Zones
~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Other Enhancements
- ``Series.str`` has gained :meth:`Series.str.casefold` method to removes all case distinctions present in a string (:issue:`25405`)
- :meth:`DataFrame.set_index` now works for instances of ``abc.Iterator``, provided their output is of the same length as the calling frame (:issue:`22484`, :issue:`24984`)
- :meth:`DatetimeIndex.union` now supports the ``sort`` argument. The behaviour of the sort parameter matches that of :meth:`Index.union` (:issue:`24994`)
-
- :meth:`datetime.timezone` objects are now supported as arguments to timezone methods and constructors (:issue:`25065`)
Copy link
Contributor

Choose a reason for hiding this comment

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

meth -> class (not sure if it matters).


.. _whatsnew_0250.api_breaking:

Expand Down
6 changes: 5 additions & 1 deletion pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from datetime import timezone

# dateutil compat
from dateutil.tz import (
Expand All @@ -23,11 +24,12 @@ from pandas._libs.tslibs.util cimport (
is_string_object, is_integer_object, get_nat)

cdef int64_t NPY_NAT = get_nat()
cdef object utc_stdlib = timezone.utc

# ----------------------------------------------------------------------

cpdef inline bint is_utc(object tz):
return tz is UTC or isinstance(tz, _dateutil_tzutc)
return tz is UTC or isinstance(tz, _dateutil_tzutc) or tz is utc_stdlib
Copy link
Contributor

Choose a reason for hiding this comment

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

Potential micro optimization: I suspect that tz is utc_stdlib is relatively cheap compared to isinstance(tz, _dateutil_tzutc). Maybe swap the order of those two for a better average case? Not sure if this matters.



cdef inline bint is_tzlocal(object tz):
Expand Down Expand Up @@ -167,6 +169,8 @@ cdef inline bint is_fixed_offset(object tz):
return 1
else:
return 0
# This also implicitly accepts datetime.timezone objects which are
# considered fixed
return 1


Expand Down
8 changes: 4 additions & 4 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import date, time, timedelta
from datetime import date, time, timedelta, timezone
from decimal import Decimal
import os

Expand Down Expand Up @@ -247,13 +247,11 @@ def writable(request):

@pytest.fixture(scope='module')
def datetime_tz_utc():
from datetime import timezone
return timezone.utc


utc_objs = ['utc', 'dateutil/UTC', utc, tzutc()]
if PY3:
from datetime import timezone
utc_objs.append(timezone.utc)


Expand Down Expand Up @@ -366,7 +364,9 @@ def unique_nulls_fixture(request):

TIMEZONES = [None, 'UTC', 'US/Eastern', 'Asia/Tokyo', 'dateutil/US/Pacific',
'dateutil/Asia/Singapore', tzutc(), tzlocal(), FixedOffset(300),
FixedOffset(0), FixedOffset(-300)]
FixedOffset(0), FixedOffset(-300), timezone.utc,
timezone(timedelta(hours=1)),
timezone(timedelta(hours=-1), name='foo')]


@td.parametrize_fixture_doc(str(TIMEZONES))
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_construction_with_alt_tz_localize(self, kwargs, tz_aware_fixture):
i = pd.date_range('20130101', periods=5, freq='H', tz=tz)
kwargs = {key: attrgetter(val)(i) for key, val in kwargs.items()}

if str(tz) in ('UTC', 'tzutc()'):
if str(tz) in ('UTC', 'tzutc()', 'UTC+00:00'):
warn = None
else:
warn = FutureWarning
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,8 @@ def test_constructor_dtypes_datetime(self, tz_naive_fixture, attr, utc,
# TODO(GH-24559): Remove the sys.modules and warnings
# not sure what this is from. It's Py2 only.
modules = [sys.modules['pandas.core.indexes.base']]

if (tz_naive_fixture and attr == "asi8" and
str(tz_naive_fixture) not in ('UTC', 'tzutc()')):
str(tz_naive_fixture) not in ('UTC', 'tzutc()', 'UTC+00:00')):
ex_warn = FutureWarning
else:
ex_warn = None
Expand Down