11import itertools
2- from warnings import catch_warnings
32
43import numpy as np
54import pytest
@@ -25,7 +24,6 @@ def frame_random_data_integer_multi_index():
2524 return DataFrame (np .random .randn (6 , 2 ), index = index )
2625
2726
28- @pytest .mark .filterwarnings ("ignore:\\ n.ix:FutureWarning" )
2927class TestMultiIndexLoc :
3028
3129 def test_loc_getitem_series (self ):
@@ -84,54 +82,48 @@ def test_loc_getitem_array(self):
8482 result = x .loc [scalar ]
8583 tm .assert_series_equal (result , expected )
8684
87- def test_loc_multiindex (self ):
85+ def test_loc_multiindex_labels (self ):
86+ df = DataFrame (np .random .randn (3 , 3 ),
87+ columns = [['i' , 'i' , 'j' ], ['A' , 'A' , 'B' ]],
88+ index = [['i' , 'i' , 'j' ], ['X' , 'X' , 'Y' ]])
8889
89- mi_labels = DataFrame (np .random .randn (3 , 3 ),
90- columns = [['i' , 'i' , 'j' ], ['A' , 'A' , 'B' ]],
91- index = [['i' , 'i' , 'j' ], ['X' , 'X' , 'Y' ]])
92-
93- mi_int = DataFrame (np .random .randn (3 , 3 ),
94- columns = [[2 , 2 , 4 ], [6 , 8 , 10 ]],
95- index = [[4 , 4 , 8 ], [8 , 10 , 12 ]])
96-
97- # the first row
98- rs = mi_labels .loc ['i' ]
99- with catch_warnings (record = True ):
100- xp = mi_labels .ix ['i' ]
101- tm .assert_frame_equal (rs , xp )
90+ # the first 2 rows
91+ expected = df .iloc [[0 , 1 ]].droplevel (0 )
92+ result = df .loc ['i' ]
93+ tm .assert_frame_equal (result , expected )
10294
103- # 2nd (last) columns
104- rs = mi_labels .loc [:, 'j' ]
105- with catch_warnings (record = True ):
106- xp = mi_labels .ix [:, 'j' ]
107- tm .assert_frame_equal (rs , xp )
95+ # 2nd (last) column
96+ expected = df .iloc [:, [2 ]].droplevel (0 , axis = 1 )
97+ result = df .loc [:, 'j' ]
98+ tm .assert_frame_equal (result , expected )
10899
109- # corner column
110- rs = mi_labels .loc ['j' ].loc [:, 'j' ]
111- with catch_warnings (record = True ):
112- xp = mi_labels .ix ['j' ].ix [:, 'j' ]
113- tm .assert_frame_equal (rs , xp )
100+ # bottom right corner
101+ expected = df .iloc [[2 ], [2 ]].droplevel (0 ).droplevel (0 , axis = 1 )
102+ result = df .loc ['j' ].loc [:, 'j' ]
103+ tm .assert_frame_equal (result , expected )
114104
115105 # with a tuple
116- rs = mi_labels .loc [('i' , 'X' )]
117- with catch_warnings (record = True ):
118- xp = mi_labels .ix [('i' , 'X' )]
119- tm .assert_frame_equal (rs , xp )
106+ expected = df .iloc [[0 , 1 ]]
107+ result = df .loc [('i' , 'X' )]
108+ tm .assert_frame_equal (result , expected )
109+
110+ def test_loc_multiindex_ints (self ):
111+ df = DataFrame (np .random .randn (3 , 3 ),
112+ columns = [[2 , 2 , 4 ], [6 , 8 , 10 ]],
113+ index = [[4 , 4 , 8 ], [8 , 10 , 12 ]])
114+ expected = df .iloc [[0 , 1 ]].droplevel (0 )
115+ result = df .loc [4 ]
116+ tm .assert_frame_equal (result , expected )
120117
121- rs = mi_int . loc [ 4 ]
122- with catch_warnings ( record = True ):
123- xp = mi_int . ix [ 4 ]
124- tm . assert_frame_equal ( rs , xp )
118+ def test_loc_multiindex_missing_label_raises ( self ):
119+ df = DataFrame ( np . random . randn ( 3 , 3 ),
120+ columns = [[ 2 , 2 , 4 ], [ 6 , 8 , 10 ]],
121+ index = [[ 4 , 4 , 8 ], [ 8 , 10 , 12 ]] )
125122
126- # missing label
127123 with pytest .raises (KeyError , match = r"^2$" ):
128- mi_int .loc [2 ]
129- with catch_warnings (record = True ):
130- # GH 21593
131- with pytest .raises (KeyError , match = r"^2$" ):
132- mi_int .ix [2 ]
124+ df .loc [2 ]
133125
134- def test_loc_multiindex_too_many_dims (self ):
126+ def test_loc_multiindex_too_many_dims_raises (self ):
135127 # GH 14885
136128 s = Series (range (8 ), index = MultiIndex .from_product (
137129 [['a' , 'b' ], ['c' , 'd' ], ['e' , 'f' ]]))
@@ -227,7 +219,6 @@ def test_loc_getitem_int_slice(self):
227219 tm .assert_frame_equal (result , expected )
228220
229221 result = df .loc [:, 10 ]
230- # expected = df.ix[:,10] (this fails)
231222 expected = df [10 ]
232223 tm .assert_frame_equal (result , expected )
233224
@@ -309,17 +300,11 @@ def test_loc_getitem_duplicates_multiindex_missing_indexers(indexer, is_level1,
309300 tm .assert_series_equal (result , expected )
310301
311302
312- @pytest .mark .filterwarnings ("ignore:\\ n.ix:FutureWarning" )
313- @pytest .mark .parametrize ('indexer' , [
314- lambda s : s .loc [[(2000 , 3 , 10 ), (2000 , 3 , 13 )]],
315- lambda s : s .ix [[(2000 , 3 , 10 ), (2000 , 3 , 13 )]]
316- ])
317303def test_series_loc_getitem_fancy (
318- multiindex_year_month_day_dataframe_random_data , indexer ):
304+ multiindex_year_month_day_dataframe_random_data ):
319305 s = multiindex_year_month_day_dataframe_random_data ['A' ]
320306 expected = s .reindex (s .index [49 :51 ])
321-
322- result = indexer (s )
307+ result = s .loc [[(2000 , 3 , 10 ), (2000 , 3 , 13 )]]
323308 tm .assert_series_equal (result , expected )
324309
325310
0 commit comments