Skip to content

Commit d2f7e98

Browse files
authored
TST: move custom business month tests to own file (#27085) (#42284)
1 parent 1f135eb commit d2f7e98

File tree

2 files changed

+343
-320
lines changed

2 files changed

+343
-320
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
"""
2+
Tests for the following offsets:
3+
- CustomBusinessMonthBase
4+
- CustomBusinessMonthBegin
5+
- CustomBusinessMonthEnd
6+
"""
7+
from datetime import (
8+
date,
9+
datetime,
10+
)
11+
12+
import numpy as np
13+
import pytest
14+
15+
from pandas._libs.tslibs.offsets import (
16+
CBMonthBegin,
17+
CBMonthEnd,
18+
CDay,
19+
)
20+
21+
from pandas import (
22+
_testing as tm,
23+
date_range,
24+
)
25+
from pandas.tests.tseries.offsets.common import (
26+
Base,
27+
assert_is_on_offset,
28+
assert_offset_equal,
29+
)
30+
from pandas.tests.tseries.offsets.test_offsets import _ApplyCases
31+
32+
from pandas.tseries import offsets as offsets
33+
from pandas.tseries.holiday import USFederalHolidayCalendar
34+
35+
36+
class CustomBusinessMonthBase:
37+
def setup_method(self, method):
38+
self.d = datetime(2008, 1, 1)
39+
self.offset = self._offset()
40+
self.offset1 = self.offset
41+
self.offset2 = self._offset(2)
42+
43+
def test_eq(self):
44+
assert self.offset2 == self.offset2
45+
46+
def test_mul(self):
47+
pass
48+
49+
def test_hash(self):
50+
assert hash(self.offset2) == hash(self.offset2)
51+
52+
def test_roundtrip_pickle(self):
53+
def _check_roundtrip(obj):
54+
unpickled = tm.round_trip_pickle(obj)
55+
assert unpickled == obj
56+
57+
_check_roundtrip(self._offset())
58+
_check_roundtrip(self._offset(2))
59+
_check_roundtrip(self._offset() * 2)
60+
61+
def test_copy(self):
62+
# GH 17452
63+
off = self._offset(weekmask="Mon Wed Fri")
64+
assert off == off.copy()
65+
66+
67+
class TestCustomBusinessMonthBegin(CustomBusinessMonthBase, Base):
68+
_offset = CBMonthBegin
69+
70+
def test_different_normalize_equals(self):
71+
# GH#21404 changed __eq__ to return False when `normalize` does not match
72+
offset = self._offset()
73+
offset2 = self._offset(normalize=True)
74+
assert offset != offset2
75+
76+
def test_repr(self):
77+
assert repr(self.offset) == "<CustomBusinessMonthBegin>"
78+
assert repr(self.offset2) == "<2 * CustomBusinessMonthBegins>"
79+
80+
def test_call(self):
81+
with tm.assert_produces_warning(FutureWarning):
82+
# GH#34171 DateOffset.__call__ is deprecated
83+
assert self.offset2(self.d) == datetime(2008, 3, 3)
84+
85+
def testRollback1(self):
86+
assert CDay(10).rollback(datetime(2007, 12, 31)) == datetime(2007, 12, 31)
87+
88+
def testRollback2(self):
89+
assert CBMonthBegin(10).rollback(self.d) == datetime(2008, 1, 1)
90+
91+
def testRollforward1(self):
92+
assert CBMonthBegin(10).rollforward(self.d) == datetime(2008, 1, 1)
93+
94+
def test_roll_date_object(self):
95+
offset = CBMonthBegin()
96+
97+
dt = date(2012, 9, 15)
98+
99+
result = offset.rollback(dt)
100+
assert result == datetime(2012, 9, 3)
101+
102+
result = offset.rollforward(dt)
103+
assert result == datetime(2012, 10, 1)
104+
105+
offset = offsets.Day()
106+
result = offset.rollback(dt)
107+
assert result == datetime(2012, 9, 15)
108+
109+
result = offset.rollforward(dt)
110+
assert result == datetime(2012, 9, 15)
111+
112+
on_offset_cases = [
113+
(CBMonthBegin(), datetime(2008, 1, 1), True),
114+
(CBMonthBegin(), datetime(2008, 1, 31), False),
115+
]
116+
117+
@pytest.mark.parametrize("case", on_offset_cases)
118+
def test_is_on_offset(self, case):
119+
offset, dt, expected = case
120+
assert_is_on_offset(offset, dt, expected)
121+
122+
apply_cases: _ApplyCases = [
123+
(
124+
CBMonthBegin(),
125+
{
126+
datetime(2008, 1, 1): datetime(2008, 2, 1),
127+
datetime(2008, 2, 7): datetime(2008, 3, 3),
128+
},
129+
),
130+
(
131+
2 * CBMonthBegin(),
132+
{
133+
datetime(2008, 1, 1): datetime(2008, 3, 3),
134+
datetime(2008, 2, 7): datetime(2008, 4, 1),
135+
},
136+
),
137+
(
138+
-CBMonthBegin(),
139+
{
140+
datetime(2008, 1, 1): datetime(2007, 12, 3),
141+
datetime(2008, 2, 8): datetime(2008, 2, 1),
142+
},
143+
),
144+
(
145+
-2 * CBMonthBegin(),
146+
{
147+
datetime(2008, 1, 1): datetime(2007, 11, 1),
148+
datetime(2008, 2, 9): datetime(2008, 1, 1),
149+
},
150+
),
151+
(
152+
CBMonthBegin(0),
153+
{
154+
datetime(2008, 1, 1): datetime(2008, 1, 1),
155+
datetime(2008, 1, 7): datetime(2008, 2, 1),
156+
},
157+
),
158+
]
159+
160+
@pytest.mark.parametrize("case", apply_cases)
161+
def test_apply(self, case):
162+
offset, cases = case
163+
for base, expected in cases.items():
164+
assert_offset_equal(offset, base, expected)
165+
166+
def test_apply_large_n(self):
167+
dt = datetime(2012, 10, 23)
168+
169+
result = dt + CBMonthBegin(10)
170+
assert result == datetime(2013, 8, 1)
171+
172+
result = dt + CDay(100) - CDay(100)
173+
assert result == dt
174+
175+
off = CBMonthBegin() * 6
176+
rs = datetime(2012, 1, 1) - off
177+
xp = datetime(2011, 7, 1)
178+
assert rs == xp
179+
180+
st = datetime(2011, 12, 18)
181+
rs = st + off
182+
183+
xp = datetime(2012, 6, 1)
184+
assert rs == xp
185+
186+
def test_holidays(self):
187+
# Define a TradingDay offset
188+
holidays = ["2012-02-01", datetime(2012, 2, 2), np.datetime64("2012-03-01")]
189+
bm_offset = CBMonthBegin(holidays=holidays)
190+
dt = datetime(2012, 1, 1)
191+
192+
assert dt + bm_offset == datetime(2012, 1, 2)
193+
assert dt + 2 * bm_offset == datetime(2012, 2, 3)
194+
195+
@pytest.mark.filterwarnings("ignore:Non:pandas.errors.PerformanceWarning")
196+
def test_datetimeindex(self):
197+
hcal = USFederalHolidayCalendar()
198+
cbmb = CBMonthBegin(calendar=hcal)
199+
assert date_range(start="20120101", end="20130101", freq=cbmb).tolist()[
200+
0
201+
] == datetime(2012, 1, 3)
202+
203+
204+
class TestCustomBusinessMonthEnd(CustomBusinessMonthBase, Base):
205+
_offset = CBMonthEnd
206+
207+
def test_different_normalize_equals(self):
208+
# GH#21404 changed __eq__ to return False when `normalize` does not match
209+
offset = self._offset()
210+
offset2 = self._offset(normalize=True)
211+
assert offset != offset2
212+
213+
def test_repr(self):
214+
assert repr(self.offset) == "<CustomBusinessMonthEnd>"
215+
assert repr(self.offset2) == "<2 * CustomBusinessMonthEnds>"
216+
217+
def test_call(self):
218+
with tm.assert_produces_warning(FutureWarning):
219+
# GH#34171 DateOffset.__call__ is deprecated
220+
assert self.offset2(self.d) == datetime(2008, 2, 29)
221+
222+
def testRollback1(self):
223+
assert CDay(10).rollback(datetime(2007, 12, 31)) == datetime(2007, 12, 31)
224+
225+
def testRollback2(self):
226+
assert CBMonthEnd(10).rollback(self.d) == datetime(2007, 12, 31)
227+
228+
def testRollforward1(self):
229+
assert CBMonthEnd(10).rollforward(self.d) == datetime(2008, 1, 31)
230+
231+
def test_roll_date_object(self):
232+
offset = CBMonthEnd()
233+
234+
dt = date(2012, 9, 15)
235+
236+
result = offset.rollback(dt)
237+
assert result == datetime(2012, 8, 31)
238+
239+
result = offset.rollforward(dt)
240+
assert result == datetime(2012, 9, 28)
241+
242+
offset = offsets.Day()
243+
result = offset.rollback(dt)
244+
assert result == datetime(2012, 9, 15)
245+
246+
result = offset.rollforward(dt)
247+
assert result == datetime(2012, 9, 15)
248+
249+
on_offset_cases = [
250+
(CBMonthEnd(), datetime(2008, 1, 31), True),
251+
(CBMonthEnd(), datetime(2008, 1, 1), False),
252+
]
253+
254+
@pytest.mark.parametrize("case", on_offset_cases)
255+
def test_is_on_offset(self, case):
256+
offset, d, expected = case
257+
assert_is_on_offset(offset, d, expected)
258+
259+
apply_cases: _ApplyCases = [
260+
(
261+
CBMonthEnd(),
262+
{
263+
datetime(2008, 1, 1): datetime(2008, 1, 31),
264+
datetime(2008, 2, 7): datetime(2008, 2, 29),
265+
},
266+
),
267+
(
268+
2 * CBMonthEnd(),
269+
{
270+
datetime(2008, 1, 1): datetime(2008, 2, 29),
271+
datetime(2008, 2, 7): datetime(2008, 3, 31),
272+
},
273+
),
274+
(
275+
-CBMonthEnd(),
276+
{
277+
datetime(2008, 1, 1): datetime(2007, 12, 31),
278+
datetime(2008, 2, 8): datetime(2008, 1, 31),
279+
},
280+
),
281+
(
282+
-2 * CBMonthEnd(),
283+
{
284+
datetime(2008, 1, 1): datetime(2007, 11, 30),
285+
datetime(2008, 2, 9): datetime(2007, 12, 31),
286+
},
287+
),
288+
(
289+
CBMonthEnd(0),
290+
{
291+
datetime(2008, 1, 1): datetime(2008, 1, 31),
292+
datetime(2008, 2, 7): datetime(2008, 2, 29),
293+
},
294+
),
295+
]
296+
297+
@pytest.mark.parametrize("case", apply_cases)
298+
def test_apply(self, case):
299+
offset, cases = case
300+
for base, expected in cases.items():
301+
assert_offset_equal(offset, base, expected)
302+
303+
def test_apply_large_n(self):
304+
dt = datetime(2012, 10, 23)
305+
306+
result = dt + CBMonthEnd(10)
307+
assert result == datetime(2013, 7, 31)
308+
309+
result = dt + CDay(100) - CDay(100)
310+
assert result == dt
311+
312+
off = CBMonthEnd() * 6
313+
rs = datetime(2012, 1, 1) - off
314+
xp = datetime(2011, 7, 29)
315+
assert rs == xp
316+
317+
st = datetime(2011, 12, 18)
318+
rs = st + off
319+
xp = datetime(2012, 5, 31)
320+
assert rs == xp
321+
322+
def test_holidays(self):
323+
# Define a TradingDay offset
324+
holidays = ["2012-01-31", datetime(2012, 2, 28), np.datetime64("2012-02-29")]
325+
bm_offset = CBMonthEnd(holidays=holidays)
326+
dt = datetime(2012, 1, 1)
327+
assert dt + bm_offset == datetime(2012, 1, 30)
328+
assert dt + 2 * bm_offset == datetime(2012, 2, 27)
329+
330+
@pytest.mark.filterwarnings("ignore:Non:pandas.errors.PerformanceWarning")
331+
def test_datetimeindex(self):
332+
from pandas.tseries.holiday import USFederalHolidayCalendar
333+
334+
hcal = USFederalHolidayCalendar()
335+
freq = CBMonthEnd(calendar=hcal)
336+
337+
assert date_range(start="20120101", end="20130101", freq=freq).tolist()[
338+
0
339+
] == datetime(2012, 1, 31)

0 commit comments

Comments
 (0)