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