12
12
import pytest
13
13
14
14
from pandas .compat import (
15
- PY3 , PY36 , is_platform_little_endian , lmap , long , lrange , lzip , range , zip )
15
+ PY2 , PY3 , PY36 , is_platform_little_endian , lmap , long , lrange , lzip , range ,
16
+ zip )
16
17
17
18
from pandas .core .dtypes .cast import construct_1d_object_array_from_listlike
18
19
from pandas .core .dtypes .common import is_integer_dtype
@@ -58,8 +59,9 @@ def test_constructor_cast_failure(self):
58
59
df ['foo' ] = np .ones ((4 , 2 )).tolist ()
59
60
60
61
# this is not ok
61
- pytest .raises (ValueError , df .__setitem__ , tuple (['test' ]),
62
- np .ones ((4 , 2 )))
62
+ msg = "Wrong number of items passed 2, placement implies 1"
63
+ with pytest .raises (ValueError , match = msg ):
64
+ df ['test' ] = np .ones ((4 , 2 ))
63
65
64
66
# this is ok
65
67
df ['foo2' ] = np .ones ((4 , 2 )).tolist ()
@@ -1259,7 +1261,9 @@ def test_constructor_Series_named(self):
1259
1261
expected = DataFrame ({0 : s })
1260
1262
tm .assert_frame_equal (df , expected )
1261
1263
1262
- pytest .raises (ValueError , DataFrame , s , columns = [1 , 2 ])
1264
+ msg = r"Shape of passed values is \(10, 1\), indices imply \(10, 2\)"
1265
+ with pytest .raises (ValueError , match = msg ):
1266
+ DataFrame (s , columns = [1 , 2 ])
1263
1267
1264
1268
# #2234
1265
1269
a = Series ([], name = 'x' )
@@ -1433,8 +1437,10 @@ def test_constructor_column_duplicates(self):
1433
1437
1434
1438
tm .assert_frame_equal (idf , edf )
1435
1439
1436
- pytest .raises (ValueError , DataFrame .from_dict ,
1437
- OrderedDict ([('b' , 8 ), ('a' , 5 ), ('a' , 6 )]))
1440
+ msg = "If using all scalar values, you must pass an index"
1441
+ with pytest .raises (ValueError , match = msg ):
1442
+ DataFrame .from_dict (
1443
+ OrderedDict ([('b' , 8 ), ('a' , 5 ), ('a' , 6 )]))
1438
1444
1439
1445
def test_constructor_empty_with_string_dtype (self ):
1440
1446
# GH 9428
@@ -1465,8 +1471,11 @@ def test_constructor_single_value(self):
1465
1471
dtype = object ),
1466
1472
index = [1 , 2 ], columns = ['a' , 'c' ]))
1467
1473
1468
- pytest .raises (ValueError , DataFrame , 'a' , [1 , 2 ])
1469
- pytest .raises (ValueError , DataFrame , 'a' , columns = ['a' , 'c' ])
1474
+ msg = "DataFrame constructor not properly called!"
1475
+ with pytest .raises (ValueError , match = msg ):
1476
+ DataFrame ('a' , [1 , 2 ])
1477
+ with pytest .raises (ValueError , match = msg ):
1478
+ DataFrame ('a' , columns = ['a' , 'c' ])
1470
1479
1471
1480
msg = 'incompatible data and dtype'
1472
1481
with pytest .raises (TypeError , match = msg ):
@@ -1692,6 +1701,7 @@ def test_constructor_series_copy(self):
1692
1701
1693
1702
assert not (series ['A' ] == 5 ).all ()
1694
1703
1704
+ @pytest .mark .skipif (PY2 , reason = "pytest.raises match regex fails" )
1695
1705
def test_constructor_with_nas (self ):
1696
1706
# GH 5016
1697
1707
# na's in indices
@@ -1704,9 +1714,11 @@ def check(df):
1704
1714
1705
1715
# No NaN found -> error
1706
1716
if len (indexer ) == 0 :
1707
- def f ():
1717
+ msg = ("cannot do label indexing on"
1718
+ r" <class 'pandas\.core\.indexes\.range\.RangeIndex'>"
1719
+ r" with these indexers \[nan\] of <class 'float'>" )
1720
+ with pytest .raises (TypeError , match = msg ):
1708
1721
df .loc [:, np .nan ]
1709
- pytest .raises (TypeError , f )
1710
1722
# single nan should result in Series
1711
1723
elif len (indexer ) == 1 :
1712
1724
tm .assert_series_equal (df .iloc [:, indexer [0 ]],
@@ -1782,13 +1794,15 @@ def test_constructor_categorical(self):
1782
1794
tm .assert_frame_equal (df , expected )
1783
1795
1784
1796
# invalid (shape)
1785
- pytest .raises (ValueError ,
1786
- lambda : DataFrame ([Categorical (list ('abc' )),
1787
- Categorical (list ('abdefg' ))]))
1797
+ msg = r"Shape of passed values is \(6, 2\), indices imply \(3, 2\)"
1798
+ with pytest .raises (ValueError , match = msg ):
1799
+ DataFrame ([Categorical (list ('abc' )),
1800
+ Categorical (list ('abdefg' ))])
1788
1801
1789
1802
# ndim > 1
1790
- pytest .raises (NotImplementedError ,
1791
- lambda : Categorical (np .array ([list ('abcd' )])))
1803
+ msg = "> 1 ndim Categorical are not supported at this time"
1804
+ with pytest .raises (NotImplementedError , match = msg ):
1805
+ Categorical (np .array ([list ('abcd' )]))
1792
1806
1793
1807
def test_constructor_categorical_series (self ):
1794
1808
@@ -2164,8 +2178,11 @@ def test_from_records_bad_index_column(self):
2164
2178
tm .assert_index_equal (df1 .index , Index (df .C ))
2165
2179
2166
2180
# should fail
2167
- pytest .raises (ValueError , DataFrame .from_records , df , index = [2 ])
2168
- pytest .raises (KeyError , DataFrame .from_records , df , index = 2 )
2181
+ msg = r"Shape of passed values is \(10, 3\), indices imply \(1, 3\)"
2182
+ with pytest .raises (ValueError , match = msg ):
2183
+ DataFrame .from_records (df , index = [2 ])
2184
+ with pytest .raises (KeyError , match = r"^2$" ):
2185
+ DataFrame .from_records (df , index = 2 )
2169
2186
2170
2187
def test_from_records_non_tuple (self ):
2171
2188
class Record (object ):
0 commit comments