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