15
15
Timestamp ,
16
16
)
17
17
from pandas .api .types import CategoricalDtype as CDT
18
- from pandas .util import testing as tm
19
- from pandas .util .testing import assert_frame_equal , assert_series_equal
18
+ import pandas .util .testing as tm
20
19
21
20
22
21
class TestCategoricalIndex :
@@ -52,7 +51,7 @@ def test_loc_scalar(self):
52
51
expected = DataFrame (
53
52
{"A" : [0 , 1 , 5 ], "B" : (Series (list ("aaa" )).astype (CDT (list ("cab" ))))}
54
53
).set_index ("B" )
55
- assert_frame_equal (result , expected )
54
+ tm . assert_frame_equal (result , expected )
56
55
57
56
df = self .df .copy ()
58
57
df .loc ["a" ] = 20
@@ -62,7 +61,7 @@ def test_loc_scalar(self):
62
61
"B" : (Series (list ("aabbca" )).astype (CDT (list ("cab" )))),
63
62
}
64
63
).set_index ("B" )
65
- assert_frame_equal (df , expected )
64
+ tm . assert_frame_equal (df , expected )
66
65
67
66
# value not in the categories
68
67
with pytest .raises (KeyError , match = r"^'d'$" ):
@@ -329,12 +328,12 @@ def test_loc_listlike(self):
329
328
# list of labels
330
329
result = self .df .loc [["c" , "a" ]]
331
330
expected = self .df .iloc [[4 , 0 , 1 , 5 ]]
332
- assert_frame_equal (result , expected , check_index_type = True )
331
+ tm . assert_frame_equal (result , expected , check_index_type = True )
333
332
334
333
result = self .df2 .loc [["a" , "b" , "e" ]]
335
334
exp_index = CategoricalIndex (list ("aaabbe" ), categories = list ("cabe" ), name = "B" )
336
335
expected = DataFrame ({"A" : [0 , 1 , 5 , 2 , 3 , np .nan ]}, index = exp_index )
337
- assert_frame_equal (result , expected , check_index_type = True )
336
+ tm . assert_frame_equal (result , expected , check_index_type = True )
338
337
339
338
# element in the categories but not in the values
340
339
with pytest .raises (KeyError , match = r"^'e'$" ):
@@ -346,13 +345,13 @@ def test_loc_listlike(self):
346
345
result = df .loc [["a" , "b" , "e" ]]
347
346
exp_index = CategoricalIndex (list ("aaabbe" ), categories = list ("cabe" ), name = "B" )
348
347
expected = DataFrame ({"A" : [0 , 1 , 5 , 2 , 3 , 20 ]}, index = exp_index )
349
- assert_frame_equal (result , expected )
348
+ tm . assert_frame_equal (result , expected )
350
349
351
350
df = self .df2 .copy ()
352
351
result = df .loc [["a" , "b" , "e" ]]
353
352
exp_index = CategoricalIndex (list ("aaabbe" ), categories = list ("cabe" ), name = "B" )
354
353
expected = DataFrame ({"A" : [0 , 1 , 5 , 2 , 3 , np .nan ]}, index = exp_index )
355
- assert_frame_equal (result , expected , check_index_type = True )
354
+ tm . assert_frame_equal (result , expected , check_index_type = True )
356
355
357
356
# not all labels in the categories
358
357
with pytest .raises (
@@ -474,7 +473,7 @@ def test_getitem_with_listlike(self):
474
473
)
475
474
dummies = pd .get_dummies (cats )
476
475
result = dummies [[c for c in dummies .columns ]]
477
- assert_frame_equal (result , expected )
476
+ tm . assert_frame_equal (result , expected )
478
477
479
478
def test_setitem_listlike (self ):
480
479
@@ -500,18 +499,18 @@ def test_ix_categorical_index(self):
500
499
cdf .columns = CategoricalIndex (df .columns )
501
500
502
501
expect = Series (df .loc ["A" , :], index = cdf .columns , name = "A" )
503
- assert_series_equal (cdf .loc ["A" , :], expect )
502
+ tm . assert_series_equal (cdf .loc ["A" , :], expect )
504
503
505
504
expect = Series (df .loc [:, "X" ], index = cdf .index , name = "X" )
506
- assert_series_equal (cdf .loc [:, "X" ], expect )
505
+ tm . assert_series_equal (cdf .loc [:, "X" ], expect )
507
506
508
507
exp_index = CategoricalIndex (list ("AB" ), categories = ["A" , "B" , "C" ])
509
508
expect = DataFrame (df .loc [["A" , "B" ], :], columns = cdf .columns , index = exp_index )
510
- assert_frame_equal (cdf .loc [["A" , "B" ], :], expect )
509
+ tm . assert_frame_equal (cdf .loc [["A" , "B" ], :], expect )
511
510
512
511
exp_columns = CategoricalIndex (list ("XY" ), categories = ["X" , "Y" , "Z" ])
513
512
expect = DataFrame (df .loc [:, ["X" , "Y" ]], index = cdf .index , columns = exp_columns )
514
- assert_frame_equal (cdf .loc [:, ["X" , "Y" ]], expect )
513
+ tm . assert_frame_equal (cdf .loc [:, ["X" , "Y" ]], expect )
515
514
516
515
# non-unique
517
516
df = DataFrame (np .random .randn (3 , 3 ), index = list ("ABA" ), columns = list ("XYX" ))
@@ -521,25 +520,25 @@ def test_ix_categorical_index(self):
521
520
522
521
exp_index = CategoricalIndex (list ("AA" ), categories = ["A" , "B" ])
523
522
expect = DataFrame (df .loc ["A" , :], columns = cdf .columns , index = exp_index )
524
- assert_frame_equal (cdf .loc ["A" , :], expect )
523
+ tm . assert_frame_equal (cdf .loc ["A" , :], expect )
525
524
526
525
exp_columns = CategoricalIndex (list ("XX" ), categories = ["X" , "Y" ])
527
526
expect = DataFrame (df .loc [:, "X" ], index = cdf .index , columns = exp_columns )
528
- assert_frame_equal (cdf .loc [:, "X" ], expect )
527
+ tm . assert_frame_equal (cdf .loc [:, "X" ], expect )
529
528
530
529
expect = DataFrame (
531
530
df .loc [["A" , "B" ], :],
532
531
columns = cdf .columns ,
533
532
index = CategoricalIndex (list ("AAB" )),
534
533
)
535
- assert_frame_equal (cdf .loc [["A" , "B" ], :], expect )
534
+ tm . assert_frame_equal (cdf .loc [["A" , "B" ], :], expect )
536
535
537
536
expect = DataFrame (
538
537
df .loc [:, ["X" , "Y" ]],
539
538
index = cdf .index ,
540
539
columns = CategoricalIndex (list ("XXY" )),
541
540
)
542
- assert_frame_equal (cdf .loc [:, ["X" , "Y" ]], expect )
541
+ tm . assert_frame_equal (cdf .loc [:, ["X" , "Y" ]], expect )
543
542
544
543
def test_read_only_source (self ):
545
544
# GH 10043
@@ -550,15 +549,15 @@ def test_read_only_source(self):
550
549
ro_array .setflags (write = False )
551
550
ro_df = DataFrame (ro_array )
552
551
553
- assert_frame_equal (rw_df .iloc [[1 , 2 , 3 ]], ro_df .iloc [[1 , 2 , 3 ]])
554
- assert_frame_equal (rw_df .iloc [[1 ]], ro_df .iloc [[1 ]])
555
- assert_series_equal (rw_df .iloc [1 ], ro_df .iloc [1 ])
556
- assert_frame_equal (rw_df .iloc [1 :3 ], ro_df .iloc [1 :3 ])
552
+ tm . assert_frame_equal (rw_df .iloc [[1 , 2 , 3 ]], ro_df .iloc [[1 , 2 , 3 ]])
553
+ tm . assert_frame_equal (rw_df .iloc [[1 ]], ro_df .iloc [[1 ]])
554
+ tm . assert_series_equal (rw_df .iloc [1 ], ro_df .iloc [1 ])
555
+ tm . assert_frame_equal (rw_df .iloc [1 :3 ], ro_df .iloc [1 :3 ])
557
556
558
- assert_frame_equal (rw_df .loc [[1 , 2 , 3 ]], ro_df .loc [[1 , 2 , 3 ]])
559
- assert_frame_equal (rw_df .loc [[1 ]], ro_df .loc [[1 ]])
560
- assert_series_equal (rw_df .loc [1 ], ro_df .loc [1 ])
561
- assert_frame_equal (rw_df .loc [1 :3 ], ro_df .loc [1 :3 ])
557
+ tm . assert_frame_equal (rw_df .loc [[1 , 2 , 3 ]], ro_df .loc [[1 , 2 , 3 ]])
558
+ tm . assert_frame_equal (rw_df .loc [[1 ]], ro_df .loc [[1 ]])
559
+ tm . assert_series_equal (rw_df .loc [1 ], ro_df .loc [1 ])
560
+ tm . assert_frame_equal (rw_df .loc [1 :3 ], ro_df .loc [1 :3 ])
562
561
563
562
def test_reindexing (self ):
564
563
df = DataFrame (
@@ -574,19 +573,19 @@ def test_reindexing(self):
574
573
expected = DataFrame ({"A" : [0 , 1 , np .nan ], "B" : Series (list ("abe" ))}).set_index (
575
574
"B"
576
575
)
577
- assert_frame_equal (result , expected , check_index_type = True )
576
+ tm . assert_frame_equal (result , expected , check_index_type = True )
578
577
579
578
result = df .reindex (["a" , "b" ])
580
579
expected = DataFrame ({"A" : [0 , 1 ], "B" : Series (list ("ab" ))}).set_index ("B" )
581
- assert_frame_equal (result , expected , check_index_type = True )
580
+ tm . assert_frame_equal (result , expected , check_index_type = True )
582
581
583
582
result = df .reindex (["e" ])
584
583
expected = DataFrame ({"A" : [np .nan ], "B" : Series (["e" ])}).set_index ("B" )
585
- assert_frame_equal (result , expected , check_index_type = True )
584
+ tm . assert_frame_equal (result , expected , check_index_type = True )
586
585
587
586
result = df .reindex (["d" ])
588
587
expected = DataFrame ({"A" : [np .nan ], "B" : Series (["d" ])}).set_index ("B" )
589
- assert_frame_equal (result , expected , check_index_type = True )
588
+ tm . assert_frame_equal (result , expected , check_index_type = True )
590
589
591
590
# since we are actually reindexing with a Categorical
592
591
# then return a Categorical
@@ -596,40 +595,40 @@ def test_reindexing(self):
596
595
expected = DataFrame (
597
596
{"A" : [0 , np .nan ], "B" : Series (list ("ae" )).astype (CDT (cats ))}
598
597
).set_index ("B" )
599
- assert_frame_equal (result , expected , check_index_type = True )
598
+ tm . assert_frame_equal (result , expected , check_index_type = True )
600
599
601
600
result = df .reindex (Categorical (["a" ], categories = cats ))
602
601
expected = DataFrame (
603
602
{"A" : [0 ], "B" : Series (list ("a" )).astype (CDT (cats ))}
604
603
).set_index ("B" )
605
- assert_frame_equal (result , expected , check_index_type = True )
604
+ tm . assert_frame_equal (result , expected , check_index_type = True )
606
605
607
606
result = df .reindex (["a" , "b" , "e" ])
608
607
expected = DataFrame ({"A" : [0 , 1 , np .nan ], "B" : Series (list ("abe" ))}).set_index (
609
608
"B"
610
609
)
611
- assert_frame_equal (result , expected , check_index_type = True )
610
+ tm . assert_frame_equal (result , expected , check_index_type = True )
612
611
613
612
result = df .reindex (["a" , "b" ])
614
613
expected = DataFrame ({"A" : [0 , 1 ], "B" : Series (list ("ab" ))}).set_index ("B" )
615
- assert_frame_equal (result , expected , check_index_type = True )
614
+ tm . assert_frame_equal (result , expected , check_index_type = True )
616
615
617
616
result = df .reindex (["e" ])
618
617
expected = DataFrame ({"A" : [np .nan ], "B" : Series (["e" ])}).set_index ("B" )
619
- assert_frame_equal (result , expected , check_index_type = True )
618
+ tm . assert_frame_equal (result , expected , check_index_type = True )
620
619
621
620
# give back the type of categorical that we received
622
621
result = df .reindex (Categorical (["a" , "e" ], categories = cats , ordered = True ))
623
622
expected = DataFrame (
624
623
{"A" : [0 , np .nan ], "B" : Series (list ("ae" )).astype (CDT (cats , ordered = True ))}
625
624
).set_index ("B" )
626
- assert_frame_equal (result , expected , check_index_type = True )
625
+ tm . assert_frame_equal (result , expected , check_index_type = True )
627
626
628
627
result = df .reindex (Categorical (["a" , "d" ], categories = ["a" , "d" ]))
629
628
expected = DataFrame (
630
629
{"A" : [0 , np .nan ], "B" : Series (list ("ad" )).astype (CDT (["a" , "d" ]))}
631
630
).set_index ("B" )
632
- assert_frame_equal (result , expected , check_index_type = True )
631
+ tm . assert_frame_equal (result , expected , check_index_type = True )
633
632
634
633
# passed duplicate indexers are not allowed
635
634
msg = "cannot reindex from a duplicate axis"
@@ -661,7 +660,7 @@ def test_loc_slice(self):
661
660
662
661
# result = df.loc[1:5]
663
662
# expected = df.iloc[[1,2,3,4]]
664
- # assert_frame_equal(result, expected)
663
+ # tm. assert_frame_equal(result, expected)
665
664
666
665
def test_loc_and_at_with_categorical_index (self ):
667
666
# GH 20629
@@ -681,19 +680,19 @@ def test_boolean_selection(self):
681
680
682
681
result = df3 [df3 .index == "a" ]
683
682
expected = df3 .iloc [[]]
684
- assert_frame_equal (result , expected )
683
+ tm . assert_frame_equal (result , expected )
685
684
686
685
result = df4 [df4 .index == "a" ]
687
686
expected = df4 .iloc [[]]
688
- assert_frame_equal (result , expected )
687
+ tm . assert_frame_equal (result , expected )
689
688
690
689
result = df3 [df3 .index == 1 ]
691
690
expected = df3 .iloc [[0 , 1 , 3 ]]
692
- assert_frame_equal (result , expected )
691
+ tm . assert_frame_equal (result , expected )
693
692
694
693
result = df4 [df4 .index == 1 ]
695
694
expected = df4 .iloc [[0 , 1 , 3 ]]
696
- assert_frame_equal (result , expected )
695
+ tm . assert_frame_equal (result , expected )
697
696
698
697
# since we have an ordered categorical
699
698
@@ -703,11 +702,11 @@ def test_boolean_selection(self):
703
702
# name='B')
704
703
result = df3 [df3 .index < 2 ]
705
704
expected = df3 .iloc [[4 ]]
706
- assert_frame_equal (result , expected )
705
+ tm . assert_frame_equal (result , expected )
707
706
708
707
result = df3 [df3 .index > 1 ]
709
708
expected = df3 .iloc [[]]
710
- assert_frame_equal (result , expected )
709
+ tm . assert_frame_equal (result , expected )
711
710
712
711
# unordered
713
712
# cannot be compared
0 commit comments