Skip to content

Commit 46b8aac

Browse files
committed
Fix to _round for timestamps to accommodate numpy int64
Also minor PEP8 edits to timestamp tests
1 parent b2f2113 commit 46b8aac

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 2 additions & 3 deletions
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

Lines changed: 4 additions & 1 deletion
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
@@ -749,11 +748,15 @@ def test_to_pydatetime_nonzero_nano(self):
749748
('20130101 09:10:11', 'floor', 'D', '20130101', {}),
750749
# GH 19206 - times far in the future and past rounding incorrectly
751750
('2117-01-01 00:00:45', 'floor', '15s', '2117-01-01 00:00:45', {}),
751+
('2117-01-01 00:00:45', 'ceil', '15s', '2117-01-01 00:00:45', {}),
752752
('2117-01-01 00:00:45.000000012', 'floor', '10ns',
753753
'2117-01-01 00:00:45.000000010', {}),
754754
('1823-01-01 00:00:01', 'floor', '1s', '1823-01-01 00:00:01', {}),
755+
('1823-01-01 00:00:01', 'ceil', '1s', '1823-01-01 00:00:01', {}),
755756
('1823-01-01 00:00:01.000000012', 'floor', '10ns',
756757
'1823-01-01 00:00:01.000000010', {}),
758+
('1823-01-01 00:00:01.000000012', 'ceil', '10ns',
759+
'1823-01-01 00:00:01.000000020', {}),
757760
# ----
758761
('20130101 09:10:11', 'ceil', 'D', '20130102', {}),
759762
('20130101 09:10:11', 'round', 'D', '20130101', {}),

0 commit comments

Comments
 (0)