Skip to content

Commit 5eb3ae3

Browse files
committed
Fix to _round for timestamps to accommodate numpy int64
Also minor PEP8 edits to timestamp tests
1 parent 7a34bcb commit 5eb3ae3

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

pandas/_libs/tslibs/timestamps.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,6 @@ class Timestamp(_Timestamp):
601601
value = self.tz_localize(None).value
602602
else:
603603
value = self.value
604-
605604
if unit < 1000:
606605
# for nano rounding, work with the last 6 digits separately
607606
# due to float precision
@@ -617,9 +616,9 @@ class Timestamp(_Timestamp):
617616
if unit >= 1e9:
618617
divisor = 10 ** int(np.log10(unit / 1e7))
619618
else:
620-
divisor = 1
619+
divisor = 10
621620

622-
r = (unit * rounder((value * divisor / float(unit)) / divisor)
621+
r = (unit * rounder((value * (divisor / float(unit))) / divisor)
623622
.astype('i8'))
624623

625624
result = Timestamp(r, unit='ns')

pandas/tests/scalar/test_timestamp.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from datetime import datetime, timedelta
1414
from distutils.version import LooseVersion
1515
from pytz.exceptions import AmbiguousTimeError, NonExistentTimeError
16-
from collections import namedtuple
1716

1817
import pandas.util.testing as tm
1918
import pandas.util._test_decorators as td
@@ -661,11 +660,15 @@ def test_to_pydatetime_nonzero_nano(self):
661660
('20130101 09:10:11', 'floor', 'D', '20130101', {}),
662661
# GH 19206 - times far in the future and past rounding incorrectly
663662
('2117-01-01 00:00:45', 'floor', '15s', '2117-01-01 00:00:45', {}),
663+
('2117-01-01 00:00:45', 'ceil', '15s', '2117-01-01 00:00:45', {}),
664664
('2117-01-01 00:00:45.000000012', 'floor', '10ns',
665665
'2117-01-01 00:00:45.000000010', {}),
666666
('1823-01-01 00:00:01', 'floor', '1s', '1823-01-01 00:00:01', {}),
667+
('1823-01-01 00:00:01', 'ceil', '1s', '1823-01-01 00:00:01', {}),
667668
('1823-01-01 00:00:01.000000012', 'floor', '10ns',
668669
'1823-01-01 00:00:01.000000010', {}),
670+
('1823-01-01 00:00:01.000000012', 'ceil', '10ns',
671+
'1823-01-01 00:00:01.000000020', {}),
669672
# ----
670673
('20130101 09:10:11', 'ceil', 'D', '20130102', {}),
671674
('20130101 09:10:11', 'round', 'D', '20130101', {}),

0 commit comments

Comments
 (0)