Skip to content

Commit df778fd

Browse files
authored
CLN: Create tests/window/moments/conftest for specific fixtures (#41430)
1 parent a47387c commit df778fd

File tree

3 files changed

+186
-181
lines changed

3 files changed

+186
-181
lines changed

pandas/tests/window/conftest.py

Lines changed: 1 addition & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
from datetime import (
2-
datetime,
3-
timedelta,
4-
)
1+
from datetime import timedelta
52

6-
import numpy as np
73
import pytest
84

95
import pandas.util._test_decorators as td
106

117
from pandas import (
128
DataFrame,
13-
Series,
14-
bdate_range,
15-
notna,
169
to_datetime,
1710
)
1811

@@ -141,168 +134,6 @@ def engine_and_raw(request):
141134
return request.param
142135

143136

144-
# create the data only once as we are not setting it
145-
def _create_consistency_data():
146-
def create_series():
147-
return [
148-
Series(dtype=object),
149-
Series([np.nan]),
150-
Series([np.nan, np.nan]),
151-
Series([3.0]),
152-
Series([np.nan, 3.0]),
153-
Series([3.0, np.nan]),
154-
Series([1.0, 3.0]),
155-
Series([2.0, 2.0]),
156-
Series([3.0, 1.0]),
157-
Series(
158-
[5.0, 5.0, 5.0, 5.0, np.nan, np.nan, np.nan, 5.0, 5.0, np.nan, np.nan]
159-
),
160-
Series(
161-
[
162-
np.nan,
163-
5.0,
164-
5.0,
165-
5.0,
166-
np.nan,
167-
np.nan,
168-
np.nan,
169-
5.0,
170-
5.0,
171-
np.nan,
172-
np.nan,
173-
]
174-
),
175-
Series(
176-
[
177-
np.nan,
178-
np.nan,
179-
5.0,
180-
5.0,
181-
np.nan,
182-
np.nan,
183-
np.nan,
184-
5.0,
185-
5.0,
186-
np.nan,
187-
np.nan,
188-
]
189-
),
190-
Series(
191-
[
192-
np.nan,
193-
3.0,
194-
np.nan,
195-
3.0,
196-
4.0,
197-
5.0,
198-
6.0,
199-
np.nan,
200-
np.nan,
201-
7.0,
202-
12.0,
203-
13.0,
204-
14.0,
205-
15.0,
206-
]
207-
),
208-
Series(
209-
[
210-
np.nan,
211-
5.0,
212-
np.nan,
213-
2.0,
214-
4.0,
215-
0.0,
216-
9.0,
217-
np.nan,
218-
np.nan,
219-
3.0,
220-
12.0,
221-
13.0,
222-
14.0,
223-
15.0,
224-
]
225-
),
226-
Series(
227-
[
228-
2.0,
229-
3.0,
230-
np.nan,
231-
3.0,
232-
4.0,
233-
5.0,
234-
6.0,
235-
np.nan,
236-
np.nan,
237-
7.0,
238-
12.0,
239-
13.0,
240-
14.0,
241-
15.0,
242-
]
243-
),
244-
Series(
245-
[
246-
2.0,
247-
5.0,
248-
np.nan,
249-
2.0,
250-
4.0,
251-
0.0,
252-
9.0,
253-
np.nan,
254-
np.nan,
255-
3.0,
256-
12.0,
257-
13.0,
258-
14.0,
259-
15.0,
260-
]
261-
),
262-
Series(range(10)),
263-
Series(range(20, 0, -2)),
264-
]
265-
266-
def create_dataframes():
267-
return [
268-
DataFrame(),
269-
DataFrame(columns=["a"]),
270-
DataFrame(columns=["a", "a"]),
271-
DataFrame(columns=["a", "b"]),
272-
DataFrame(np.arange(10).reshape((5, 2))),
273-
DataFrame(np.arange(25).reshape((5, 5))),
274-
DataFrame(np.arange(25).reshape((5, 5)), columns=["a", "b", 99, "d", "d"]),
275-
] + [DataFrame(s) for s in create_series()]
276-
277-
def is_constant(x):
278-
values = x.values.ravel("K")
279-
return len(set(values[notna(values)])) == 1
280-
281-
def no_nans(x):
282-
return x.notna().all().all()
283-
284-
# data is a tuple(object, is_constant, no_nans)
285-
data = create_series() + create_dataframes()
286-
287-
return [(x, is_constant(x), no_nans(x)) for x in data]
288-
289-
290-
@pytest.fixture(params=_create_consistency_data())
291-
def consistency_data(request):
292-
"""Create consistency data"""
293-
return request.param
294-
295-
296-
@pytest.fixture
297-
def frame():
298-
"""Make mocked frame as fixture."""
299-
return DataFrame(
300-
np.random.randn(100, 10),
301-
index=bdate_range(datetime(2009, 1, 1), periods=100),
302-
columns=np.arange(10),
303-
)
304-
305-
306137
@pytest.fixture
307138
def times_frame():
308139
"""Frame for testing times argument in EWM groupby."""
@@ -328,16 +159,6 @@ def times_frame():
328159
)
329160

330161

331-
@pytest.fixture
332-
def series():
333-
"""Make mocked series as fixture."""
334-
arr = np.random.randn(100)
335-
locs = np.arange(20, 40)
336-
arr[locs] = np.NaN
337-
series = Series(arr, index=bdate_range(datetime(2009, 1, 1), periods=100))
338-
return series
339-
340-
341162
@pytest.fixture(params=["1 day", timedelta(days=1)])
342163
def halflife_with_times(request):
343164
"""Halflife argument for EWM when times is specified."""
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
from datetime import datetime
2+
3+
import numpy as np
4+
import pytest
5+
6+
from pandas import (
7+
DataFrame,
8+
Series,
9+
bdate_range,
10+
notna,
11+
)
12+
13+
14+
@pytest.fixture
15+
def series():
16+
"""Make mocked series as fixture."""
17+
arr = np.random.randn(100)
18+
locs = np.arange(20, 40)
19+
arr[locs] = np.NaN
20+
series = Series(arr, index=bdate_range(datetime(2009, 1, 1), periods=100))
21+
return series
22+
23+
24+
@pytest.fixture
25+
def frame():
26+
"""Make mocked frame as fixture."""
27+
return DataFrame(
28+
np.random.randn(100, 10),
29+
index=bdate_range(datetime(2009, 1, 1), periods=100),
30+
columns=np.arange(10),
31+
)
32+
33+
34+
# create the data only once as we are not setting it
35+
def _create_consistency_data():
36+
def create_series():
37+
return [
38+
Series(dtype=object),
39+
Series([np.nan]),
40+
Series([np.nan, np.nan]),
41+
Series([3.0]),
42+
Series([np.nan, 3.0]),
43+
Series([3.0, np.nan]),
44+
Series([1.0, 3.0]),
45+
Series([2.0, 2.0]),
46+
Series([3.0, 1.0]),
47+
Series(
48+
[5.0, 5.0, 5.0, 5.0, np.nan, np.nan, np.nan, 5.0, 5.0, np.nan, np.nan]
49+
),
50+
Series(
51+
[
52+
np.nan,
53+
5.0,
54+
5.0,
55+
5.0,
56+
np.nan,
57+
np.nan,
58+
np.nan,
59+
5.0,
60+
5.0,
61+
np.nan,
62+
np.nan,
63+
]
64+
),
65+
Series(
66+
[
67+
np.nan,
68+
np.nan,
69+
5.0,
70+
5.0,
71+
np.nan,
72+
np.nan,
73+
np.nan,
74+
5.0,
75+
5.0,
76+
np.nan,
77+
np.nan,
78+
]
79+
),
80+
Series(
81+
[
82+
np.nan,
83+
3.0,
84+
np.nan,
85+
3.0,
86+
4.0,
87+
5.0,
88+
6.0,
89+
np.nan,
90+
np.nan,
91+
7.0,
92+
12.0,
93+
13.0,
94+
14.0,
95+
15.0,
96+
]
97+
),
98+
Series(
99+
[
100+
np.nan,
101+
5.0,
102+
np.nan,
103+
2.0,
104+
4.0,
105+
0.0,
106+
9.0,
107+
np.nan,
108+
np.nan,
109+
3.0,
110+
12.0,
111+
13.0,
112+
14.0,
113+
15.0,
114+
]
115+
),
116+
Series(
117+
[
118+
2.0,
119+
3.0,
120+
np.nan,
121+
3.0,
122+
4.0,
123+
5.0,
124+
6.0,
125+
np.nan,
126+
np.nan,
127+
7.0,
128+
12.0,
129+
13.0,
130+
14.0,
131+
15.0,
132+
]
133+
),
134+
Series(
135+
[
136+
2.0,
137+
5.0,
138+
np.nan,
139+
2.0,
140+
4.0,
141+
0.0,
142+
9.0,
143+
np.nan,
144+
np.nan,
145+
3.0,
146+
12.0,
147+
13.0,
148+
14.0,
149+
15.0,
150+
]
151+
),
152+
Series(range(10)),
153+
Series(range(20, 0, -2)),
154+
]
155+
156+
def create_dataframes():
157+
return [
158+
DataFrame(),
159+
DataFrame(columns=["a"]),
160+
DataFrame(columns=["a", "a"]),
161+
DataFrame(columns=["a", "b"]),
162+
DataFrame(np.arange(10).reshape((5, 2))),
163+
DataFrame(np.arange(25).reshape((5, 5))),
164+
DataFrame(np.arange(25).reshape((5, 5)), columns=["a", "b", 99, "d", "d"]),
165+
] + [DataFrame(s) for s in create_series()]
166+
167+
def is_constant(x):
168+
values = x.values.ravel("K")
169+
return len(set(values[notna(values)])) == 1
170+
171+
def no_nans(x):
172+
return x.notna().all().all()
173+
174+
# data is a tuple(object, is_constant, no_nans)
175+
data = create_series() + create_dataframes()
176+
177+
return [(x, is_constant(x), no_nans(x)) for x in data]
178+
179+
180+
@pytest.fixture(params=_create_consistency_data())
181+
def consistency_data(request):
182+
"""Create consistency data"""
183+
return request.param

pandas/tests/window/test_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
from pandas.core.base import SpecificationError
1717

1818

19-
def test_getitem(frame):
19+
def test_getitem():
20+
frame = DataFrame(np.random.randn(5, 5))
2021
r = frame.rolling(window=5)
2122
tm.assert_index_equal(r._selected_obj.columns, frame.columns)
2223

0 commit comments

Comments
 (0)