Skip to content

Commit 0a6eecd

Browse files
committed
Refactored rounding in DatetimeIndex to use rounding function exported from timestamps
1 parent 38deeaf commit 0a6eecd

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

pandas/core/indexes/datetimelike.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from pandas._libs import lib, iNaT, NaT
3737
from pandas._libs.tslibs.period import Period
3838
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds
39+
from pandas._libs.tslibs.timestamps import round_ns
3940

4041
from pandas.core.indexes.base import Index, _index_shared_docs
4142
from pandas.util._decorators import Appender, cache_readonly
@@ -91,33 +92,34 @@ class TimelikeOps(object):
9192

9293
def _round(self, freq, rounder):
9394

94-
from pandas.tseries.frequencies import to_offset
95-
unit = to_offset(freq).nanos
95+
# from pandas.tseries.frequencies import to_offset
96+
# unit = to_offset(freq).nanos
9697
# round the local times
9798
values = _ensure_datetimelike_to_i8(self)
9899

99-
if unit < 1000:
100-
# for nano rounding, work with the last 6 digits separately
101-
# due to float precision
102-
buff = 1000000
103-
result = (buff * (values // buff) + unit *
104-
(rounder((values % buff) * (1 / float(unit))))
105-
.astype('i8'))
106-
else:
107-
if unit % 1000 != 0:
108-
msg = 'Precision will be lost using frequency: {}'
109-
warnings.warn(msg.format(freq))
110-
111-
# GH19206
112-
# to deal with round-off when unit is large
113-
if unit >= 1e9:
114-
divisor = 10 ** int(np.log10(unit / 1e7))
115-
else:
116-
divisor = 10
117-
118-
result = (unit * rounder((values * (divisor / float(unit))) /
119-
divisor).astype('i8'))
120-
100+
# if unit < 1000:
101+
# # for nano rounding, work with the last 6 digits separately
102+
# # due to float precision
103+
# buff = 1000000
104+
# result = (buff * (values // buff) + unit *
105+
# (rounder((values % buff) * (1 / float(unit))))
106+
# .astype('i8'))
107+
# else:
108+
# if unit % 1000 != 0:
109+
# msg = 'Precision will be lost using frequency: {}'
110+
# warnings.warn(msg.format(freq))
111+
#
112+
# # GH19206
113+
# # to deal with round-off when unit is large
114+
# if unit >= 1e9:
115+
# divisor = 10 ** int(np.log10(unit / 1e7))
116+
# else:
117+
# divisor = 10
118+
#
119+
# result = (unit * rounder((values * (divisor / float(unit))) /
120+
# divisor).astype('i8'))
121+
122+
result = round_ns(values, rounder, freq)
121123
result = self._maybe_mask_results(result, fill_value=NaT)
122124

123125
attribs = self._get_attributes_dict()

0 commit comments

Comments
 (0)