Skip to content

Commit 6ab2c06

Browse files
committed
Refactored _round
1 parent 852d0ed commit 6ab2c06

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

pandas/_libs/tslibs/timestamps.pyx

+13-16
Original file line numberDiff line numberDiff line change
@@ -602,27 +602,24 @@ class Timestamp(_Timestamp):
602602
else:
603603
value = self.value
604604

605-
# GH 19206
606-
# to deal with round-off when unit is large
607-
if unit >= 1e9:
608-
divisor = 10 ** int(np.log10(unit / 1e7))
609-
else:
610-
divisor = 1
611-
612-
if unit < 1000 and unit % 1000 != 0:
605+
if unit < 1000:
613606
# for nano rounding, work with the last 6 digits separately
614607
# due to float precision
615608
r = (buff * (value // buff) + unit *
616609
(rounder((value % buff) * (1 / float(unit)))).astype('i8'))
617-
elif unit >= 1000 and unit % 1000 != 0:
618-
msg = 'Precision will be lost using frequency: {}'
619-
warnings.warn(msg.format(freq))
620-
r = (unit *
621-
rounder((value * divisor / float(unit)) / divisor)
622-
.astype('i8'))
623610
else:
624-
r = (unit *
625-
rounder((value * divisor / float(unit)) / divisor)
611+
if unit % 1000 != 0:
612+
msg = 'Precision will be lost using frequency: {}'
613+
warnings.warn(msg.format(freq))
614+
615+
# GH19206
616+
# to deal with round-off when unit is large
617+
if unit >= 1e9:
618+
divisor = 10 ** int(np.log10(unit / 1e7))
619+
else:
620+
divisor = 1
621+
622+
r = (unit * rounder((value * divisor / float(unit)) / divisor)
626623
.astype('i8'))
627624

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

0 commit comments

Comments
 (0)