Skip to content

Commit eb72c01

Browse files
committed
TST: move custom business month tests to own file (pandas-dev#27085)
1 parent fa6b96e commit eb72c01

File tree

2 files changed

+348
-320
lines changed

2 files changed

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

0 commit comments

Comments
 (0)