1212import pytest
1313
1414from 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 )
1617
1718from pandas .core .dtypes .cast import construct_1d_object_array_from_listlike
1819from pandas .core .dtypes .common import is_integer_dtype
@@ -58,8 +59,9 @@ def test_constructor_cast_failure(self):
5859 df ['foo' ] = np .ones ((4 , 2 )).tolist ()
5960
6061 # 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 ))
6365
6466 # this is ok
6567 df ['foo2' ] = np .ones ((4 , 2 )).tolist ()
@@ -1259,7 +1261,9 @@ def test_constructor_Series_named(self):
12591261 expected = DataFrame ({0 : s })
12601262 tm .assert_frame_equal (df , expected )
12611263
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 ])
12631267
12641268 # #2234
12651269 a = Series ([], name = 'x' )
@@ -1433,8 +1437,10 @@ def test_constructor_column_duplicates(self):
14331437
14341438 tm .assert_frame_equal (idf , edf )
14351439
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 )]))
14381444
14391445 def test_constructor_empty_with_string_dtype (self ):
14401446 # GH 9428
@@ -1465,8 +1471,11 @@ def test_constructor_single_value(self):
14651471 dtype = object ),
14661472 index = [1 , 2 ], columns = ['a' , 'c' ]))
14671473
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' ])
14701479
14711480 msg = 'incompatible data and dtype'
14721481 with pytest .raises (TypeError , match = msg ):
@@ -1692,6 +1701,7 @@ def test_constructor_series_copy(self):
16921701
16931702 assert not (series ['A' ] == 5 ).all ()
16941703
1704+ @pytest .mark .skipif (PY2 , reason = "pytest.raises match regex fails" )
16951705 def test_constructor_with_nas (self ):
16961706 # GH 5016
16971707 # na's in indices
@@ -1704,9 +1714,11 @@ def check(df):
17041714
17051715 # No NaN found -> error
17061716 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 ):
17081721 df .loc [:, np .nan ]
1709- pytest .raises (TypeError , f )
17101722 # single nan should result in Series
17111723 elif len (indexer ) == 1 :
17121724 tm .assert_series_equal (df .iloc [:, indexer [0 ]],
@@ -1782,13 +1794,15 @@ def test_constructor_categorical(self):
17821794 tm .assert_frame_equal (df , expected )
17831795
17841796 # 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' ))])
17881801
17891802 # 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' )]))
17921806
17931807 def test_constructor_categorical_series (self ):
17941808
@@ -2164,8 +2178,11 @@ def test_from_records_bad_index_column(self):
21642178 tm .assert_index_equal (df1 .index , Index (df .C ))
21652179
21662180 # 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 )
21692186
21702187 def test_from_records_non_tuple (self ):
21712188 class Record (object ):
0 commit comments