Skip to content

BUG: Do not round DatetimeIndex nanosecond precision when iterating #19628

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 2 commits into from
Feb 14, 2018
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ Timezones
- Bug in :func:`DatetimeIndex.insert` where inserting ``NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`)
- Bug in the :class:`DataFrame` constructor, where tz-aware Datetimeindex and a given column name will result in an empty ``DataFrame`` (:issue:`19157`)
- Bug in :func:`Timestamp.tz_localize` where localizing a timestamp near the minimum or maximum valid values could overflow and return a timestamp with an incorrect nanosecond value (:issue:`12677`)
- Bug when iterating over :class:`DatetimeIndex` that was localized with fixed timezone offset that rounded nanosecond precision to microseconds (:issue:`19603`)

Offsets
^^^^^^^
Expand Down
15 changes: 8 additions & 7 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ from tslibs.timezones cimport (is_utc, is_tzlocal, is_fixed_offset,
treat_tz_as_pytz, get_dst_info)
from tslibs.conversion cimport (tz_convert_single, _TSObject,
convert_datetime_to_tsobject,
get_datetime64_nanos)
get_datetime64_nanos,
tz_convert_utc_to_tzlocal)
from tslibs.conversion import tz_convert_single

from tslibs.nattype import NaT, nat_strings, iNaT
Expand Down Expand Up @@ -144,12 +145,12 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None,
if value == NPY_NAT:
result[i] = NaT
else:
dt64_to_dtstruct(value, &dts)
dt = create_datetime_from_ts(value, dts, tz, freq)
dt = dt + tz.utcoffset(dt)
if box:
dt = Timestamp(dt)
result[i] = dt
# Python datetime objects do not support nanosecond
Copy link
Contributor

Choose a reason for hiding this comment

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

use tz_convert_tzlocal_to_utc here from conversion.pyx

# resolution (yet, PEP 564). Need to compute new value
# using the i8 representation.
local_value = tz_convert_utc_to_tzlocal(value, tz)
dt64_to_dtstruct(local_value, &dts)
result[i] = func_create(value, dts, tz, freq)
else:
trans, deltas, typ = get_dst_info(tz)

Expand Down
6 changes: 6 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,9 @@ def compression_no_zip(request):
except zip
"""
return request.param


@pytest.fixture(scope='module')
def datetime_tz_utc():
from datetime import timezone
return timezone.utc
13 changes: 12 additions & 1 deletion pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pandas as pd
from pandas._libs import tslib
from pandas._libs.tslibs import timezones
from pandas.compat import lrange, zip
from pandas.compat import lrange, zip, PY3
from pandas import (DatetimeIndex, date_range, bdate_range,
Timestamp, isna, to_datetime, Index)

Expand Down Expand Up @@ -949,6 +949,17 @@ def test_dti_union_aware(self):
result = rng.union(rng2)
assert result.tz.zone == 'UTC'

@pytest.mark.parametrize('tz', [None, 'UTC', "US/Central",
dateutil.tz.tzoffset(None, -28800)])
@pytest.mark.usefixtures("datetime_tz_utc")
@pytest.mark.skipif(not PY3, reason="datetime.timezone not in PY2")
def test_iteration_preserves_nanoseconds(self, tz):
# GH 19603
index = DatetimeIndex(["2018-02-08 15:00:00.168456358",
"2018-02-08 15:00:00.168456359"], tz=tz)
for i, ts in enumerate(index):
assert ts == index[i]


class TestDateRange(object):
"""Tests for date_range with timezones"""
Expand Down