@@ -234,29 +234,32 @@ def test_from_arrays_empty():
234
234
tm .assert_index_equal (result , expected )
235
235
236
236
237
- def test_from_arrays_invalid_input ():
237
+ @pytest .mark .parametrize ('invalid_array' ,[
238
+ pytest .param (1 ),
239
+ pytest .param ([1 ]),
240
+ pytest .param ([1 , 2 ]),
241
+ pytest .param ([[1 ], 2 ]),
242
+ pytest .param ('a' ),
243
+ pytest .param (['a' ]),
244
+ pytest .param (['a' , 'b' ]),
245
+ pytest .param ([['a' ], 'b' ]),
246
+ ]
247
+ )
248
+ def test_from_arrays_invalid_input (invalid_array ):
238
249
invalid_inputs = [1 , [1 ], [1 , 2 ], [[1 ], 2 ],
239
250
'a' , ['a' ], ['a' , 'b' ], [['a' ], 'b' ]]
240
251
for i in invalid_inputs :
241
252
pytest .raises (TypeError , MultiIndex .from_arrays , arrays = i )
242
253
243
254
244
- def test_from_arrays_different_lengths ():
255
+ @pytest .mark .parametrize ('idx1, idx2' ,[
256
+ pytest .param ([1 , 2 , 3 ], ['a' , 'b' ]),
257
+ pytest .param ([], ['a' , 'b' ]),
258
+ pytest .param ([1 , 2 , 3 ], [])
259
+ ]
260
+ )
261
+ def test_from_arrays_different_lengths (idx1 , idx2 ):
245
262
# see gh-13599
246
- idx1 = [1 , 2 , 3 ]
247
- idx2 = ['a' , 'b' ]
248
- tm .assert_raises_regex (ValueError , '^all arrays must '
249
- 'be same length$' ,
250
- MultiIndex .from_arrays , [idx1 , idx2 ])
251
-
252
- idx1 = []
253
- idx2 = ['a' , 'b' ]
254
- tm .assert_raises_regex (ValueError , '^all arrays must '
255
- 'be same length$' ,
256
- MultiIndex .from_arrays , [idx1 , idx2 ])
257
-
258
- idx1 = [1 , 2 , 3 ]
259
- idx2 = []
260
263
tm .assert_raises_regex (ValueError , '^all arrays must '
261
264
'be same length$' ,
262
265
MultiIndex .from_arrays , [idx1 , idx2 ])
@@ -305,35 +308,41 @@ def test_from_tuples_index_values(idx):
305
308
assert (result .values == idx .values ).all ()
306
309
307
310
308
- def test_from_product_empty ():
311
+ def test_from_product_empty_zero_levels ():
309
312
# 0 levels
310
313
with tm .assert_raises_regex (
311
314
ValueError , "Must pass non-zero number of levels/labels" ):
312
315
MultiIndex .from_product ([])
313
316
314
- # 1 level
317
+
318
+ def test_from_product_empty_one_level ():
315
319
result = MultiIndex .from_product ([[]], names = ['A' ])
316
320
expected = pd .Index ([], name = 'A' )
317
321
tm .assert_index_equal (result .levels [0 ], expected )
318
322
319
- # 2 levels
320
- l1 = [[], ['foo' , 'bar' , 'baz' ], []]
321
- l2 = [[], [], ['a' , 'b' , 'c' ]]
323
+
324
+ @pytest .mark .parametrize ('first, second' ,[
325
+ ([],[]),
326
+ (['foo' , 'bar' , 'baz' ], []),
327
+ ([], ['a' , 'b' , 'c' ]),
328
+ ])
329
+ def test_from_product_empty_two_levels (first , second ):
322
330
names = ['A' , 'B' ]
323
- for first , second in zip ( l1 , l2 ):
324
- result = MultiIndex . from_product ( [first , second ], names = names )
325
- expected = MultiIndex ( levels = [ first , second ],
326
- labels = [[], []], names = names )
327
- tm . assert_index_equal ( result , expected )
331
+ result = MultiIndex . from_product ([ first , second ], names = names )
332
+ expected = MultiIndex ( levels = [first , second ],
333
+ labels = [[], []], names = names )
334
+ tm . assert_index_equal ( result , expected )
335
+
328
336
337
+ @pytest .mark .parametrize ('N' , list (range (4 )))
338
+ def test_from_product_empty_three_levels (N ):
329
339
# GH12258
330
340
names = ['A' , 'B' , 'C' ]
331
- for N in range (4 ):
332
- lvl2 = lrange (N )
333
- result = MultiIndex .from_product ([[], lvl2 , []], names = names )
334
- expected = MultiIndex (levels = [[], lvl2 , []],
335
- labels = [[], [], []], names = names )
336
- tm .assert_index_equal (result , expected )
341
+ lvl2 = lrange (N )
342
+ result = MultiIndex .from_product ([[], lvl2 , []], names = names )
343
+ expected = MultiIndex (levels = [[], lvl2 , []],
344
+ labels = [[], [], []], names = names )
345
+ tm .assert_index_equal (result , expected )
337
346
338
347
339
348
def test_from_product_invalid_input ():
@@ -352,19 +361,24 @@ def test_from_product_datetimeindex():
352
361
tm .assert_numpy_array_equal (mi .values , etalon )
353
362
354
363
355
- def test_from_product_index_series_categorical ():
364
+ @pytest .mark .parametrize ('ordered' , [False , True ])
365
+ @pytest .mark .parametrize ('f' , [
366
+ lambda x : x ,
367
+ lambda x : pd .Series (x ),
368
+ lambda x : x .values
369
+ ])
370
+ def test_from_product_index_series_categorical (ordered , f ):
356
371
# GH13743
357
372
first = ['foo' , 'bar' ]
358
- for ordered in [ False , True ]:
359
- idx = pd .CategoricalIndex (list ("abcaab" ), categories = list ("bac" ),
360
- ordered = ordered )
361
- expected = pd .CategoricalIndex (list ("abcaab" ) + list ("abcaab" ),
362
- categories = list ("bac" ),
363
- ordered = ordered )
373
+
374
+ idx = pd .CategoricalIndex (list ("abcaab" ), categories = list ("bac" ),
375
+ ordered = ordered )
376
+ expected = pd .CategoricalIndex (list ("abcaab" ) + list ("abcaab" ),
377
+ categories = list ("bac" ),
378
+ ordered = ordered )
364
379
365
- for arr in [idx , pd .Series (idx ), idx .values ]:
366
- result = pd .MultiIndex .from_product ([first , arr ])
367
- tm .assert_index_equal (result .get_level_values (1 ), expected )
380
+ result = pd .MultiIndex .from_product ([first , f (idx )])
381
+ tm .assert_index_equal (result .get_level_values (1 ), expected )
368
382
369
383
370
384
def test_from_product ():
@@ -409,19 +423,28 @@ def test_create_index_existing_name(idx):
409
423
index = idx
410
424
index .names = ['foo' , 'bar' ]
411
425
result = pd .Index (index )
412
- tm .assert_index_equal (
413
- result , Index (Index ([('foo' , 'one' ), ('foo' , 'two' ),
414
- ('bar' , 'one' ), ('baz' , 'two' ),
415
- ('qux' , 'one' ), ('qux' , 'two' )],
416
- dtype = 'object' ),
417
- names = ['foo' , 'bar' ]))
426
+ expected = Index (
427
+ Index ([
428
+ ('foo' , 'one' ), ('foo' , 'two' ),
429
+ ('bar' , 'one' ), ('baz' , 'two' ),
430
+ ('qux' , 'one' ), ('qux' , 'two' )],
431
+ dtype = 'object'
432
+ ),
433
+ names = ['foo' , 'bar' ]
434
+ )
435
+ tm .assert_index_equal (result , expected )
418
436
419
437
result = pd .Index (index , names = ['A' , 'B' ])
420
- tm .assert_index_equal (
421
- result ,
422
- Index (Index ([('foo' , 'one' ), ('foo' , 'two' ), ('bar' , 'one' ),
423
- ('baz' , 'two' ), ('qux' , 'one' ), ('qux' , 'two' )],
424
- dtype = 'object' ), names = ['A' , 'B' ]))
438
+ expected = Index (
439
+ Index ([
440
+ ('foo' , 'one' ), ('foo' , 'two' ),
441
+ ('bar' , 'one' ), ('baz' , 'two' ),
442
+ ('qux' , 'one' ), ('qux' , 'two' )],
443
+ dtype = 'object'
444
+ ),
445
+ names = ['A' , 'B' ]
446
+ )
447
+ tm .assert_index_equal (result , expected )
425
448
426
449
427
450
def test_tuples_with_name_string ():
0 commit comments