Skip to content

Commit f6f131b

Browse files
committed
code review: adding docs, references, minor formatting
1 parent eee47a6 commit f6f131b

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ Datetimelike
646646
- Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise ``OverflowError`` (:issue:`22492`, :issue:`22508`)
647647
- Bug in :class:`DatetimeIndex` incorrectly allowing indexing with ``Timedelta`` object (:issue:`20464`)
648648
- Bug in :class:`DatetimeIndex` where frequency was being set if original frequency was ``None`` (:issue:`22150`)
649+
- Bug in rounding methods of :class:`DatetimeIndex` (:meth:`~DatetimeIndex.round`, :meth:`~DatetimeIndex.ceil`, :meth:`~DatetimeIndex.floor`) and :class:`Timestamp` (:meth:`~Timestamp.round`, :meth:`~Timestamp.ceil`, :meth:`~Timestamp.floor`) could give raise to loss of precision (:issue:`22591`)
649650

650651
Timedelta
651652
^^^^^^^^^

pandas/_libs/tslibs/timestamps.pyx

+32
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ cdef inline object create_timestamp_from_ts(int64_t value,
6060

6161
@enum.unique
6262
class RoundTo(enum.Enum):
63+
"""
64+
enumeration defining the available rounding modes
65+
66+
Attributes
67+
----------
68+
MINUS_INFTY
69+
round towards -∞, or floor [2]_
70+
PLUS_INFTY
71+
round towards +∞, or ceil [3]_
72+
NEAREST_HALF_EVEN
73+
round to nearest, tie-break half to even [6]_
74+
NEAREST_HALF_MINUS_INFTY
75+
round to nearest, tie-break half to -∞ [5]_
76+
NEAREST_HALF_PLUS_INFTY
77+
round to nearest, tie-break half to +∞ [4]_
78+
79+
80+
References
81+
----------
82+
.. [1] "Rounding - Wikipedia"
83+
https://en.wikipedia.org/wiki/Rounding
84+
.. [2] "Rounding down"
85+
https://en.wikipedia.org/wiki/Rounding#Rounding_down
86+
.. [3] "Rounding up"
87+
https://en.wikipedia.org/wiki/Rounding#Rounding_up
88+
.. [4] "Round half up"
89+
https://en.wikipedia.org/wiki/Rounding#Round_half_up
90+
.. [5] "Round half down"
91+
https://en.wikipedia.org/wiki/Rounding#Round_half_down
92+
.. [6] "Round half to even"
93+
https://en.wikipedia.org/wiki/Rounding#Round_half_to_even
94+
"""
6395
MINUS_INFTY = 0
6496
PLUS_INFTY = 1
6597
NEAREST_HALF_EVEN = 2

pandas/tests/indexes/datetimes/test_scalar_compat.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -184,27 +184,30 @@ def test_ceil_floor_edge(self, test_input, rounder, freq, expected):
184184
def test_round_int64(self, start, index_freq, periods, rounding_freq):
185185
dt = DatetimeIndex(start=start, freq=index_freq, periods=periods)
186186
unit = to_offset(rounding_freq).nanos
187+
187188
# test floor
188-
result = dt.floor(rounding_freq).asi8
189-
diff = dt.asi8 - result
190-
mod = result % unit
189+
result = dt.floor(rounding_freq)
190+
diff = dt.asi8 - result.asi8
191+
mod = result.asi8 % unit
191192
assert (mod == 0).all(), "floor not a %s multiple" % (rounding_freq, )
192193
assert (0 <= diff).all() and (diff < unit).all(), "floor error"
194+
193195
# test ceil
194-
result = dt.ceil(rounding_freq).asi8
195-
diff = result - dt.asi8
196-
mod = result % unit
196+
result = dt.ceil(rounding_freq)
197+
diff = result.asi8 - dt.asi8
198+
mod = result.asi8 % unit
197199
assert (mod == 0).all(), "ceil not a %s multiple" % (rounding_freq, )
198200
assert (0 <= diff).all() and (diff < unit).all(), "ceil error"
201+
199202
# test round
200-
result = dt.round(rounding_freq).asi8
201-
diff = abs(result - dt.asi8)
202-
mod = result % unit
203+
result = dt.round(rounding_freq)
204+
diff = abs(result.asi8 - dt.asi8)
205+
mod = result.asi8 % unit
203206
assert (mod == 0).all(), "round not a %s multiple" % (rounding_freq, )
204207
assert (diff <= unit // 2).all(), "round error"
205208
if unit % 2 == 0:
206209
assert (
207-
result[diff == unit // 2] % 2 == 0
210+
result.asi8[diff == unit // 2] % 2 == 0
208211
).all(), "round half to even error"
209212

210213
# ----------------------------------------------------------------

pandas/tests/scalar/timestamp/test_unary_ops.py

+3
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,17 @@ def test_round_int64(self, timestamp, freq):
176176
"""
177177
dt = Timestamp(timestamp)
178178
unit = to_offset(freq).nanos
179+
179180
# test floor
180181
result = dt.floor(freq)
181182
assert result.value % unit == 0, "floor not a %s multiple" % (freq, )
182183
assert 0 <= dt.value - result.value < unit, "floor error"
184+
183185
# test ceil
184186
result = dt.ceil(freq)
185187
assert result.value % unit == 0, "ceil not a %s multiple" % (freq, )
186188
assert 0 <= result.value - dt.value < unit, "ceil error"
189+
187190
# test round
188191
result = dt.round(freq)
189192
assert result.value % unit == 0, "round not a %s multiple" % (freq, )

0 commit comments

Comments
 (0)