1
1
import itertools
2
- from warnings import catch_warnings
3
2
4
3
import numpy as np
5
4
import pytest
@@ -25,7 +24,6 @@ def frame_random_data_integer_multi_index():
25
24
return DataFrame (np .random .randn (6 , 2 ), index = index )
26
25
27
26
28
- @pytest .mark .filterwarnings ("ignore:\\ n.ix:FutureWarning" )
29
27
class TestMultiIndexLoc :
30
28
31
29
def test_loc_getitem_series (self ):
@@ -84,54 +82,48 @@ def test_loc_getitem_array(self):
84
82
result = x .loc [scalar ]
85
83
tm .assert_series_equal (result , expected )
86
84
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' ]])
88
89
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 )
102
94
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 )
108
99
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 )
114
104
115
105
# 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 )
120
117
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 ]] )
125
122
126
- # missing label
127
123
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 ]
133
125
134
- def test_loc_multiindex_too_many_dims (self ):
126
+ def test_loc_multiindex_too_many_dims_raises (self ):
135
127
# GH 14885
136
128
s = Series (range (8 ), index = MultiIndex .from_product (
137
129
[['a' , 'b' ], ['c' , 'd' ], ['e' , 'f' ]]))
@@ -227,7 +219,6 @@ def test_loc_getitem_int_slice(self):
227
219
tm .assert_frame_equal (result , expected )
228
220
229
221
result = df .loc [:, 10 ]
230
- # expected = df.ix[:,10] (this fails)
231
222
expected = df [10 ]
232
223
tm .assert_frame_equal (result , expected )
233
224
@@ -309,17 +300,11 @@ def test_loc_getitem_duplicates_multiindex_missing_indexers(indexer, is_level1,
309
300
tm .assert_series_equal (result , expected )
310
301
311
302
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
- ])
317
303
def test_series_loc_getitem_fancy (
318
- multiindex_year_month_day_dataframe_random_data , indexer ):
304
+ multiindex_year_month_day_dataframe_random_data ):
319
305
s = multiindex_year_month_day_dataframe_random_data ['A' ]
320
306
expected = s .reindex (s .index [49 :51 ])
321
-
322
- result = indexer (s )
307
+ result = s .loc [[(2000 , 3 , 10 ), (2000 , 3 , 13 )]]
323
308
tm .assert_series_equal (result , expected )
324
309
325
310
0 commit comments