@@ -2449,6 +2449,31 @@ def test_getitem_slice_not_sorted(self):
24492449 expected  =  df .reindex (columns = df .columns [:3 ])
24502450 tm .assert_frame_equal (result , expected )
24512451
2452+  @pytest .mark .xfail (reason = "need axis reconstruction" ) 
2453+  def  test_frame_getitem_not_sorted2 (self ):
2454+  # 13431 
2455+  df  =  DataFrame ({'col1' : ['b' , 'd' , 'b' , 'a' ],
2456+  'col2' : [3 , 1 , 1 , 2 ],
2457+  'data' : ['one' , 'two' , 'three' , 'four' ]})
2458+ 
2459+  df2  =  df .set_index (['col1' , 'col2' ])
2460+  df2_original  =  df2 .copy ()
2461+ 
2462+  df2 .index .set_levels (['b' , 'd' , 'a' ], level = 'col1' , inplace = True )
2463+  df2 .index .set_labels ([0 , 1 , 0 , 2 ], level = 'col1' , inplace = True )
2464+  assert  not  df2 .index .is_lexsorted ()
2465+  assert  not  df2 .index .is_monotonic 
2466+ 
2467+  assert  df2_original .index .equals (df2 .index )
2468+ 
2469+  result  =  df2 .sort_index ()
2470+  assert  result .index .is_lexsorted ()
2471+  assert  result .index .is_monotonic 
2472+ 
2473+  result  =  df2 .sort_index (level = 0 )
2474+  assert  result .index .is_lexsorted ()
2475+  assert  result .index .is_monotonic 
2476+ 
24522477 def  test_frame_getitem_not_sorted (self ):
24532478 df  =  self .frame .T 
24542479 df ['foo' , 'four' ] =  'foo' 
@@ -2485,3 +2510,115 @@ def test_series_getitem_not_sorted(self):
24852510 expected .index  =  expected .index .droplevel (0 )
24862511 tm .assert_series_equal (result , expected )
24872512 tm .assert_series_equal (result2 , expected )
2513+ 
2514+  def  test_sort_index_reorder_on_ops (self ):
2515+  # 15687 
2516+  df  =  pd .DataFrame (
2517+  np .random .randn (8 , 2 ),
2518+  index = MultiIndex .from_product (
2519+  [['a' , 'b' ],
2520+  ['big' , 'small' ],
2521+  ['red' , 'blu' ]],
2522+  names = ['letter' , 'size' , 'color' ]),
2523+  columns = ['near' , 'far' ])
2524+  df  =  df .sort_index ()
2525+ 
2526+  def  my_func (group ):
2527+  group .index  =  ['newz' , 'newa' ]
2528+  return  group 
2529+ 
2530+  result  =  df .groupby (level = ['letter' , 'size' ]).apply (
2531+  my_func ).sort_index ()
2532+  expected  =  MultiIndex .from_product (
2533+  [['a' , 'b' ],
2534+  ['big' , 'small' ],
2535+  ['newa' , 'newz' ]],
2536+  names = ['letter' , 'size' , None ])
2537+ 
2538+  tm .assert_index_equal (result .index , expected )
2539+ 
2540+  def  test_sort_index_and_reconstruction (self ):
2541+ 
2542+  # 15622 
2543+  # lexsortedness should be identical 
2544+  # across MultiIndex consruction methods 
2545+ 
2546+  df  =  DataFrame ([[1 , 1 ], [2 , 2 ]], index = list ('ab' ))
2547+  expected  =  DataFrame ([[1 , 1 ], [2 , 2 ], [1 , 1 ], [2 , 2 ]],
2548+  index = MultiIndex .from_tuples ([(0.5 , 'a' ),
2549+  (0.5 , 'b' ),
2550+  (0.8 , 'a' ),
2551+  (0.8 , 'b' )]))
2552+  assert  expected .index .is_lexsorted ()
2553+ 
2554+  result  =  DataFrame (
2555+  [[1 , 1 ], [2 , 2 ], [1 , 1 ], [2 , 2 ]],
2556+  index = MultiIndex .from_product ([[0.5 , 0.8 ], list ('ab' )]))
2557+  result  =  result .sort_index ()
2558+  assert  result .index .is_lexsorted ()
2559+  assert  result .index .is_monotonic 
2560+ 
2561+  tm .assert_frame_equal (result , expected )
2562+ 
2563+  result  =  DataFrame (
2564+  [[1 , 1 ], [2 , 2 ], [1 , 1 ], [2 , 2 ]],
2565+  index = MultiIndex (levels = [[0.5 , 0.8 ], ['a' , 'b' ]],
2566+  labels = [[0 , 0 , 1 , 1 ], [0 , 1 , 0 , 1 ]]))
2567+  result  =  result .sort_index ()
2568+  assert  result .index .is_lexsorted ()
2569+ 
2570+  tm .assert_frame_equal (result , expected )
2571+ 
2572+  concatted  =  pd .concat ([df , df ], keys = [0.8 , 0.5 ])
2573+  result  =  concatted .sort_index ()
2574+ 
2575+  # this will be monotonic, but not lexsorted! 
2576+  assert  not  result .index .is_lexsorted ()
2577+  assert  result .index .is_monotonic 
2578+ 
2579+  tm .assert_frame_equal (result , expected )
2580+ 
2581+  # 14015 
2582+  df  =  DataFrame ([[1 , 2 ], [6 , 7 ]],
2583+  columns = MultiIndex .from_tuples (
2584+  [(0 , '20160811 12:00:00' ),
2585+  (0 , '20160809 12:00:00' )],
2586+  names = ['l1' , 'Date' ]))
2587+ 
2588+  df .columns .set_levels (pd .to_datetime (df .columns .levels [1 ]),
2589+  level = 1 ,
2590+  inplace = True )
2591+  assert  not  df .columns .is_lexsorted ()
2592+  assert  not  df .columns .is_monotonic 
2593+  result  =  df .sort_index (axis = 1 )
2594+  assert  result .columns .is_lexsorted ()
2595+  assert  result .columns .is_monotonic 
2596+  result  =  df .sort_index (axis = 1 , level = 1 )
2597+  assert  result .columns .is_lexsorted ()
2598+  assert  result .columns .is_monotonic 
2599+ 
2600+  def  test_sort_index_reorder_on_ops (self ):
2601+  # 15687 
2602+  df  =  pd .DataFrame (
2603+  np .random .randn (8 , 2 ),
2604+  index = MultiIndex .from_product (
2605+  [['a' , 'b' ],
2606+  ['big' , 'small' ],
2607+  ['red' , 'blu' ]],
2608+  names = ['letter' , 'size' , 'color' ]),
2609+  columns = ['near' , 'far' ])
2610+  df  =  df .sort_index ()
2611+ 
2612+  def  my_func (group ):
2613+  group .index  =  ['newz' , 'newa' ]
2614+  return  group 
2615+ 
2616+  result  =  df .groupby (level = ['letter' , 'size' ]).apply (
2617+  my_func ).sort_index ()
2618+  expected  =  MultiIndex .from_product (
2619+  [['a' , 'b' ],
2620+  ['big' , 'small' ],
2621+  ['newa' , 'newz' ]],
2622+  names = ['letter' , 'size' , None ])
2623+ 
2624+  tm .assert_index_equal (result .index , expected )
0 commit comments