@@ -71,9 +71,14 @@ def test_query_numexpr(self):
7171 result = df .eval ("A+1" , engine = "numexpr" )
7272 tm .assert_series_equal (result , self .expected2 , check_names = False )
7373 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 ):
7580 df .query ("A>0" , engine = "numexpr" )
76- with pytest .raises (ImportError ):
81+ with pytest .raises (ImportError , match = msg ):
7782 df .eval ("A+1" , engine = "numexpr" )
7883
7984
@@ -452,14 +457,16 @@ def test_date_query_with_non_date(self):
452457 result = df .query ("dates != nondate" , parser = parser , engine = engine )
453458 tm .assert_frame_equal (result , df )
454459
460+ msg = r"Invalid comparison between dtype=datetime64\[ns\] and ndarray"
455461 for op in ["<" , ">" , "<=" , ">=" ]:
456- with pytest .raises (TypeError ):
462+ with pytest .raises (TypeError , match = msg ):
457463 df .query (f"dates { op } nondate" , parser = parser , engine = engine )
458464
459465 def test_query_syntax_error (self ):
460466 engine , parser = self .engine , self .parser
461467 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 ):
463470 df .query ("i - +" , engine = engine , parser = parser )
464471
465472 def test_query_scope (self ):
@@ -781,7 +788,8 @@ def test_date_index_query_with_NaT_duplicates(self):
781788 df ["dates3" ] = date_range ("1/1/2014" , periods = n )
782789 df .loc [np .random .rand (n ) > 0.5 , "dates1" ] = pd .NaT
783790 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 ):
785793 df .query ("index < 20130101 < dates3" , engine = engine , parser = parser )
786794
787795 def test_nested_scope (self ):
@@ -798,7 +806,8 @@ def test_nested_scope(self):
798806 df2 = DataFrame (np .random .randn (5 , 3 ))
799807
800808 # 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 ):
802811 df .query ("(@df>0) & (@df2>0)" , engine = engine , parser = parser )
803812
804813 with pytest .raises (UndefinedVariableError , match = "name 'df' is not defined" ):
@@ -867,10 +876,10 @@ def test_str_query_method(self, parser, engine):
867876
868877 eq , ne = "==" , "!="
869878 ops = 2 * ([eq ] + [ne ])
879+ msg = r"'(Not)?In' nodes are not implemented"
870880
871881 for lhs , op , rhs in zip (lhs , ops , rhs ):
872882 ex = f"{ lhs } { op } { rhs } "
873- msg = r"'(Not)?In' nodes are not implemented"
874883 with pytest .raises (NotImplementedError , match = msg ):
875884 df .query (
876885 ex ,
@@ -908,10 +917,11 @@ def test_str_list_query_method(self, parser, engine):
908917
909918 eq , ne = "==" , "!="
910919 ops = 2 * ([eq ] + [ne ])
920+ msg = r"'(Not)?In' nodes are not implemented"
911921
912922 for lhs , op , rhs in zip (lhs , ops , rhs ):
913923 ex = f"{ lhs } { op } { rhs } "
914- with pytest .raises (NotImplementedError ):
924+ with pytest .raises (NotImplementedError , match = msg ):
915925 df .query (ex , engine = engine , parser = parser )
916926 else :
917927 res = df .query ('strings == ["a", "b"]' , engine = engine , parser = parser )
@@ -946,10 +956,12 @@ def test_query_with_string_columns(self, parser, engine):
946956 expec = df [df .a .isin (df .b ) & (df .c < df .d )]
947957 tm .assert_frame_equal (res , expec )
948958 else :
949- with pytest .raises (NotImplementedError ):
959+ msg = r"'(Not)?In' nodes are not implemented"
960+ with pytest .raises (NotImplementedError , match = msg ):
950961 df .query ("a in b" , parser = parser , engine = engine )
951962
952- with pytest .raises (NotImplementedError ):
963+ msg = r"'BoolOp' nodes are not implemented"
964+ with pytest .raises (NotImplementedError , match = msg ):
953965 df .query ("a in b and c < d" , parser = parser , engine = engine )
954966
955967 def test_object_array_eq_ne (self , parser , engine ):
@@ -1186,15 +1198,18 @@ def test_missing_attribute(self, df):
11861198 df .eval ("@pd.thing" )
11871199
11881200 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 ):
11901203 df .query ("`it's` > `that's`" )
11911204
11921205 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 ):
11941208 df .query ("`☺` > 4" )
11951209
11961210 def test_failing_hashtag (self , df ):
1197- with pytest .raises (SyntaxError ):
1211+ msg = "Failed to parse backticks"
1212+ with pytest .raises (SyntaxError , match = msg ):
11981213 df .query ("`foo#bar` > 4" )
11991214
12001215 def test_call_non_named_expression (self , df ):
0 commit comments