@@ -71,9 +71,14 @@ def test_query_numexpr(self):
71
71
result = df .eval ("A+1" , engine = "numexpr" )
72
72
tm .assert_series_equal (result , self .expected2 , check_names = False )
73
73
else :
74
- with pytest .raises (ImportError ):
74
+ msg = (
75
+ r"'numexpr' is not installed or an unsupported version. "
76
+ r"Cannot use engine='numexpr' for query/eval if 'numexpr' is "
77
+ r"not installed"
78
+ )
79
+ with pytest .raises (ImportError , match = msg ):
75
80
df .query ("A>0" , engine = "numexpr" )
76
- with pytest .raises (ImportError ):
81
+ with pytest .raises (ImportError , match = msg ):
77
82
df .eval ("A+1" , engine = "numexpr" )
78
83
79
84
@@ -452,14 +457,16 @@ def test_date_query_with_non_date(self):
452
457
result = df .query ("dates != nondate" , parser = parser , engine = engine )
453
458
tm .assert_frame_equal (result , df )
454
459
460
+ msg = r"Invalid comparison between dtype=datetime64\[ns\] and ndarray"
455
461
for op in ["<" , ">" , "<=" , ">=" ]:
456
- with pytest .raises (TypeError ):
462
+ with pytest .raises (TypeError , match = msg ):
457
463
df .query (f"dates { op } nondate" , parser = parser , engine = engine )
458
464
459
465
def test_query_syntax_error (self ):
460
466
engine , parser = self .engine , self .parser
461
467
df = DataFrame ({"i" : range (10 ), "+" : range (3 , 13 ), "r" : range (4 , 14 )})
462
- with pytest .raises (SyntaxError ):
468
+ msg = "invalid syntax"
469
+ with pytest .raises (SyntaxError , match = msg ):
463
470
df .query ("i - +" , engine = engine , parser = parser )
464
471
465
472
def test_query_scope (self ):
@@ -781,7 +788,8 @@ def test_date_index_query_with_NaT_duplicates(self):
781
788
df ["dates3" ] = date_range ("1/1/2014" , periods = n )
782
789
df .loc [np .random .rand (n ) > 0.5 , "dates1" ] = pd .NaT
783
790
df .set_index ("dates1" , inplace = True , drop = True )
784
- with pytest .raises (NotImplementedError ):
791
+ msg = r"'BoolOp' nodes are not implemented"
792
+ with pytest .raises (NotImplementedError , match = msg ):
785
793
df .query ("index < 20130101 < dates3" , engine = engine , parser = parser )
786
794
787
795
def test_nested_scope (self ):
@@ -798,7 +806,8 @@ def test_nested_scope(self):
798
806
df2 = DataFrame (np .random .randn (5 , 3 ))
799
807
800
808
# don't have the pandas parser
801
- with pytest .raises (SyntaxError ):
809
+ msg = r"The '@' prefix is only supported by the pandas parser"
810
+ with pytest .raises (SyntaxError , match = msg ):
802
811
df .query ("(@df>0) & (@df2>0)" , engine = engine , parser = parser )
803
812
804
813
with pytest .raises (UndefinedVariableError , match = "name 'df' is not defined" ):
@@ -867,10 +876,10 @@ def test_str_query_method(self, parser, engine):
867
876
868
877
eq , ne = "==" , "!="
869
878
ops = 2 * ([eq ] + [ne ])
879
+ msg = r"'(Not)?In' nodes are not implemented"
870
880
871
881
for lhs , op , rhs in zip (lhs , ops , rhs ):
872
882
ex = f"{ lhs } { op } { rhs } "
873
- msg = r"'(Not)?In' nodes are not implemented"
874
883
with pytest .raises (NotImplementedError , match = msg ):
875
884
df .query (
876
885
ex ,
@@ -908,10 +917,11 @@ def test_str_list_query_method(self, parser, engine):
908
917
909
918
eq , ne = "==" , "!="
910
919
ops = 2 * ([eq ] + [ne ])
920
+ msg = r"'(Not)?In' nodes are not implemented"
911
921
912
922
for lhs , op , rhs in zip (lhs , ops , rhs ):
913
923
ex = f"{ lhs } { op } { rhs } "
914
- with pytest .raises (NotImplementedError ):
924
+ with pytest .raises (NotImplementedError , match = msg ):
915
925
df .query (ex , engine = engine , parser = parser )
916
926
else :
917
927
res = df .query ('strings == ["a", "b"]' , engine = engine , parser = parser )
@@ -946,10 +956,12 @@ def test_query_with_string_columns(self, parser, engine):
946
956
expec = df [df .a .isin (df .b ) & (df .c < df .d )]
947
957
tm .assert_frame_equal (res , expec )
948
958
else :
949
- with pytest .raises (NotImplementedError ):
959
+ msg = r"'(Not)?In' nodes are not implemented"
960
+ with pytest .raises (NotImplementedError , match = msg ):
950
961
df .query ("a in b" , parser = parser , engine = engine )
951
962
952
- with pytest .raises (NotImplementedError ):
963
+ msg = r"'BoolOp' nodes are not implemented"
964
+ with pytest .raises (NotImplementedError , match = msg ):
953
965
df .query ("a in b and c < d" , parser = parser , engine = engine )
954
966
955
967
def test_object_array_eq_ne (self , parser , engine ):
@@ -1186,15 +1198,18 @@ def test_missing_attribute(self, df):
1186
1198
df .eval ("@pd.thing" )
1187
1199
1188
1200
def test_failing_quote (self , df ):
1189
- with pytest .raises (SyntaxError ):
1201
+ msg = r"(Could not convert ).*( to a valid Python identifier.)"
1202
+ with pytest .raises (SyntaxError , match = msg ):
1190
1203
df .query ("`it's` > `that's`" )
1191
1204
1192
1205
def test_failing_character_outside_range (self , df ):
1193
- with pytest .raises (SyntaxError ):
1206
+ msg = r"(Could not convert ).*( to a valid Python identifier.)"
1207
+ with pytest .raises (SyntaxError , match = msg ):
1194
1208
df .query ("`☺` > 4" )
1195
1209
1196
1210
def test_failing_hashtag (self , df ):
1197
- with pytest .raises (SyntaxError ):
1211
+ msg = "Failed to parse backticks"
1212
+ with pytest .raises (SyntaxError , match = msg ):
1198
1213
df .query ("`foo#bar` > 4" )
1199
1214
1200
1215
def test_call_non_named_expression (self , df ):
0 commit comments