Skip to content

Commit 700f83c

Browse files
committed
Bug: Do not round DatetimeIndex nanosecond precision when iterating
1 parent 6485a36 commit 700f83c

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@ Timezones
663663
- Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`)
664664
- Bug in :func:`DatetimeIndex.insert` where inserting ``NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`)
665665
- Bug in the :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`)
666+
- Bug iterating over :class:`DatetimeIndex` that was localized with ``datetime.timezone.utc`` that rounded nanosecond precision to microseconds (:issue:`19603`)
666667

667668
Offsets
668669
^^^^^^^

pandas/_libs/tslibs/timezones.pyx

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ cimport numpy as cnp
2222
from numpy cimport ndarray, int64_t
2323
cnp.import_array()
2424

25+
from pandas.compat import PY3
26+
2527
# ----------------------------------------------------------------------
2628
from util cimport is_string_object, is_integer_object, get_nat
2729

@@ -30,6 +32,10 @@ cdef int64_t NPY_NAT = get_nat()
3032
# ----------------------------------------------------------------------
3133

3234
cdef inline bint is_utc(object tz):
35+
if PY3:
36+
from datetime import timezone
37+
return (tz is UTC or isinstance(tz, _dateutil_tzutc) or
38+
tz is timezone.utc)
3339
return tz is UTC or isinstance(tz, _dateutil_tzutc)
3440

3541

pandas/tests/indexes/datetimes/test_datetime.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
import dateutil
88
import pandas as pd
99
import pandas.util.testing as tm
10-
from pandas.compat import lrange
10+
from pandas.compat import lrange, PY3
1111
from pandas import (DatetimeIndex, Index, date_range, DataFrame,
1212
Timestamp, offsets)
1313

1414
from pandas.util.testing import assert_almost_equal
1515

16+
if PY3:
17+
from datetime import timezone
18+
dt_UTC = timezone.utc
19+
else:
20+
dt_UTC = None
21+
1622
randn = np.random.randn
1723

1824

@@ -258,6 +264,12 @@ def test_iteration_preserves_tz(self):
258264
assert result._repr_base == expected._repr_base
259265
assert result == expected
260266

267+
@pytest.mark.parametrize('tz', [None, 'UTC', "US/Central", dt_UTC])
268+
def test_iteration_preserves_nanoseconds(self, tz):
269+
# GH 19603
270+
index = pd.DatetimeIndex(["2018-02-08 15:00:00.168456358"], tz=tz)
271+
assert list(index)[0] == index[0]
272+
261273
def test_misc_coverage(self):
262274
rng = date_range('1/1/2000', periods=5)
263275
result = rng.groupby(rng.day)

0 commit comments

Comments
 (0)