|
| 1 | +import itertools |
1 | 2 | from warnings import catch_warnings
|
2 | 3 |
|
3 | 4 | import numpy as np
|
4 | 5 | import pytest
|
5 | 6 |
|
6 |
| -from pandas import DataFrame, MultiIndex, Series |
| 7 | +from pandas import DataFrame, Index, MultiIndex, Series |
7 | 8 | from pandas.util import testing as tm
|
8 | 9 |
|
9 | 10 |
|
@@ -175,3 +176,74 @@ def test_get_loc_single_level(self, single_level_multiindex):
|
175 | 176 | index=single_level)
|
176 | 177 | for k in single_level.values:
|
177 | 178 | s[k]
|
| 179 | + |
| 180 | + def test_loc_getitem_int_slice(self): |
| 181 | + # GH 3053 |
| 182 | + # loc should treat integer slices like label slices |
| 183 | + |
| 184 | + index = MultiIndex.from_tuples([t for t in itertools.product( |
| 185 | + [6, 7, 8], ['a', 'b'])]) |
| 186 | + df = DataFrame(np.random.randn(6, 6), index, index) |
| 187 | + result = df.loc[6:8, :] |
| 188 | + expected = df |
| 189 | + tm.assert_frame_equal(result, expected) |
| 190 | + |
| 191 | + index = MultiIndex.from_tuples([t |
| 192 | + for t in itertools.product( |
| 193 | + [10, 20, 30], ['a', 'b'])]) |
| 194 | + df = DataFrame(np.random.randn(6, 6), index, index) |
| 195 | + result = df.loc[20:30, :] |
| 196 | + expected = df.iloc[2:] |
| 197 | + tm.assert_frame_equal(result, expected) |
| 198 | + |
| 199 | + # doc examples |
| 200 | + result = df.loc[10, :] |
| 201 | + expected = df.iloc[0:2] |
| 202 | + expected.index = ['a', 'b'] |
| 203 | + tm.assert_frame_equal(result, expected) |
| 204 | + |
| 205 | + result = df.loc[:, 10] |
| 206 | + # expected = df.ix[:,10] (this fails) |
| 207 | + expected = df[10] |
| 208 | + tm.assert_frame_equal(result, expected) |
| 209 | + |
| 210 | + @pytest.mark.parametrize( |
| 211 | + 'indexer_type_1', |
| 212 | + (list, tuple, set, slice, np.ndarray, Series, Index)) |
| 213 | + @pytest.mark.parametrize( |
| 214 | + 'indexer_type_2', |
| 215 | + (list, tuple, set, slice, np.ndarray, Series, Index)) |
| 216 | + def test_loc_getitem_nested_indexer(self, indexer_type_1, indexer_type_2): |
| 217 | + # GH #19686 |
| 218 | + # .loc should work with nested indexers which can be |
| 219 | + # any list-like objects (see `pandas.api.types.is_list_like`) or slices |
| 220 | + |
| 221 | + def convert_nested_indexer(indexer_type, keys): |
| 222 | + if indexer_type == np.ndarray: |
| 223 | + return np.array(keys) |
| 224 | + if indexer_type == slice: |
| 225 | + return slice(*keys) |
| 226 | + return indexer_type(keys) |
| 227 | + |
| 228 | + a = [10, 20, 30] |
| 229 | + b = [1, 2, 3] |
| 230 | + index = MultiIndex.from_product([a, b]) |
| 231 | + df = DataFrame( |
| 232 | + np.arange(len(index), dtype='int64'), |
| 233 | + index=index, columns=['Data']) |
| 234 | + |
| 235 | + keys = ([10, 20], [2, 3]) |
| 236 | + types = (indexer_type_1, indexer_type_2) |
| 237 | + |
| 238 | + # check indexers with all the combinations of nested objects |
| 239 | + # of all the valid types |
| 240 | + indexer = tuple( |
| 241 | + convert_nested_indexer(indexer_type, k) |
| 242 | + for indexer_type, k in zip(types, keys)) |
| 243 | + |
| 244 | + result = df.loc[indexer, 'Data'] |
| 245 | + expected = Series( |
| 246 | + [1, 2, 4, 5], name='Data', |
| 247 | + index=MultiIndex.from_product(keys)) |
| 248 | + |
| 249 | + tm.assert_series_equal(result, expected) |
0 commit comments