Skip to content

Commit 7bed86a

Browse files
TomAugspurgergfyoung
authored andcommitted
TST: Replace yield-based tests in test_query_eval
1 parent 0fb4854 commit 7bed86a

File tree

1 file changed

+28
-78
lines changed

1 file changed

+28
-78
lines changed

pandas/tests/frame/test_query_eval.py

+28-78
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import operator
66
import pytest
7-
from itertools import product
87

98
from pandas.compat import (zip, range, lrange, StringIO)
109
from pandas import DataFrame, Series, Index, MultiIndex, date_range
@@ -27,6 +26,16 @@
2726
ENGINES = 'python', 'numexpr'
2827

2928

29+
@pytest.fixture(params=PARSERS, ids=lambda x: x)
30+
def parser(request):
31+
return request.param
32+
33+
34+
@pytest.fixture(params=ENGINES, ids=lambda x: x)
35+
def engine(request):
36+
return request.param
37+
38+
3039
def skip_if_no_pandas_parser(parser):
3140
if parser != 'pandas':
3241
pytest.skip("cannot evaluate with parser {0!r}".format(parser))
@@ -165,8 +174,9 @@ def test_eval_resolvers_as_list(self):
165174

166175
class TestDataFrameQueryWithMultiIndex(tm.TestCase):
167176

168-
def check_query_with_named_multiindex(self, parser, engine):
177+
def test_query_with_named_multiindex(self, parser, engine):
169178
tm.skip_if_no_ne(engine)
179+
skip_if_no_pandas_parser(parser)
170180
a = np.random.choice(['red', 'green'], size=10)
171181
b = np.random.choice(['eggs', 'ham'], size=10)
172182
index = MultiIndex.from_arrays([a, b], names=['color', 'food'])
@@ -214,12 +224,9 @@ def check_query_with_named_multiindex(self, parser, engine):
214224
assert_frame_equal(res1, exp)
215225
assert_frame_equal(res2, exp)
216226

217-
def test_query_with_named_multiindex(self):
218-
for parser, engine in product(['pandas'], ENGINES):
219-
yield self.check_query_with_named_multiindex, parser, engine
220-
221-
def check_query_with_unnamed_multiindex(self, parser, engine):
227+
def test_query_with_unnamed_multiindex(self, parser, engine):
222228
tm.skip_if_no_ne(engine)
229+
skip_if_no_pandas_parser(parser)
223230
a = np.random.choice(['red', 'green'], size=10)
224231
b = np.random.choice(['eggs', 'ham'], size=10)
225232
index = MultiIndex.from_arrays([a, b])
@@ -308,12 +315,9 @@ def check_query_with_unnamed_multiindex(self, parser, engine):
308315
assert_frame_equal(res1, exp)
309316
assert_frame_equal(res2, exp)
310317

311-
def test_query_with_unnamed_multiindex(self):
312-
for parser, engine in product(['pandas'], ENGINES):
313-
yield self.check_query_with_unnamed_multiindex, parser, engine
314-
315-
def check_query_with_partially_named_multiindex(self, parser, engine):
318+
def test_query_with_partially_named_multiindex(self, parser, engine):
316319
tm.skip_if_no_ne(engine)
320+
skip_if_no_pandas_parser(parser)
317321
a = np.random.choice(['red', 'green'], size=10)
318322
b = np.arange(10)
319323
index = MultiIndex.from_arrays([a, b])
@@ -341,17 +345,7 @@ def check_query_with_partially_named_multiindex(self, parser, engine):
341345
exp = df[ind != "red"]
342346
assert_frame_equal(res, exp)
343347

344-
def test_query_with_partially_named_multiindex(self):
345-
for parser, engine in product(['pandas'], ENGINES):
346-
yield (self.check_query_with_partially_named_multiindex,
347-
parser, engine)
348-
349348
def test_query_multiindex_get_index_resolvers(self):
350-
for parser, engine in product(['pandas'], ENGINES):
351-
yield (self.check_query_multiindex_get_index_resolvers, parser,
352-
engine)
353-
354-
def check_query_multiindex_get_index_resolvers(self, parser, engine):
355349
df = mkdf(10, 3, r_idx_nlevels=2, r_idx_names=['spam', 'eggs'])
356350
resolvers = df._get_index_resolvers()
357351

@@ -375,22 +369,14 @@ def to_series(mi, level):
375369
else:
376370
raise AssertionError("object must be a Series or Index")
377371

378-
def test_raise_on_panel_with_multiindex(self):
379-
for parser, engine in product(PARSERS, ENGINES):
380-
yield self.check_raise_on_panel_with_multiindex, parser, engine
381-
382-
def check_raise_on_panel_with_multiindex(self, parser, engine):
372+
def test_raise_on_panel_with_multiindex(self, parser, engine):
383373
tm.skip_if_no_ne()
384374
p = tm.makePanel(7)
385375
p.items = tm.makeCustomIndex(len(p.items), nlevels=2)
386376
with pytest.raises(NotImplementedError):
387377
pd.eval('p + 1', parser=parser, engine=engine)
388378

389-
def test_raise_on_panel4d_with_multiindex(self):
390-
for parser, engine in product(PARSERS, ENGINES):
391-
yield self.check_raise_on_panel4d_with_multiindex, parser, engine
392-
393-
def check_raise_on_panel4d_with_multiindex(self, parser, engine):
379+
def test_raise_on_panel4d_with_multiindex(self, parser, engine):
394380
tm.skip_if_no_ne()
395381
p4d = tm.makePanel4D(7)
396382
p4d.items = tm.makeCustomIndex(len(p4d.items), nlevels=2)
@@ -874,7 +860,7 @@ def test_query_builtin(self):
874860

875861
class TestDataFrameQueryStrings(tm.TestCase):
876862

877-
def check_str_query_method(self, parser, engine):
863+
def test_str_query_method(self, parser, engine):
878864
tm.skip_if_no_ne(engine)
879865
df = DataFrame(randn(10, 1), columns=['b'])
880866
df['strings'] = Series(list('aabbccddee'))
@@ -911,15 +897,7 @@ def check_str_query_method(self, parser, engine):
911897
assert_frame_equal(res, expect)
912898
assert_frame_equal(res, df[~df.strings.isin(['a'])])
913899

914-
def test_str_query_method(self):
915-
for parser, engine in product(PARSERS, ENGINES):
916-
yield self.check_str_query_method, parser, engine
917-
918-
def test_str_list_query_method(self):
919-
for parser, engine in product(PARSERS, ENGINES):
920-
yield self.check_str_list_query_method, parser, engine
921-
922-
def check_str_list_query_method(self, parser, engine):
900+
def test_str_list_query_method(self, parser, engine):
923901
tm.skip_if_no_ne(engine)
924902
df = DataFrame(randn(10, 1), columns=['b'])
925903
df['strings'] = Series(list('aabbccddee'))
@@ -958,7 +936,7 @@ def check_str_list_query_method(self, parser, engine):
958936
parser=parser)
959937
assert_frame_equal(res, expect)
960938

961-
def check_query_with_string_columns(self, parser, engine):
939+
def test_query_with_string_columns(self, parser, engine):
962940
tm.skip_if_no_ne(engine)
963941
df = DataFrame({'a': list('aaaabbbbcccc'),
964942
'b': list('aabbccddeeff'),
@@ -979,11 +957,7 @@ def check_query_with_string_columns(self, parser, engine):
979957
with pytest.raises(NotImplementedError):
980958
df.query('a in b and c < d', parser=parser, engine=engine)
981959

982-
def test_query_with_string_columns(self):
983-
for parser, engine in product(PARSERS, ENGINES):
984-
yield self.check_query_with_string_columns, parser, engine
985-
986-
def check_object_array_eq_ne(self, parser, engine):
960+
def test_object_array_eq_ne(self, parser, engine):
987961
tm.skip_if_no_ne(engine)
988962
df = DataFrame({'a': list('aaaabbbbcccc'),
989963
'b': list('aabbccddeeff'),
@@ -997,11 +971,7 @@ def check_object_array_eq_ne(self, parser, engine):
997971
exp = df[df.a != df.b]
998972
assert_frame_equal(res, exp)
999973

1000-
def test_object_array_eq_ne(self):
1001-
for parser, engine in product(PARSERS, ENGINES):
1002-
yield self.check_object_array_eq_ne, parser, engine
1003-
1004-
def check_query_with_nested_strings(self, parser, engine):
974+
def test_query_with_nested_strings(self, parser, engine):
1005975
tm.skip_if_no_ne(engine)
1006976
skip_if_no_pandas_parser(parser)
1007977
raw = """id event timestamp
@@ -1025,11 +995,7 @@ def check_query_with_nested_strings(self, parser, engine):
1025995
engine=engine)
1026996
assert_frame_equal(expected, res)
1027997

1028-
def test_query_with_nested_string(self):
1029-
for parser, engine in product(PARSERS, ENGINES):
1030-
yield self.check_query_with_nested_strings, parser, engine
1031-
1032-
def check_query_with_nested_special_character(self, parser, engine):
998+
def test_query_with_nested_special_character(self, parser, engine):
1033999
skip_if_no_pandas_parser(parser)
10341000
tm.skip_if_no_ne(engine)
10351001
df = DataFrame({'a': ['a', 'b', 'test & test'],
@@ -1038,12 +1004,7 @@ def check_query_with_nested_special_character(self, parser, engine):
10381004
expec = df[df.a == 'test & test']
10391005
assert_frame_equal(res, expec)
10401006

1041-
def test_query_with_nested_special_character(self):
1042-
for parser, engine in product(PARSERS, ENGINES):
1043-
yield (self.check_query_with_nested_special_character,
1044-
parser, engine)
1045-
1046-
def check_query_lex_compare_strings(self, parser, engine):
1007+
def test_query_lex_compare_strings(self, parser, engine):
10471008
tm.skip_if_no_ne(engine=engine)
10481009
import operator as opr
10491010

@@ -1058,11 +1019,7 @@ def check_query_lex_compare_strings(self, parser, engine):
10581019
expected = df[func(df.X, 'd')]
10591020
assert_frame_equal(res, expected)
10601021

1061-
def test_query_lex_compare_strings(self):
1062-
for parser, engine in product(PARSERS, ENGINES):
1063-
yield self.check_query_lex_compare_strings, parser, engine
1064-
1065-
def check_query_single_element_booleans(self, parser, engine):
1022+
def test_query_single_element_booleans(self, parser, engine):
10661023
tm.skip_if_no_ne(engine)
10671024
columns = 'bid', 'bidsize', 'ask', 'asksize'
10681025
data = np.random.randint(2, size=(1, len(columns))).astype(bool)
@@ -1071,23 +1028,16 @@ def check_query_single_element_booleans(self, parser, engine):
10711028
expected = df[df.bid & df.ask]
10721029
assert_frame_equal(res, expected)
10731030

1074-
def test_query_single_element_booleans(self):
1075-
for parser, engine in product(PARSERS, ENGINES):
1076-
yield self.check_query_single_element_booleans, parser, engine
1077-
1078-
def check_query_string_scalar_variable(self, parser, engine):
1031+
def test_query_string_scalar_variable(self, parser, engine):
10791032
tm.skip_if_no_ne(engine)
1033+
skip_if_no_pandas_parser(parser)
10801034
df = pd.DataFrame({'Symbol': ['BUD US', 'BUD US', 'IBM US', 'IBM US'],
10811035
'Price': [109.70, 109.72, 183.30, 183.35]})
10821036
e = df[df.Symbol == 'BUD US']
10831037
symb = 'BUD US' # noqa
10841038
r = df.query('Symbol == @symb', parser=parser, engine=engine)
10851039
assert_frame_equal(e, r)
10861040

1087-
def test_query_string_scalar_variable(self):
1088-
for parser, engine in product(['pandas'], ENGINES):
1089-
yield self.check_query_string_scalar_variable, parser, engine
1090-
10911041

10921042
class TestDataFrameEvalNumExprPandas(tm.TestCase):
10931043

0 commit comments

Comments
 (0)