@@ -2381,7 +2381,7 @@ def test_level_setting_resets_attributes(self):
2381
2381
# if this fails, probably didn't reset the cache correctly.
2382
2382
assert not ind .is_monotonic
2383
2383
2384
- def test_is_monotonic (self ):
2384
+ def test_is_monotonic_increasing (self ):
2385
2385
i = MultiIndex .from_product ([np .arange (10 ),
2386
2386
np .arange (10 )], names = ['one' , 'two' ])
2387
2387
assert i .is_monotonic
@@ -2442,14 +2442,89 @@ def test_is_monotonic(self):
2442
2442
assert not i .is_monotonic
2443
2443
assert not i ._is_strictly_monotonic_increasing
2444
2444
2445
- def test_is_strictly_monotonic (self ):
2445
+ # empty
2446
+ i = MultiIndex .from_arrays ([[], []])
2447
+ assert i .is_monotonic
2448
+ assert Index (i .values ).is_monotonic
2449
+ assert i ._is_strictly_monotonic_increasing
2450
+ assert Index (i .values )._is_strictly_monotonic_increasing
2451
+
2452
+ def test_is_monotonic_decreasing (self ):
2453
+ i = MultiIndex .from_product ([np .arange (9 , - 1 , - 1 ),
2454
+ np .arange (9 , - 1 , - 1 )],
2455
+ names = ['one' , 'two' ])
2456
+ assert i .is_monotonic_decreasing
2457
+ assert i ._is_strictly_monotonic_decreasing
2458
+ assert Index (i .values ).is_monotonic_decreasing
2459
+ assert i ._is_strictly_monotonic_decreasing
2460
+
2461
+ i = MultiIndex .from_product ([np .arange (10 ),
2462
+ np .arange (10 , 0 , - 1 )],
2463
+ names = ['one' , 'two' ])
2464
+ assert not i .is_monotonic_decreasing
2465
+ assert not i ._is_strictly_monotonic_decreasing
2466
+ assert not Index (i .values ).is_monotonic_decreasing
2467
+ assert not Index (i .values )._is_strictly_monotonic_decreasing
2468
+
2469
+ i = MultiIndex .from_product ([np .arange (10 , 0 , - 1 ),
2470
+ np .arange (10 )], names = ['one' , 'two' ])
2471
+ assert not i .is_monotonic_decreasing
2472
+ assert not i ._is_strictly_monotonic_decreasing
2473
+ assert not Index (i .values ).is_monotonic_decreasing
2474
+ assert not Index (i .values )._is_strictly_monotonic_decreasing
2475
+
2476
+ i = MultiIndex .from_product ([[2.0 , np .nan , 1.0 ], ['c' , 'b' , 'a' ]])
2477
+ assert not i .is_monotonic_decreasing
2478
+ assert not i ._is_strictly_monotonic_decreasing
2479
+ assert not Index (i .values ).is_monotonic_decreasing
2480
+ assert not Index (i .values )._is_strictly_monotonic_decreasing
2481
+
2482
+ # string ordering
2483
+ i = MultiIndex (levels = [['qux' , 'foo' , 'baz' , 'bar' ],
2484
+ ['three' , 'two' , 'one' ]],
2485
+ labels = [[0 , 0 , 0 , 1 , 1 , 2 , 2 , 3 , 3 , 3 ],
2486
+ [0 , 1 , 2 , 0 , 1 , 1 , 2 , 0 , 1 , 2 ]],
2487
+ names = ['first' , 'second' ])
2488
+ assert not i .is_monotonic_decreasing
2489
+ assert not Index (i .values ).is_monotonic_decreasing
2490
+ assert not i ._is_strictly_monotonic_decreasing
2491
+ assert not Index (i .values )._is_strictly_monotonic_decreasing
2492
+
2493
+ i = MultiIndex (levels = [['qux' , 'foo' , 'baz' , 'bar' ],
2494
+ ['zenith' , 'next' , 'mom' ]],
2495
+ labels = [[0 , 0 , 0 , 1 , 1 , 2 , 2 , 3 , 3 , 3 ],
2496
+ [0 , 1 , 2 , 0 , 1 , 1 , 2 , 0 , 1 , 2 ]],
2497
+ names = ['first' , 'second' ])
2498
+ assert i .is_monotonic_decreasing
2499
+ assert Index (i .values ).is_monotonic_decreasing
2500
+ assert i ._is_strictly_monotonic_decreasing
2501
+ assert Index (i .values )._is_strictly_monotonic_decreasing
2502
+
2503
+ # mixed levels, hits the TypeError
2504
+ i = MultiIndex (
2505
+ levels = [[4 , 3 , 2 , 1 ], ['nl0000301109' , 'nl0000289965' ,
2506
+ 'nl0000289783' , 'lu0197800237' ,
2507
+ 'gb00b03mlx29' ]],
2508
+ labels = [[0 , 1 , 1 , 2 , 2 , 2 , 3 ], [4 , 2 , 0 , 0 , 1 , 3 , - 1 ]],
2509
+ names = ['household_id' , 'asset_id' ])
2510
+
2511
+ assert not i .is_monotonic_decreasing
2512
+ assert not i ._is_strictly_monotonic_decreasing
2513
+
2514
+ # empty
2515
+ i = MultiIndex .from_arrays ([[], []])
2516
+ assert i .is_monotonic_decreasing
2517
+ assert Index (i .values ).is_monotonic_decreasing
2518
+ assert i ._is_strictly_monotonic_decreasing
2519
+ assert Index (i .values )._is_strictly_monotonic_decreasing
2520
+
2521
+ def test_is_strictly_monotonic_increasing (self ):
2446
2522
idx = pd .MultiIndex (levels = [['bar' , 'baz' ], ['mom' , 'next' ]],
2447
2523
labels = [[0 , 0 , 1 , 1 ], [0 , 0 , 0 , 1 ]])
2448
2524
assert idx .is_monotonic_increasing
2449
2525
assert not idx ._is_strictly_monotonic_increasing
2450
2526
2451
- @pytest .mark .xfail (reason = "buggy MultiIndex.is_monotonic_decresaing." )
2452
- def test__is_strictly_monotonic_decreasing (self ):
2527
+ def test_is_strictly_monotonic_decreasing (self ):
2453
2528
idx = pd .MultiIndex (levels = [['baz' , 'bar' ], ['next' , 'mom' ]],
2454
2529
labels = [[0 , 0 , 1 , 1 ], [0 , 0 , 0 , 1 ]])
2455
2530
assert idx .is_monotonic_decreasing
0 commit comments