Skip to content

Commit 1d8e4bf

Browse files
committed
Merge pull request #7350 from jreback/mi_index
BUG: Bug in .loc with a list of indexers on a single-multi index level (that is not nested) GH7349
2 parents 8fed790 + 946f9cd commit 1d8e4bf

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

doc/source/v0.14.1.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ users upgrade to this version.
2121

2222
- :ref:`Deprecations <whatsnew_0141.deprecations>`
2323

24-
- :ref:`Known Issues <whatsnew_0141.knownissues>`
25-
2624
- :ref:`Bug Fixes <whatsnew_0141.bug_fixes>`
2725

2826
.. _whatsnew_0141.api:
@@ -36,22 +34,21 @@ API changes
3634
containing ``NaN`` values - now also has ``dtype=object`` instead of
3735
``float`` (:issue:`7242`)
3836

37+
- `StringMethods`` now work on empty Series (:issue:`7242`)
38+
3939
.. _whatsnew_0141.prior_deprecations:
4040

4141
Prior Version Deprecations/Changes
4242
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4343

44-
There are prior version deprecations that are taking effect as of 0.14.1.
44+
There are no prior version deprecations that are taking effect as of 0.14.1.
4545

4646
.. _whatsnew_0141.deprecations:
4747

4848
Deprecations
4949
~~~~~~~~~~~~
5050

51-
.. _whatsnew_0141.knownissues:
52-
53-
Known Issues
54-
~~~~~~~~~~~~
51+
There are no deprecations that are taking effect as of 0.14.1.
5552

5653
.. _whatsnew_0141.enhancements:
5754

@@ -118,4 +115,4 @@ Bug Fixes
118115
- Bug in broadcasting with ``.div``, integer dtypes and divide-by-zero (:issue:`7325`)
119116
- Bug in ``CustomBusinessDay.apply`` raiases ``NameError`` when ``np.datetime64`` object is passed (:issue:`7196`)
120117
- Bug in ``MultiIndex.append``, ``concat`` and ``pivot_table`` don't preserve timezone (:issue:`6606`)
121-
- Bug all ``StringMethods`` now work on empty Series (:issue:`7242`)
118+
- Bug in ``.loc`` with a list of indexers on a single-multi index level (that is not nested) (:issue:`7349`)

pandas/core/indexing.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,23 +1243,37 @@ def _getitem_axis(self, key, axis=0, validate_iterable=False):
12431243
return self._get_slice_axis(key, axis=axis)
12441244
elif com._is_bool_indexer(key):
12451245
return self._getbool_axis(key, axis=axis)
1246-
elif _is_list_like(key) and not (isinstance(key, tuple) and
1247-
isinstance(labels, MultiIndex)):
1246+
elif _is_list_like(key):
12481247

1249-
if hasattr(key, 'ndim') and key.ndim > 1:
1250-
raise ValueError('Cannot index with multidimensional key')
1248+
# GH 7349
1249+
# possibly convert a list-like into a nested tuple
1250+
# but don't convert a list-like of tuples
1251+
if isinstance(labels, MultiIndex):
1252+
if not isinstance(key, tuple) and len(key) > 1 and not isinstance(key[0], tuple):
1253+
key = tuple([key])
12511254

1252-
if validate_iterable:
1253-
self._has_valid_type(key, axis)
1254-
return self._getitem_iterable(key, axis=axis)
1255-
elif _is_nested_tuple(key, labels):
1256-
locs = labels.get_locs(key)
1257-
indexer = [ slice(None) ] * self.ndim
1258-
indexer[axis] = locs
1259-
return self.obj.iloc[tuple(indexer)]
1260-
else:
1261-
self._has_valid_type(key, axis)
1262-
return self._get_label(key, axis=axis)
1255+
# an iterable multi-selection
1256+
if not (isinstance(key, tuple) and
1257+
isinstance(labels, MultiIndex)):
1258+
1259+
if hasattr(key, 'ndim') and key.ndim > 1:
1260+
raise ValueError('Cannot index with multidimensional key')
1261+
1262+
if validate_iterable:
1263+
self._has_valid_type(key, axis)
1264+
1265+
return self._getitem_iterable(key, axis=axis)
1266+
1267+
# nested tuple slicing
1268+
if _is_nested_tuple(key, labels):
1269+
locs = labels.get_locs(key)
1270+
indexer = [ slice(None) ] * self.ndim
1271+
indexer[axis] = locs
1272+
return self.obj.iloc[tuple(indexer)]
1273+
1274+
# fall thru to straight lookup
1275+
self._has_valid_type(key, axis)
1276+
return self._get_label(key, axis=axis)
12631277

12641278

12651279
class _iLocIndexer(_LocationIndexer):

pandas/tests/test_indexing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,15 @@ def test_loc_multiindex(self):
13171317
result = df[attributes]
13181318
assert_frame_equal(result, df)
13191319

1320+
# GH 7349
1321+
# loc with a multi-index seems to be doing fallback
1322+
df = DataFrame(np.arange(12).reshape(-1,1),index=pd.MultiIndex.from_product([[1,2,3,4],[1,2,3]]))
1323+
1324+
expected = df.loc[([1,2],),:]
1325+
result = df.loc[[1,2]]
1326+
assert_frame_equal(result, expected)
1327+
1328+
13201329
def test_series_getitem_multiindex(self):
13211330

13221331
# GH 6018

0 commit comments

Comments
 (0)