|
13 | 13 | from pandas._libs.tslibs import conversion
|
14 | 14 | from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
|
15 | 15 | from pandas import Timestamp, NaT
|
| 16 | +from pandas.tseries.frequencies import to_offset |
16 | 17 |
|
17 | 18 |
|
18 | 19 | class TestTimestampUnaryOps(object):
|
@@ -70,7 +71,7 @@ def test_round_subsecond(self):
|
70 | 71 | assert result == expected
|
71 | 72 |
|
72 | 73 | def test_round_nonstandard_freq(self):
|
73 |
| - with tm.assert_produces_warning(): |
| 74 | + with tm.assert_produces_warning(False): |
74 | 75 | Timestamp('2016-10-17 12:00:00.001501031').round('1010ns')
|
75 | 76 |
|
76 | 77 | def test_round_invalid_arg(self):
|
@@ -132,6 +133,43 @@ def test_floor(self):
|
132 | 133 | expected = Timestamp('20130101')
|
133 | 134 | assert result == expected
|
134 | 135 |
|
| 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 | + |
135 | 173 | # --------------------------------------------------------------
|
136 | 174 | # Timestamp.replace
|
137 | 175 |
|
|
0 commit comments