Skip to content

BUG: Timestamp.round precision error for ns (#15578) #15588

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ Bug Fixes
- Bug in ``Index`` power operations with reversed operands (:issue:`14973`)
- Bug in ``TimedeltaIndex`` addition where overflow was being allowed without error (:issue:`14816`)
- Bug in ``TimedeltaIndex`` raising a ``ValueError`` when boolean indexing with ``loc`` (:issue:`14946`)
- Bug in ``DatetimeIndex.round()`` and ``Timestamp.round()`` floating point accuracy when rounding by milliseconds (:issue: `14440`)
- Bug in ``DatetimeIndex.round()`` and ``Timestamp.round()`` floating point accuracy when rounding by milliseconds or less (:issue: `14440`, :issue:`15578`)
- Bug in ``astype()`` where ``inf`` values were incorrectly converted to integers. Now raises error now with ``astype()`` for Series and DataFrames (:issue:`14265`)
- Bug in ``DataFrame(..).apply(to_numeric)`` when values are of type decimal.Decimal. (:issue:`14827`)
- Bug in ``describe()`` when passing a numpy array which does not contain the median to the ``percentiles`` keyword argument (:issue:`14908`)
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,29 @@ def test_round(self):
tm.assertRaisesRegexp(ValueError, msg, rng.round, freq='M')
tm.assertRaisesRegexp(ValueError, msg, elt.round, freq='M')

# GH 14440
# GH 14440 & 15578
index = pd.DatetimeIndex(['2016-10-17 12:00:00.0015'], tz=tz)
result = index.round('ms')
expected = pd.DatetimeIndex(['2016-10-17 12:00:00.002000'], tz=tz)
tm.assert_index_equal(result, expected)

for freq in ['us', 'ns']:
tm.assert_index_equal(index, index.round(freq))

index = pd.DatetimeIndex(['2016-10-17 12:00:00.00149'], tz=tz)
result = index.round('ms')
expected = pd.DatetimeIndex(['2016-10-17 12:00:00.001000'], tz=tz)
tm.assert_index_equal(result, expected)

index = pd.DatetimeIndex(['2016-10-17 12:00:00.001501031'])
result = index.round('10ns')
expected = pd.DatetimeIndex(['2016-10-17 12:00:00.001501030'])
tm.assert_index_equal(result, expected)

with tm.assert_produces_warning():
ts = '2016-10-17 12:00:00.001501031'
pd.DatetimeIndex([ts]).round('1010ns')

def test_repeat_range(self):
rng = date_range('1/1/2000', '1/1/2001')

Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/scalar/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ def test_round(self):
for freq in ['Y', 'M', 'foobar']:
self.assertRaises(ValueError, lambda: dti.round(freq))

# GH 14440
# GH 14440 & 15578
result = pd.Timestamp('2016-10-17 12:00:00.0015').round('ms')
expected = pd.Timestamp('2016-10-17 12:00:00.002000')
self.assertEqual(result, expected)
Expand All @@ -741,6 +741,17 @@ def test_round(self):
expected = pd.Timestamp('2016-10-17 12:00:00.001000')
self.assertEqual(result, expected)

ts = pd.Timestamp('2016-10-17 12:00:00.0015')
for freq in ['us', 'ns']:
self.assertEqual(ts, ts.round(freq))

result = pd.Timestamp('2016-10-17 12:00:00.001501031').round('10ns')
expected = pd.Timestamp('2016-10-17 12:00:00.001501030')
self.assertEqual(result, expected)

with tm.assert_produces_warning():
pd.Timestamp('2016-10-17 12:00:00.001501031').round('1010ns')

def test_class_ops_pytz(self):
tm._skip_if_no_pytz()
from pytz import timezone
Expand Down
16 changes: 13 additions & 3 deletions pandas/tseries/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Base and utility classes for tseries type pandas objects.
"""
import warnings

from datetime import datetime, timedelta

Expand Down Expand Up @@ -79,11 +80,20 @@ def _round(self, freq, rounder):

from pandas.tseries.frequencies import to_offset
unit = to_offset(freq).nanos

# round the local times
values = _ensure_datetimelike_to_i8(self)

result = (unit * rounder(values / float(unit)).astype('i8'))
if unit < 1000 and unit % 1000 != 0:
# for nano rounding, work with the last 6 digits separately
# due to float precision
buff = 1000000
result = (buff * (values // buff) + unit *
(rounder((values % buff) / float(unit))).astype('i8'))
elif unit >= 1000 and unit % 1000 != 0:
msg = 'Precision will be lost using frequency: {}'
warnings.warn(msg.format(freq))
result = (unit * rounder(values / float(unit)).astype('i8'))
else:
result = (unit * rounder(values / float(unit)).astype('i8'))
result = self._maybe_mask_results(result, fill_value=tslib.NaT)
attribs = self._get_attributes_dict()
if 'freq' in attribs:
Expand Down
13 changes: 12 additions & 1 deletion pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,18 @@ class Timestamp(_Timestamp):
value = self.tz_localize(None).value
else:
value = self.value
result = (unit * rounder(value / float(unit)).astype('i8'))
if unit < 1000 and unit % 1000 != 0:
# for nano rounding, work with the last 6 digits separately
# due to float precision
buff = 1000000
result = (buff * (value // buff) + unit *
(rounder((value % buff) / float(unit))).astype('i8'))
elif unit >= 1000 and unit % 1000 != 0:
msg = 'Precision will be lost using frequency: {}'
warnings.warn(msg.format(freq))
result = (unit * rounder(value / float(unit)).astype('i8'))
else:
result = (unit * rounder(value / float(unit)).astype('i8'))
result = Timestamp(result, unit='ns')
if self.tz is not None:
result = result.tz_localize(self.tz)
Expand Down