@@ -375,7 +375,8 @@ def check_pow(self, lhs, arith1, rhs):
375
375
and is_scalar (rhs )
376
376
and _is_py3_complex_incompat (result , expected )
377
377
):
378
- with pytest .raises (AssertionError ):
378
+ msg = "(DataFrame.columns|numpy array) are different"
379
+ with pytest .raises (AssertionError , match = msg ):
379
380
tm .assert_numpy_array_equal (result , expected )
380
381
else :
381
382
tm .assert_almost_equal (result , expected )
@@ -449,16 +450,19 @@ def test_frame_invert(self):
449
450
# float always raises
450
451
lhs = DataFrame (randn (5 , 2 ))
451
452
if self .engine == "numexpr" :
452
- with pytest .raises (NotImplementedError ):
453
+ msg = "couldn't find matching opcode for 'invert_dd'"
454
+ with pytest .raises (NotImplementedError , match = msg ):
453
455
result = pd .eval (expr , engine = self .engine , parser = self .parser )
454
456
else :
455
- with pytest .raises (TypeError ):
457
+ msg = "ufunc 'invert' not supported for the input types"
458
+ with pytest .raises (TypeError , match = msg ):
456
459
result = pd .eval (expr , engine = self .engine , parser = self .parser )
457
460
458
461
# int raises on numexpr
459
462
lhs = DataFrame (randint (5 , size = (5 , 2 )))
460
463
if self .engine == "numexpr" :
461
- with pytest .raises (NotImplementedError ):
464
+ msg = "couldn't find matching opcode for 'invert"
465
+ with pytest .raises (NotImplementedError , match = msg ):
462
466
result = pd .eval (expr , engine = self .engine , parser = self .parser )
463
467
else :
464
468
expect = ~ lhs
@@ -474,10 +478,11 @@ def test_frame_invert(self):
474
478
# object raises
475
479
lhs = DataFrame ({"b" : ["a" , 1 , 2.0 ], "c" : rand (3 ) > 0.5 })
476
480
if self .engine == "numexpr" :
477
- with pytest .raises (ValueError ):
481
+ with pytest .raises (ValueError , match = "unknown type object" ):
478
482
result = pd .eval (expr , engine = self .engine , parser = self .parser )
479
483
else :
480
- with pytest .raises (TypeError ):
484
+ msg = "bad operand type for unary ~: 'str'"
485
+ with pytest .raises (TypeError , match = msg ):
481
486
result = pd .eval (expr , engine = self .engine , parser = self .parser )
482
487
483
488
def test_series_invert (self ):
@@ -488,16 +493,19 @@ def test_series_invert(self):
488
493
# float raises
489
494
lhs = Series (randn (5 ))
490
495
if self .engine == "numexpr" :
491
- with pytest .raises (NotImplementedError ):
496
+ msg = "couldn't find matching opcode for 'invert_dd'"
497
+ with pytest .raises (NotImplementedError , match = msg ):
492
498
result = pd .eval (expr , engine = self .engine , parser = self .parser )
493
499
else :
494
- with pytest .raises (TypeError ):
500
+ msg = "ufunc 'invert' not supported for the input types"
501
+ with pytest .raises (TypeError , match = msg ):
495
502
result = pd .eval (expr , engine = self .engine , parser = self .parser )
496
503
497
504
# int raises on numexpr
498
505
lhs = Series (randint (5 , size = 5 ))
499
506
if self .engine == "numexpr" :
500
- with pytest .raises (NotImplementedError ):
507
+ msg = "couldn't find matching opcode for 'invert"
508
+ with pytest .raises (NotImplementedError , match = msg ):
501
509
result = pd .eval (expr , engine = self .engine , parser = self .parser )
502
510
else :
503
511
expect = ~ lhs
@@ -517,10 +525,11 @@ def test_series_invert(self):
517
525
# object
518
526
lhs = Series (["a" , 1 , 2.0 ])
519
527
if self .engine == "numexpr" :
520
- with pytest .raises (ValueError ):
528
+ with pytest .raises (ValueError , match = "unknown type object" ):
521
529
result = pd .eval (expr , engine = self .engine , parser = self .parser )
522
530
else :
523
- with pytest .raises (TypeError ):
531
+ msg = "bad operand type for unary ~: 'str'"
532
+ with pytest .raises (TypeError , match = msg ):
524
533
result = pd .eval (expr , engine = self .engine , parser = self .parser )
525
534
526
535
def test_frame_negate (self ):
@@ -541,7 +550,8 @@ def test_frame_negate(self):
541
550
# bool doesn't work with numexpr but works elsewhere
542
551
lhs = DataFrame (rand (5 , 2 ) > 0.5 )
543
552
if self .engine == "numexpr" :
544
- with pytest .raises (NotImplementedError ):
553
+ msg = "couldn't find matching opcode for 'neg_bb'"
554
+ with pytest .raises (NotImplementedError , match = msg ):
545
555
result = pd .eval (expr , engine = self .engine , parser = self .parser )
546
556
else :
547
557
expect = - lhs
@@ -566,7 +576,8 @@ def test_series_negate(self):
566
576
# bool doesn't work with numexpr but works elsewhere
567
577
lhs = Series (rand (5 ) > 0.5 )
568
578
if self .engine == "numexpr" :
569
- with pytest .raises (NotImplementedError ):
579
+ msg = "couldn't find matching opcode for 'neg_bb'"
580
+ with pytest .raises (NotImplementedError , match = msg ):
570
581
result = pd .eval (expr , engine = self .engine , parser = self .parser )
571
582
else :
572
583
expect = - lhs
@@ -610,7 +621,8 @@ def test_series_pos(self, lhs):
610
621
tm .assert_series_equal (expect , result )
611
622
612
623
def test_scalar_unary (self ):
613
- with pytest .raises (TypeError ):
624
+ msg = "bad operand type for unary ~: 'float'"
625
+ with pytest .raises (TypeError , match = msg ):
614
626
pd .eval ("~1.0" , engine = self .engine , parser = self .parser )
615
627
616
628
assert pd .eval ("-1.0" , parser = self .parser , engine = self .engine ) == - 1.0
@@ -671,7 +683,8 @@ def test_disallow_scalar_bool_ops(self):
671
683
672
684
x , a , b , df = np .random .randn (3 ), 1 , 2 , DataFrame (randn (3 , 2 )) # noqa
673
685
for ex in exprs :
674
- with pytest .raises (NotImplementedError ):
686
+ msg = "cannot evaluate scalar only bool ops|'BoolOp' nodes are not"
687
+ with pytest .raises (NotImplementedError , match = msg ):
675
688
pd .eval (ex , engine = self .engine , parser = self .parser )
676
689
677
690
def test_identical (self ):
@@ -772,7 +785,8 @@ def setup_ops(self):
772
785
773
786
def check_chained_cmp_op (self , lhs , cmp1 , mid , cmp2 , rhs ):
774
787
ex1 = f"lhs { cmp1 } mid { cmp2 } rhs"
775
- with pytest .raises (NotImplementedError ):
788
+ msg = "'BoolOp' nodes are not implemented"
789
+ with pytest .raises (NotImplementedError , match = msg ):
776
790
pd .eval (ex1 , engine = self .engine , parser = self .parser )
777
791
778
792
@@ -1183,7 +1197,8 @@ def test_bool_ops_with_constants(self):
1183
1197
def test_4d_ndarray_fails (self ):
1184
1198
x = randn (3 , 4 , 5 , 6 )
1185
1199
y = Series (randn (10 ))
1186
- with pytest .raises (NotImplementedError ):
1200
+ msg = "N-dimensional objects, where N > 2, are not supported with eval"
1201
+ with pytest .raises (NotImplementedError , match = msg ):
1187
1202
self .eval ("x + y" , local_dict = {"x" : x , "y" : y })
1188
1203
1189
1204
def test_constant (self ):
@@ -1232,7 +1247,7 @@ def test_truediv(self):
1232
1247
1233
1248
def test_failing_subscript_with_name_error (self ):
1234
1249
df = DataFrame (np .random .randn (5 , 3 )) # noqa
1235
- with pytest .raises (NameError ):
1250
+ with pytest .raises (NameError , match = "name 'x' is not defined" ):
1236
1251
self .eval ("df[x > 2] > 2" )
1237
1252
1238
1253
def test_lhs_expression_subscript (self ):
@@ -1379,7 +1394,8 @@ def test_multi_line_expression(self):
1379
1394
assert ans is None
1380
1395
1381
1396
# multi-line not valid if not all assignments
1382
- with pytest .raises (ValueError ):
1397
+ msg = "Multi-line expressions are only valid if all expressions contain"
1398
+ with pytest .raises (ValueError , match = msg ):
1383
1399
df .eval (
1384
1400
"""
1385
1401
a = b + 2
@@ -1474,7 +1490,8 @@ def test_assignment_in_query(self):
1474
1490
# GH 8664
1475
1491
df = pd .DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ]})
1476
1492
df_orig = df .copy ()
1477
- with pytest .raises (ValueError ):
1493
+ msg = "cannot assign without a target object"
1494
+ with pytest .raises (ValueError , match = msg ):
1478
1495
df .query ("a = 1" )
1479
1496
tm .assert_frame_equal (df , df_orig )
1480
1497
@@ -1593,19 +1610,21 @@ def test_simple_in_ops(self):
1593
1610
)
1594
1611
assert res
1595
1612
else :
1596
- with pytest .raises (NotImplementedError ):
1613
+ msg = "'In' nodes are not implemented"
1614
+ with pytest .raises (NotImplementedError , match = msg ):
1597
1615
pd .eval ("1 in [1, 2]" , engine = self .engine , parser = self .parser )
1598
- with pytest .raises (NotImplementedError ):
1616
+ with pytest .raises (NotImplementedError , match = msg ):
1599
1617
pd .eval ("2 in (1, 2)" , engine = self .engine , parser = self .parser )
1600
- with pytest .raises (NotImplementedError ):
1618
+ with pytest .raises (NotImplementedError , match = msg ):
1601
1619
pd .eval ("3 in (1, 2)" , engine = self .engine , parser = self .parser )
1602
- with pytest .raises (NotImplementedError ):
1603
- pd .eval ("3 not in (1, 2)" , engine = self .engine , parser = self .parser )
1604
- with pytest .raises (NotImplementedError ):
1620
+ with pytest .raises (NotImplementedError , match = msg ):
1605
1621
pd .eval (
1606
1622
"[(3,)] in (1, 2, [(3,)])" , engine = self .engine , parser = self .parser
1607
1623
)
1608
- with pytest .raises (NotImplementedError ):
1624
+ msg = "'NotIn' nodes are not implemented"
1625
+ with pytest .raises (NotImplementedError , match = msg ):
1626
+ pd .eval ("3 not in (1, 2)" , engine = self .engine , parser = self .parser )
1627
+ with pytest .raises (NotImplementedError , match = msg ):
1609
1628
pd .eval (
1610
1629
"[3] not in (1, 2, [[3]])" , engine = self .engine , parser = self .parser
1611
1630
)
@@ -1664,13 +1683,15 @@ def test_fails_not(self):
1664
1683
def test_fails_ampersand (self ):
1665
1684
df = DataFrame (np .random .randn (5 , 3 )) # noqa
1666
1685
ex = "(df + 2)[df > 1] > 0 & (df > 0)"
1667
- with pytest .raises (NotImplementedError ):
1686
+ msg = "cannot evaluate scalar only bool ops"
1687
+ with pytest .raises (NotImplementedError , match = msg ):
1668
1688
pd .eval (ex , parser = self .parser , engine = self .engine )
1669
1689
1670
1690
def test_fails_pipe (self ):
1671
1691
df = DataFrame (np .random .randn (5 , 3 )) # noqa
1672
1692
ex = "(df + 2)[df > 1] > 0 | (df > 0)"
1673
- with pytest .raises (NotImplementedError ):
1693
+ msg = "cannot evaluate scalar only bool ops"
1694
+ with pytest .raises (NotImplementedError , match = msg ):
1674
1695
pd .eval (ex , parser = self .parser , engine = self .engine )
1675
1696
1676
1697
def test_bool_ops_with_constants (self ):
@@ -1679,7 +1700,8 @@ def test_bool_ops_with_constants(self):
1679
1700
):
1680
1701
ex = f"{ lhs } { op } { rhs } "
1681
1702
if op in ("and" , "or" ):
1682
- with pytest .raises (NotImplementedError ):
1703
+ msg = "'BoolOp' nodes are not implemented"
1704
+ with pytest .raises (NotImplementedError , match = msg ):
1683
1705
self .eval (ex )
1684
1706
else :
1685
1707
res = self .eval (ex )
@@ -1690,7 +1712,8 @@ def test_simple_bool_ops(self):
1690
1712
for op , lhs , rhs in product (expr ._bool_ops_syms , (True , False ), (True , False )):
1691
1713
ex = f"lhs { op } rhs"
1692
1714
if op in ("and" , "or" ):
1693
- with pytest .raises (NotImplementedError ):
1715
+ msg = "'BoolOp' nodes are not implemented"
1716
+ with pytest .raises (NotImplementedError , match = msg ):
1694
1717
pd .eval (ex , engine = self .engine , parser = self .parser )
1695
1718
else :
1696
1719
res = pd .eval (ex , engine = self .engine , parser = self .parser )
@@ -1902,19 +1925,21 @@ def test_disallowed_nodes(engine, parser):
1902
1925
inst = VisitorClass ("x + 1" , engine , parser )
1903
1926
1904
1927
for ops in uns_ops :
1905
- with pytest .raises (NotImplementedError ):
1928
+ msg = "nodes are not implemented"
1929
+ with pytest .raises (NotImplementedError , match = msg ):
1906
1930
getattr (inst , ops )()
1907
1931
1908
1932
1909
1933
def test_syntax_error_exprs (engine , parser ):
1910
1934
e = "s +"
1911
- with pytest .raises (SyntaxError ):
1935
+ with pytest .raises (SyntaxError , match = "invalid syntax" ):
1912
1936
pd .eval (e , engine = engine , parser = parser )
1913
1937
1914
1938
1915
1939
def test_name_error_exprs (engine , parser ):
1916
1940
e = "s + t"
1917
- with pytest .raises (NameError ):
1941
+ msg = "name 's' is not defined"
1942
+ with pytest .raises (NameError , match = msg ):
1918
1943
pd .eval (e , engine = engine , parser = parser )
1919
1944
1920
1945
@@ -1973,7 +1998,8 @@ def test_bool_ops_fails_on_scalars(lhs, cmp, rhs, engine, parser):
1973
1998
ex2 = f"lhs { cmp } mid and mid { cmp } rhs"
1974
1999
ex3 = f"(lhs { cmp } mid) & (mid { cmp } rhs)"
1975
2000
for ex in (ex1 , ex2 , ex3 ):
1976
- with pytest .raises (NotImplementedError ):
2001
+ msg = "cannot evaluate scalar only bool ops|'BoolOp' nodes are not"
2002
+ with pytest .raises (NotImplementedError , match = msg ):
1977
2003
pd .eval (ex , engine = engine , parser = parser )
1978
2004
1979
2005
@@ -2029,7 +2055,8 @@ def test_negate_lt_eq_le(engine, parser):
2029
2055
tm .assert_frame_equal (result , expected )
2030
2056
2031
2057
if parser == "python" :
2032
- with pytest .raises (NotImplementedError ):
2058
+ msg = "'Not' nodes are not implemented"
2059
+ with pytest .raises (NotImplementedError , match = msg ):
2033
2060
df .query ("not (cat > 0)" , engine = engine , parser = parser )
2034
2061
else :
2035
2062
result = df .query ("not (cat > 0)" , engine = engine , parser = parser )
@@ -2041,5 +2068,6 @@ def test_validate_bool_args(self):
2041
2068
invalid_values = [1 , "True" , [1 , 2 , 3 ], 5.0 ]
2042
2069
2043
2070
for value in invalid_values :
2044
- with pytest .raises (ValueError ):
2071
+ msg = 'For argument "inplace" expected type bool, received type'
2072
+ with pytest .raises (ValueError , match = msg ):
2045
2073
pd .eval ("2+2" , inplace = value )
0 commit comments