@@ -4842,14 +4842,12 @@ def test_loc_listlike(self):
4842
4842
# list of labels
4843
4843
result = self .df .loc [['c' ,'a' ]]
4844
4844
expected = self .df .iloc [[4 ,0 ,1 ,5 ]]
4845
- assert_frame_equal (result , expected )
4846
-
4847
- # ToDo: check_index_type can be True after GH XXX
4845
+ assert_frame_equal (result , expected , check_index_type = True )
4848
4846
4849
4847
result = self .df2 .loc [['a' ,'b' ,'e' ]]
4850
4848
exp_index = pd .CategoricalIndex (list ('aaabbe' ), categories = list ('cabe' ), name = 'B' )
4851
4849
expected = DataFrame ({'A' : [0 ,1 ,5 ,2 ,3 ,np .nan ]}, index = exp_index )
4852
- assert_frame_equal (result , expected , check_index_type = False )
4850
+ assert_frame_equal (result , expected , check_index_type = True )
4853
4851
4854
4852
# element in the categories but not in the values
4855
4853
self .assertRaises (KeyError , lambda : self .df2 .loc ['e' ])
@@ -4859,19 +4857,78 @@ def test_loc_listlike(self):
4859
4857
df .loc ['e' ] = 20
4860
4858
result = df .loc [['a' ,'b' ,'e' ]]
4861
4859
exp_index = pd .CategoricalIndex (list ('aaabbe' ), categories = list ('cabe' ), name = 'B' )
4862
- expected = DataFrame ({'A' : [0 ,1 , 5 , 2 , 3 , 20 ]}, index = exp_index )
4860
+ expected = DataFrame ({'A' : [0 , 1 , 5 , 2 , 3 , 20 ]}, index = exp_index )
4863
4861
assert_frame_equal (result , expected )
4864
4862
4865
4863
df = self .df2 .copy ()
4866
4864
result = df .loc [['a' ,'b' ,'e' ]]
4867
- expected = DataFrame ({'A' : [0 ,1 ,5 ,2 ,3 ,np .nan ],
4868
- 'B' : Series (list ('aaabbe' )).astype ('category' ,categories = list ('cabe' )) }).set_index ('B' )
4869
- assert_frame_equal (result , expected , check_index_type = False )
4870
-
4865
+ exp_index = pd .CategoricalIndex (list ('aaabbe' ), categories = list ('cabe' ), name = 'B' )
4866
+ expected = DataFrame ({'A' : [0 , 1 , 5 , 2 , 3 , np .nan ]}, index = exp_index )
4867
+ assert_frame_equal (result , expected , check_index_type = True )
4871
4868
4872
4869
# not all labels in the categories
4873
4870
self .assertRaises (KeyError , lambda : self .df2 .loc [['a' ,'d' ]])
4874
4871
4872
+ def test_loc_listlike_dtypes (self ):
4873
+ # GH 11586
4874
+
4875
+ # unique categories and codes
4876
+ index = pd .CategoricalIndex (['a' , 'b' , 'c' ])
4877
+ df = DataFrame ({'A' : [1 , 2 , 3 ], 'B' : [4 , 5 , 6 ]}, index = index )
4878
+
4879
+ # unique slice
4880
+ res = df .loc [['a' , 'b' ]]
4881
+ exp = DataFrame ({'A' : [1 , 2 ], 'B' : [4 , 5 ]}, index = pd .CategoricalIndex (['a' , 'b' ]))
4882
+ tm .assert_frame_equal (res , exp , check_index_type = True )
4883
+
4884
+ # duplicated slice
4885
+ res = df .loc [['a' , 'a' , 'b' ]]
4886
+ exp = DataFrame ({'A' : [1 , 1 , 2 ], 'B' : [4 , 4 , 5 ]}, index = pd .CategoricalIndex (['a' , 'a' , 'b' ]))
4887
+ tm .assert_frame_equal (res , exp , check_index_type = True )
4888
+
4889
+ with tm .assertRaisesRegexp (KeyError , 'a list-indexer must only include values that are in the categories' ):
4890
+ df .loc [['a' , 'x' ]]
4891
+
4892
+ # duplicated categories and codes
4893
+ index = pd .CategoricalIndex (['a' , 'b' , 'a' ])
4894
+ df = DataFrame ({'A' : [1 , 2 , 3 ], 'B' : [4 , 5 , 6 ]}, index = index )
4895
+
4896
+ # unique slice
4897
+ res = df .loc [['a' , 'b' ]]
4898
+ exp = DataFrame ({'A' : [1 , 3 , 2 ], 'B' : [4 , 6 , 5 ]}, index = pd .CategoricalIndex (['a' , 'a' , 'b' ]))
4899
+ tm .assert_frame_equal (res , exp , check_index_type = True )
4900
+
4901
+ # duplicated slice
4902
+ res = df .loc [['a' , 'a' , 'b' ]]
4903
+ exp = DataFrame ({'A' : [1 , 3 , 1 , 3 , 2 ], 'B' : [4 , 6 , 4 , 6 , 5 ]}, index = pd .CategoricalIndex (['a' , 'a' , 'a' , 'a' , 'b' ]))
4904
+ tm .assert_frame_equal (res , exp , check_index_type = True )
4905
+
4906
+ with tm .assertRaisesRegexp (KeyError , 'a list-indexer must only include values that are in the categories' ):
4907
+ df .loc [['a' , 'x' ]]
4908
+
4909
+ # contains unused category
4910
+ index = pd .CategoricalIndex (['a' , 'b' , 'a' , 'c' ], categories = list ('abcde' ))
4911
+ df = DataFrame ({'A' : [1 , 2 , 3 , 4 ], 'B' : [5 , 6 , 7 , 8 ]}, index = index )
4912
+
4913
+ res = df .loc [['a' , 'b' ]]
4914
+ exp = DataFrame ({'A' : [1 , 3 , 2 ], 'B' : [5 , 7 , 6 ]},
4915
+ index = pd .CategoricalIndex (['a' , 'a' , 'b' ], categories = list ('abcde' )))
4916
+ tm .assert_frame_equal (res , exp , check_index_type = True )
4917
+
4918
+ res = df .loc [['a' , 'e' ]]
4919
+ exp = DataFrame ({'A' : [1 , 3 , np .nan ], 'B' : [5 , 7 , np .nan ]},
4920
+ index = pd .CategoricalIndex (['a' , 'a' , 'e' ], categories = list ('abcde' )))
4921
+ tm .assert_frame_equal (res , exp , check_index_type = True )
4922
+
4923
+ # duplicated slice
4924
+ res = df .loc [['a' , 'a' , 'b' ]]
4925
+ exp = DataFrame ({'A' : [1 , 3 , 1 , 3 , 2 ], 'B' : [5 , 7 , 5 , 7 , 6 ]},
4926
+ index = pd .CategoricalIndex (['a' , 'a' , 'a' , 'a' , 'b' ], categories = list ('abcde' )))
4927
+ tm .assert_frame_equal (res , exp , check_index_type = True )
4928
+
4929
+ with tm .assertRaisesRegexp (KeyError , 'a list-indexer must only include values that are in the categories' ):
4930
+ df .loc [['a' , 'x' ]]
4931
+
4875
4932
def test_read_only_source (self ):
4876
4933
# GH 10043
4877
4934
rw_array = np .eye (10 )
@@ -4898,22 +4955,22 @@ def test_reindexing(self):
4898
4955
result = self .df2 .reindex (['a' ,'b' ,'e' ])
4899
4956
expected = DataFrame ({'A' : [0 ,1 ,5 ,2 ,3 ,np .nan ],
4900
4957
'B' : Series (list ('aaabbe' )) }).set_index ('B' )
4901
- assert_frame_equal (result , expected )
4958
+ assert_frame_equal (result , expected , check_index_type = True )
4902
4959
4903
4960
result = self .df2 .reindex (['a' ,'b' ])
4904
4961
expected = DataFrame ({'A' : [0 ,1 ,5 ,2 ,3 ],
4905
4962
'B' : Series (list ('aaabb' )) }).set_index ('B' )
4906
- assert_frame_equal (result , expected )
4963
+ assert_frame_equal (result , expected , check_index_type = True )
4907
4964
4908
4965
result = self .df2 .reindex (['e' ])
4909
4966
expected = DataFrame ({'A' : [np .nan ],
4910
4967
'B' : Series (['e' ]) }).set_index ('B' )
4911
- assert_frame_equal (result , expected )
4968
+ assert_frame_equal (result , expected , check_index_type = True )
4912
4969
4913
4970
result = self .df2 .reindex (['d' ])
4914
4971
expected = DataFrame ({'A' : [np .nan ],
4915
4972
'B' : Series (['d' ]) }).set_index ('B' )
4916
- assert_frame_equal (result , expected )
4973
+ assert_frame_equal (result , expected , check_index_type = True )
4917
4974
4918
4975
# since we are actually reindexing with a Categorical
4919
4976
# then return a Categorical
@@ -4922,38 +4979,38 @@ def test_reindexing(self):
4922
4979
result = self .df2 .reindex (pd .Categorical (['a' ,'d' ],categories = cats ))
4923
4980
expected = DataFrame ({'A' : [0 ,1 ,5 ,np .nan ],
4924
4981
'B' : Series (list ('aaad' )).astype ('category' ,categories = cats ) }).set_index ('B' )
4925
- assert_frame_equal (result , expected )
4982
+ assert_frame_equal (result , expected , check_index_type = True )
4926
4983
4927
4984
result = self .df2 .reindex (pd .Categorical (['a' ],categories = cats ))
4928
4985
expected = DataFrame ({'A' : [0 ,1 ,5 ],
4929
4986
'B' : Series (list ('aaa' )).astype ('category' ,categories = cats ) }).set_index ('B' )
4930
- assert_frame_equal (result , expected )
4987
+ assert_frame_equal (result , expected , check_index_type = True )
4931
4988
4932
4989
result = self .df2 .reindex (['a' ,'b' ,'e' ])
4933
4990
expected = DataFrame ({'A' : [0 ,1 ,5 ,2 ,3 ,np .nan ],
4934
4991
'B' : Series (list ('aaabbe' )) }).set_index ('B' )
4935
- assert_frame_equal (result , expected )
4992
+ assert_frame_equal (result , expected , check_index_type = True )
4936
4993
4937
4994
result = self .df2 .reindex (['a' ,'b' ])
4938
4995
expected = DataFrame ({'A' : [0 ,1 ,5 ,2 ,3 ],
4939
4996
'B' : Series (list ('aaabb' )) }).set_index ('B' )
4940
- assert_frame_equal (result , expected )
4997
+ assert_frame_equal (result , expected , check_index_type = True )
4941
4998
4942
4999
result = self .df2 .reindex (['e' ])
4943
5000
expected = DataFrame ({'A' : [np .nan ],
4944
5001
'B' : Series (['e' ]) }).set_index ('B' )
4945
- assert_frame_equal (result , expected )
5002
+ assert_frame_equal (result , expected , check_index_type = True )
4946
5003
4947
5004
# give back the type of categorical that we received
4948
5005
result = self .df2 .reindex (pd .Categorical (['a' ,'d' ],categories = cats ,ordered = True ))
4949
5006
expected = DataFrame ({'A' : [0 ,1 ,5 ,np .nan ],
4950
5007
'B' : Series (list ('aaad' )).astype ('category' ,categories = cats ,ordered = True ) }).set_index ('B' )
4951
- assert_frame_equal (result , expected )
5008
+ assert_frame_equal (result , expected , check_index_type = True )
4952
5009
4953
5010
result = self .df2 .reindex (pd .Categorical (['a' ,'d' ],categories = ['a' ,'d' ]))
4954
5011
expected = DataFrame ({'A' : [0 ,1 ,5 ,np .nan ],
4955
5012
'B' : Series (list ('aaad' )).astype ('category' ,categories = ['a' ,'d' ]) }).set_index ('B' )
4956
- assert_frame_equal (result , expected )
5013
+ assert_frame_equal (result , expected , check_index_type = True )
4957
5014
4958
5015
# passed duplicate indexers are not allowed
4959
5016
self .assertRaises (ValueError , lambda : self .df2 .reindex (['a' ,'a' ]))
0 commit comments