Skip to content

Commit bfba946

Browse files
author
Ben Kandel
committed
BUG: Indexing MultiIndex with Series failed.
Previously, accessing elements of a MultiIndex-indexed DataFrame with a Series failed. This changes that behavior so that it is possible to use a Series to access elements from a MultiIndex-indexed DataFrame, just as one would use a list.
1 parent 74e20a0 commit bfba946

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ Bug Fixes
297297
- Bug in ``pd.to_numeric()`` in which float and unsigned integer elements were being improperly casted (:issue:`14941`, :issue:`15005`)
298298
- Bug in ``pd.read_csv()`` in which the ``dialect`` parameter was not being verified before processing (:issue:`14898`)
299299

300-
300+
- Bug in ``DataFrame`` where indexing a ``MultiIndex`` using a ``Series`` failed (:issue:`14730`)
301301

302302
- Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`)
303303
- Bug in ``Series.ffill()`` with mixed dtypes containing tz-aware datetimes. (:issue:`14956`)

pandas/core/indexing.py

+3
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,9 @@ def _getitem_axis(self, key, axis=0):
14611461
if isinstance(labels, MultiIndex):
14621462
if (not isinstance(key, tuple) and len(key) > 1 and
14631463
not isinstance(key[0], tuple)):
1464+
if isinstance(key, ABCSeries):
1465+
# GH 14730
1466+
key = key.values.tolist()
14641467
key = tuple([key])
14651468

14661469
# an iterable multi-selection

pandas/tests/indexes/test_multi.py

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
# -*- coding: utf-8 -*-
22

3-
from datetime import timedelta
4-
from itertools import product
5-
import nose
63
import re
74
import warnings
85

9-
from pandas import (DataFrame, date_range, period_range, MultiIndex, Index,
10-
CategoricalIndex, compat)
11-
from pandas.core.common import PerformanceWarning, UnsortedIndexError
12-
from pandas.indexes.base import InvalidIndexError
13-
from pandas.compat import range, lrange, u, PY3, long, lzip
6+
from datetime import timedelta
7+
from itertools import product
8+
9+
import nose
1410

1511
import numpy as np
1612

17-
from pandas.util.testing import (assert_almost_equal, assertRaises,
18-
assertRaisesRegexp, assert_copy)
13+
import pandas as pd
14+
15+
from pandas import (CategoricalIndex, DataFrame, Index, MultiIndex, Series,
16+
compat, date_range, period_range)
17+
from pandas.compat import PY3, long, lrange, lzip, range, u
18+
from pandas.core.common import PerformanceWarning, UnsortedIndexError
19+
from pandas.indexes.base import InvalidIndexError
20+
from pandas.lib import Timestamp
1921

2022
import pandas.util.testing as tm
2123

22-
import pandas as pd
23-
from pandas.lib import Timestamp
24+
from pandas.util.testing import (assertRaises, assertRaisesRegexp,
25+
assert_almost_equal, assert_copy)
26+
2427

2528
from .common import Base
2629

@@ -343,6 +346,19 @@ def test_set_levels_labels_names_bad_input(self):
343346
with tm.assertRaisesRegexp(TypeError, 'string'):
344347
self.index.set_names(names, level=0)
345348

349+
def test_series_index(self):
350+
# GH14730
351+
index = MultiIndex.from_product([[1, 2, 3], ['A', 'B', 'C']])
352+
x = Series(index=index, data=range(9))
353+
y = Series([1, 3])
354+
expected = Series(
355+
data=[0, 1, 2, 6, 7, 8],
356+
index=MultiIndex.from_product([[1, 3], ['A', 'B', 'C']]))
357+
actual_from_series = x.loc[y]
358+
actual_from_list = x.loc[[1, 3]]
359+
tm.assert_series_equal(expected, actual_from_list)
360+
tm.assert_series_equal(expected, actual_from_series)
361+
346362
def test_set_levels_categorical(self):
347363
# GH13854
348364
index = MultiIndex.from_arrays([list("xyzx"), [0, 1, 2, 3]])

0 commit comments

Comments
 (0)