|
9 | 9 | from pandas import Timestamp
|
10 | 10 |
|
11 | 11 | import pandas._libs.tslibs.offsets as liboffsets
|
| 12 | +from pandas._libs.tslibs.offsets import roll_qtrday |
12 | 13 |
|
13 | 14 |
|
14 | 15 | def test_get_lastbday():
|
@@ -95,3 +96,93 @@ def test_roll_yearday():
|
95 | 96 | assert liboffsets.roll_yearday(other, 5, month, day_opt) == 5
|
96 | 97 | assert liboffsets.roll_yearday(other, -7, month, day_opt) == -6
|
97 | 98 | assert liboffsets.roll_yearday(other, 0, month, day_opt) == 1
|
| 99 | + |
| 100 | + |
| 101 | +def test_roll_qtrday(): |
| 102 | + other = Timestamp(2072, 10, 1, 6, 17, 18) # Saturday |
| 103 | + for day_opt in ['start', 'end', 'business_start', 'business_end']: |
| 104 | + # as long as (other.month % 3) != (month % 3), day_opt is irrelevant |
| 105 | + # the `day_opt` doesn't matter. |
| 106 | + month = 5 # (other.month % 3) < (month % 3) |
| 107 | + assert roll_qtrday(other, 4, month, day_opt, modby=3) == 3 |
| 108 | + assert roll_qtrday(other, -3, month, day_opt, modby=3) == -3 |
| 109 | + |
| 110 | + month = 3 # (other.month % 3) > (month % 3) |
| 111 | + assert roll_qtrday(other, 4, month, day_opt, modby=3) == 4 |
| 112 | + assert roll_qtrday(other, -3, month, day_opt, modby=3) == -2 |
| 113 | + |
| 114 | + month = 2 |
| 115 | + other = datetime(1999, 5, 31) # Monday |
| 116 | + # has (other.month % 3) == (month % 3) |
| 117 | + |
| 118 | + n = 2 |
| 119 | + assert roll_qtrday(other, n, month, 'start', modby=3) == n |
| 120 | + assert roll_qtrday(other, n, month, 'end', modby=3) == n |
| 121 | + assert roll_qtrday(other, n, month, 'business_start', modby=3) == n |
| 122 | + assert roll_qtrday(other, n, month, 'business_end', modby=3) == n |
| 123 | + |
| 124 | + n = -1 |
| 125 | + assert roll_qtrday(other, n, month, 'start', modby=3) == n + 1 |
| 126 | + assert roll_qtrday(other, n, month, 'end', modby=3) == n |
| 127 | + assert roll_qtrday(other, n, month, 'business_start', modby=3) == n + 1 |
| 128 | + assert roll_qtrday(other, n, month, 'business_end', modby=3) == n |
| 129 | + |
| 130 | + other = Timestamp(2072, 10, 1, 6, 17, 18) # Saturday |
| 131 | + month = 4 # (other.month % 3) == (month % 3) |
| 132 | + n = 2 |
| 133 | + assert roll_qtrday(other, n, month, 'start', modby=3) == n |
| 134 | + assert roll_qtrday(other, n, month, 'end', modby=3) == n - 1 |
| 135 | + assert roll_qtrday(other, n, month, 'business_start', modby=3) == n - 1 |
| 136 | + assert roll_qtrday(other, n, month, 'business_end', modby=3) == n - 1 |
| 137 | + |
| 138 | + n = -1 |
| 139 | + assert roll_qtrday(other, n, month, 'start', modby=3) == n |
| 140 | + assert roll_qtrday(other, n, month, 'end', modby=3) == n |
| 141 | + assert roll_qtrday(other, n, month, 'business_start', modby=3) == n |
| 142 | + assert roll_qtrday(other, n, month, 'business_end', modby=3) == n |
| 143 | + |
| 144 | + other = Timestamp(2072, 10, 3, 6, 17, 18) # First businessday |
| 145 | + month = 4 # (other.month % 3) == (month % 3) |
| 146 | + n = 2 |
| 147 | + assert roll_qtrday(other, n, month, 'start', modby=3) == n |
| 148 | + assert roll_qtrday(other, n, month, 'end', modby=3) == n - 1 |
| 149 | + assert roll_qtrday(other, n, month, 'business_start', modby=3) == n |
| 150 | + assert roll_qtrday(other, n, month, 'business_end', modby=3) == n - 1 |
| 151 | + |
| 152 | + n = -1 |
| 153 | + assert roll_qtrday(other, n, month, 'start', modby=3) == n + 1 |
| 154 | + assert roll_qtrday(other, n, month, 'end', modby=3) == n |
| 155 | + assert roll_qtrday(other, n, month, 'business_start', modby=3) == n |
| 156 | + assert roll_qtrday(other, n, month, 'business_end', modby=3) == n |
| 157 | + |
| 158 | + |
| 159 | +def test_roll_monthday(): |
| 160 | + other = Timestamp('2017-12-29', tz='US/Pacific') |
| 161 | + before = Timestamp('2017-12-01', tz='US/Pacific') |
| 162 | + after = Timestamp('2017-12-31', tz='US/Pacific') |
| 163 | + |
| 164 | + n = 42 |
| 165 | + assert liboffsets.roll_monthday(other, n, other) == n |
| 166 | + assert liboffsets.roll_monthday(other, n, before) == n |
| 167 | + assert liboffsets.roll_monthday(other, n, after) == n - 1 |
| 168 | + |
| 169 | + n = -4 |
| 170 | + assert liboffsets.roll_monthday(other, n, other) == n |
| 171 | + assert liboffsets.roll_monthday(other, n, before) == n + 1 |
| 172 | + assert liboffsets.roll_monthday(other, n, after) == n |
| 173 | + |
| 174 | + |
| 175 | +def test_roll_convention(): |
| 176 | + other = 29 |
| 177 | + before = 1 |
| 178 | + after = 31 |
| 179 | + |
| 180 | + n = 42 |
| 181 | + assert liboffsets.roll_convention(other, n, other) == n |
| 182 | + assert liboffsets.roll_convention(other, n, before) == n |
| 183 | + assert liboffsets.roll_convention(other, n, after) == n - 1 |
| 184 | + |
| 185 | + n = -4 |
| 186 | + assert liboffsets.roll_convention(other, n, other) == n |
| 187 | + assert liboffsets.roll_convention(other, n, before) == n + 1 |
| 188 | + assert liboffsets.roll_convention(other, n, after) == n |
0 commit comments