Skip to content

Commit 6d05568

Browse files
committed
new test cases for GH#22591
demonstrates loss of precision and requires no UserWarning is raised for non standard frequencies
1 parent 9b9f6bd commit 6d05568

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

pandas/tests/scalar/timestamp/test_unary_ops.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas._libs.tslibs import conversion
1414
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
1515
from pandas import Timestamp, NaT
16+
from pandas.tseries.frequencies import to_offset
1617

1718

1819
class TestTimestampUnaryOps(object):
@@ -70,7 +71,7 @@ def test_round_subsecond(self):
7071
assert result == expected
7172

7273
def test_round_nonstandard_freq(self):
73-
with tm.assert_produces_warning():
74+
with tm.assert_produces_warning(False):
7475
Timestamp('2016-10-17 12:00:00.001501031').round('1010ns')
7576

7677
def test_round_invalid_arg(self):
@@ -132,6 +133,43 @@ def test_floor(self):
132133
expected = Timestamp('20130101')
133134
assert result == expected
134135

136+
@pytest.mark.parametrize('timestamp', [
137+
'2018-01-01 0:0:0.124999360',
138+
'2018-01-01 0:0:0.125000367',
139+
'2018-01-01 0:0:0.125500',
140+
'2018-01-01 0:0:0.126500',
141+
'2018-01-01 12:00:00',
142+
'2019-01-01 12:00:00',
143+
])
144+
@pytest.mark.parametrize('freq', [
145+
'2ns', '3ns', '4ns', '5ns', '6ns', '7ns',
146+
'250ns', '500ns', '750ns',
147+
'1us', '19us', '250us', '500us', '750us',
148+
'1s', '2s', '3s',
149+
'1D',
150+
])
151+
def test_round_int64(self, timestamp, freq):
152+
"""check that all rounding modes are accurate to int64 precision
153+
see GH#22591
154+
"""
155+
dt = Timestamp(timestamp)
156+
unit = to_offset(freq).nanos
157+
# test floor
158+
result = dt.floor(freq)
159+
assert result.value % unit == 0, "floor not a %s multiple" % (freq, )
160+
assert 0 <= dt.value - result.value < unit, "floor error"
161+
# test ceil
162+
result = dt.ceil(freq)
163+
assert result.value % unit == 0, "ceil not a %s multiple" % (freq, )
164+
assert 0 <= result.value - dt.value < unit, "ceil error"
165+
# test round
166+
result = dt.round(freq)
167+
assert result.value % unit == 0, "round not a %s multiple" % (freq, )
168+
assert abs(result.value - dt.value) <= unit // 2, "round error"
169+
if unit % 2 == 0 and abs(result.value - dt.value) == unit // 2:
170+
# round half to even
171+
assert result.value // unit % 2 == 0, "round half to even error"
172+
135173
# --------------------------------------------------------------
136174
# Timestamp.replace
137175

0 commit comments

Comments
 (0)