11# -*- coding: utf-8 -*-
22
33from collections import OrderedDict
4- import re
54
65import numpy as np
76import pytest
@@ -30,10 +29,10 @@ def test_constructor_no_levels():
3029 with pytest .raises (ValueError , match = msg ):
3130 MultiIndex (levels = [], codes = [])
3231
33- both_re = re . compile ( ' Must pass both levels and codes' )
34- with pytest .raises (TypeError , match = both_re ):
32+ msg = " Must pass both levels and codes"
33+ with pytest .raises (TypeError , match = msg ):
3534 MultiIndex (levels = [])
36- with pytest .raises (TypeError , match = both_re ):
35+ with pytest .raises (TypeError , match = msg ):
3736 MultiIndex (codes = [])
3837
3938
@@ -42,20 +41,20 @@ def test_constructor_nonhashable_names():
4241 levels = [[1 , 2 ], [u'one' , u'two' ]]
4342 codes = [[0 , 0 , 1 , 1 ], [0 , 1 , 0 , 1 ]]
4443 names = (['foo' ], ['bar' ])
45- message = "MultiIndex.name must be a hashable type"
46- with pytest .raises (TypeError , match = message ):
44+ msg = r "MultiIndex\ .name must be a hashable type"
45+ with pytest .raises (TypeError , match = msg ):
4746 MultiIndex (levels = levels , codes = codes , names = names )
4847
4948 # With .rename()
5049 mi = MultiIndex (levels = [[1 , 2 ], [u'one' , u'two' ]],
5150 codes = [[0 , 0 , 1 , 1 ], [0 , 1 , 0 , 1 ]],
5251 names = ('foo' , 'bar' ))
5352 renamed = [['foor' ], ['barr' ]]
54- with pytest .raises (TypeError , match = message ):
53+ with pytest .raises (TypeError , match = msg ):
5554 mi .rename (names = renamed )
5655
5756 # With .set_names()
58- with pytest .raises (TypeError , match = message ):
57+ with pytest .raises (TypeError , match = msg ):
5958 mi .set_names (names = renamed )
6059
6160
@@ -67,8 +66,9 @@ def test_constructor_mismatched_codes_levels(idx):
6766 with pytest .raises (ValueError , match = msg ):
6867 MultiIndex (levels = levels , codes = codes )
6968
70- length_error = re .compile ('>= length of level' )
71- label_error = re .compile (r'Unequal code lengths: \[4, 2\]' )
69+ length_error = (r"On level 0, code max \(3\) >= length of level \(1\)\."
70+ " NOTE: this index is in an inconsistent state" )
71+ label_error = r"Unequal code lengths: \[4, 2\]"
7272
7373 # important to check that it's looking at the right thing.
7474 with pytest .raises (ValueError , match = length_error ):
@@ -253,21 +253,14 @@ def test_from_arrays_empty():
253253 tm .assert_index_equal (result , expected )
254254
255255
256- @pytest .mark .parametrize ('invalid_array' , [
257- (1 ),
258- ([1 ]),
259- ([1 , 2 ]),
260- ([[1 ], 2 ]),
261- ('a' ),
262- (['a' ]),
263- (['a' , 'b' ]),
264- ([['a' ], 'b' ]),
265- ])
266- def test_from_arrays_invalid_input (invalid_array ):
267- invalid_inputs = [1 , [1 ], [1 , 2 ], [[1 ], 2 ],
268- 'a' , ['a' ], ['a' , 'b' ], [['a' ], 'b' ]]
269- for i in invalid_inputs :
270- pytest .raises (TypeError , MultiIndex .from_arrays , arrays = i )
256+ @pytest .mark .parametrize ('invalid_sequence_of_arrays' , [
257+ 1 , [1 ], [1 , 2 ], [[1 ], 2 ], 'a' , ['a' ], ['a' , 'b' ], [['a' ], 'b' ]])
258+ def test_from_arrays_invalid_input (invalid_sequence_of_arrays ):
259+ msg = (r"Input must be a list / sequence of array-likes|"
260+ r"Input must be list-like|"
261+ r"object of type 'int' has no len\(\)" )
262+ with pytest .raises (TypeError , match = msg ):
263+ MultiIndex .from_arrays (arrays = invalid_sequence_of_arrays )
271264
272265
273266@pytest .mark .parametrize ('idx1, idx2' , [
@@ -332,9 +325,10 @@ def test_tuples_with_name_string():
332325 # GH 15110 and GH 14848
333326
334327 li = [(0 , 0 , 1 ), (0 , 1 , 0 ), (1 , 0 , 0 )]
335- with pytest .raises (ValueError ):
328+ msg = "Names should be list-like for a MultiIndex"
329+ with pytest .raises (ValueError , match = msg ):
336330 pd .Index (li , name = 'abc' )
337- with pytest .raises (ValueError ):
331+ with pytest .raises (ValueError , match = msg ):
338332 pd .Index (li , name = 'a' )
339333
340334
@@ -398,7 +392,10 @@ def test_from_product_empty_three_levels(N):
398392 [['a' ], 'b' ],
399393])
400394def test_from_product_invalid_input (invalid_input ):
401- pytest .raises (TypeError , MultiIndex .from_product , iterables = invalid_input )
395+ msg = (r"Input must be a list / sequence of iterables|"
396+ "Input must be list-like" )
397+ with pytest .raises (TypeError , match = msg ):
398+ MultiIndex .from_product (iterables = invalid_input )
402399
403400
404401def test_from_product_datetimeindex ():
@@ -563,15 +560,15 @@ def test_from_frame_valid_names(names_in, names_out):
563560 assert mi .names == names_out
564561
565562
566- @pytest .mark .parametrize ('names_in,names_out ' , [
567- ('bad_input' , ValueError ( "Names should be list-like for a MultiIndex" ) ),
568- (['a' , 'b' , 'c' ], ValueError ( "Length of names must match number of "
569- " levels in MultiIndex." ) )
563+ @pytest .mark .parametrize ('names,expected_error_msg ' , [
564+ ('bad_input' , "Names should be list-like for a MultiIndex" ),
565+ (['a' , 'b' , 'c' ],
566+ "Length of names must match number of levels in MultiIndex" )
570567])
571- def test_from_frame_invalid_names (names_in , names_out ):
568+ def test_from_frame_invalid_names (names , expected_error_msg ):
572569 # GH 22420
573570 df = pd .DataFrame ([['a' , 'a' ], ['a' , 'b' ], ['b' , 'a' ], ['b' , 'b' ]],
574571 columns = pd .MultiIndex .from_tuples ([('L1' , 'x' ),
575572 ('L2' , 'y' )]))
576- with pytest .raises (type ( names_out ) , match = names_out . args [ 0 ] ):
577- pd .MultiIndex .from_frame (df , names = names_in )
573+ with pytest .raises (ValueError , match = expected_error_msg ):
574+ pd .MultiIndex .from_frame (df , names = names )
0 commit comments