@@ -46,6 +46,18 @@ def setup_method(self, method):
46
46
"B" : (Series ([1 , 1 , 2 , 1 , 3 , 2 ]).astype (CDT ([3 , 2 , 1 ], ordered = False ))),
47
47
}
48
48
).set_index ("B" )
49
+ self .df5 = DataFrame (
50
+ {
51
+ "A" : np .arange (3 , dtype = "int64" ),
52
+ "B" : Series (list ("abc" )).astype (CDT (list ("cabe" ))),
53
+ }
54
+ ).set_index ("B" )
55
+ self .df6 = DataFrame (
56
+ {
57
+ "A" : np .arange (3 , dtype = "int64" ),
58
+ "B" : (Series ([1 , 3 , 2 ]).astype (CDT ([3 , 2 , 1 ], ordered = False ))),
59
+ }
60
+ ).set_index ("B" )
49
61
50
62
def test_loc_scalar (self ):
51
63
result = self .df .loc ["a" ]
@@ -564,89 +576,82 @@ def test_reindexing(self):
564
576
565
577
# reindexing
566
578
# convert to a regular index
567
- result = self .df2 .reindex (["a" , "b" , "e" ])
568
- expected = DataFrame (
569
- { "A" : [ 0 , 1 , 5 , 2 , 3 , np . nan ], "B" : Series ( list ( "aaabbe" ))}
570
- ). set_index ( "B" )
579
+ result = self .df5 .reindex (["a" , "b" , "e" ])
580
+ expected = DataFrame ({ "A" : [ 0 , 1 , np . nan ], "B" : Series ( list ( "abe" ))}). set_index (
581
+ "B"
582
+ )
571
583
assert_frame_equal (result , expected , check_index_type = True )
572
584
573
- result = self .df2 .reindex (["a" , "b" ])
574
- expected = DataFrame (
575
- {"A" : [0 , 1 , 5 , 2 , 3 ], "B" : Series (list ("aaabb" ))}
576
- ).set_index ("B" )
585
+ result = self .df5 .reindex (["a" , "b" ])
586
+ expected = DataFrame ({"A" : [0 , 1 ], "B" : Series (list ("ab" ))}).set_index ("B" )
577
587
assert_frame_equal (result , expected , check_index_type = True )
578
588
579
- result = self .df2 .reindex (["e" ])
589
+ result = self .df5 .reindex (["e" ])
580
590
expected = DataFrame ({"A" : [np .nan ], "B" : Series (["e" ])}).set_index ("B" )
581
591
assert_frame_equal (result , expected , check_index_type = True )
582
592
583
- result = self .df2 .reindex (["d" ])
593
+ result = self .df5 .reindex (["d" ])
584
594
expected = DataFrame ({"A" : [np .nan ], "B" : Series (["d" ])}).set_index ("B" )
585
595
assert_frame_equal (result , expected , check_index_type = True )
586
596
587
597
# since we are actually reindexing with a Categorical
588
598
# then return a Categorical
589
599
cats = list ("cabe" )
590
600
591
- result = self .df2 .reindex (Categorical (["a" , "d " ], categories = cats ))
601
+ result = self .df5 .reindex (Categorical (["a" , "e " ], categories = cats ))
592
602
expected = DataFrame (
593
- {"A" : [0 , 1 , 5 , np .nan ], "B" : Series (list ("aaad " )).astype (CDT (cats ))}
603
+ {"A" : [0 , np .nan ], "B" : Series (list ("ae " )).astype (CDT (cats ))}
594
604
).set_index ("B" )
595
605
assert_frame_equal (result , expected , check_index_type = True )
596
606
597
- result = self .df2 .reindex (Categorical (["a" ], categories = cats ))
607
+ result = self .df5 .reindex (Categorical (["a" ], categories = cats ))
598
608
expected = DataFrame (
599
- {"A" : [0 , 1 , 5 ], "B" : Series (list ("aaa " )).astype (CDT (cats ))}
609
+ {"A" : [0 ], "B" : Series (list ("a " )).astype (CDT (cats ))}
600
610
).set_index ("B" )
601
611
assert_frame_equal (result , expected , check_index_type = True )
602
612
603
- result = self .df2 .reindex (["a" , "b" , "e" ])
604
- expected = DataFrame (
605
- { "A" : [ 0 , 1 , 5 , 2 , 3 , np . nan ], "B" : Series ( list ( "aaabbe" ))}
606
- ). set_index ( "B" )
613
+ result = self .df5 .reindex (["a" , "b" , "e" ])
614
+ expected = DataFrame ({ "A" : [ 0 , 1 , np . nan ], "B" : Series ( list ( "abe" ))}). set_index (
615
+ "B"
616
+ )
607
617
assert_frame_equal (result , expected , check_index_type = True )
608
618
609
- result = self .df2 .reindex (["a" , "b" ])
610
- expected = DataFrame (
611
- {"A" : [0 , 1 , 5 , 2 , 3 ], "B" : Series (list ("aaabb" ))}
612
- ).set_index ("B" )
619
+ result = self .df5 .reindex (["a" , "b" ])
620
+ expected = DataFrame ({"A" : [0 , 1 ], "B" : Series (list ("ab" ))}).set_index ("B" )
613
621
assert_frame_equal (result , expected , check_index_type = True )
614
622
615
- result = self .df2 .reindex (["e" ])
623
+ result = self .df5 .reindex (["e" ])
616
624
expected = DataFrame ({"A" : [np .nan ], "B" : Series (["e" ])}).set_index ("B" )
617
625
assert_frame_equal (result , expected , check_index_type = True )
618
626
619
627
# give back the type of categorical that we received
620
- result = self .df2 .reindex (
621
- Categorical (["a" , "d " ], categories = cats , ordered = True )
628
+ result = self .df5 .reindex (
629
+ Categorical (["a" , "e " ], categories = cats , ordered = True )
622
630
)
623
631
expected = DataFrame (
624
- {
625
- "A" : [0 , 1 , 5 , np .nan ],
626
- "B" : Series (list ("aaad" )).astype (CDT (cats , ordered = True )),
627
- }
632
+ {"A" : [0 , np .nan ], "B" : Series (list ("ae" )).astype (CDT (cats , ordered = True ))}
628
633
).set_index ("B" )
629
634
assert_frame_equal (result , expected , check_index_type = True )
630
635
631
- result = self .df2 .reindex (Categorical (["a" , "d" ], categories = ["a" , "d" ]))
636
+ result = self .df5 .reindex (Categorical (["a" , "d" ], categories = ["a" , "d" ]))
632
637
expected = DataFrame (
633
- {"A" : [0 , 1 , 5 , np .nan ], "B" : Series (list ("aaad " )).astype (CDT (["a" , "d" ]))}
638
+ {"A" : [0 , np .nan ], "B" : Series (list ("ad " )).astype (CDT (["a" , "d" ]))}
634
639
).set_index ("B" )
635
640
assert_frame_equal (result , expected , check_index_type = True )
636
641
637
642
# passed duplicate indexers are not allowed
638
- msg = "cannot reindex with a non-unique indexer "
643
+ msg = "cannot reindex from a duplicate axis "
639
644
with pytest .raises (ValueError , match = msg ):
640
- self .df2 .reindex (["a" , "a " ])
645
+ self .df2 .reindex (["a" , "b " ])
641
646
642
647
# args NotImplemented ATM
643
648
msg = r"argument {} is not implemented for CategoricalIndex\.reindex"
644
649
with pytest .raises (NotImplementedError , match = msg .format ("method" )):
645
- self .df2 .reindex (["a" ], method = "ffill" )
650
+ self .df5 .reindex (["a" ], method = "ffill" )
646
651
with pytest .raises (NotImplementedError , match = msg .format ("level" )):
647
- self .df2 .reindex (["a" ], level = 1 )
652
+ self .df5 .reindex (["a" ], level = 1 )
648
653
with pytest .raises (NotImplementedError , match = msg .format ("limit" )):
649
- self .df2 .reindex (["a" ], limit = 2 )
654
+ self .df5 .reindex (["a" ], limit = 2 )
650
655
651
656
def test_loc_slice (self ):
652
657
# slicing
0 commit comments