Skip to content

BUG: Indexing MultiIndex with Series failed. #15041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Bug Fixes
- Bug in ``pd.to_numeric()`` in which float and unsigned integer elements were being improperly casted (:issue:`14941`, :issue:`15005`)
- Bug in ``pd.read_csv()`` in which the ``dialect`` parameter was not being verified before processing (:issue:`14898`)


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

- Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`)
- Bug in ``Series.ffill()`` with mixed dtypes containing tz-aware datetimes. (:issue:`14956`)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,9 @@ def _getitem_axis(self, key, axis=0):
if isinstance(labels, MultiIndex):
if (not isinstance(key, tuple) and len(key) > 1 and
not isinstance(key[0], tuple)):
if isinstance(key, ABCSeries):
# GH 14730
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can prob just

list(key)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought taking the .values was more explicit, but list is probably more familiar. Changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no .values is fraught with issues as it converts dtypes, we don't use it internally if at all possible.

key = list(key)
key = tuple([key])

# an iterable multi-selection
Expand Down
27 changes: 15 additions & 12 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
# -*- coding: utf-8 -*-

from datetime import timedelta
from itertools import product
import nose
import re
import warnings

from pandas import (DataFrame, date_range, period_range, MultiIndex, Index,
CategoricalIndex, compat)
from pandas.core.common import PerformanceWarning, UnsortedIndexError
from pandas.indexes.base import InvalidIndexError
from pandas.compat import range, lrange, u, PY3, long, lzip
from datetime import timedelta
from itertools import product

import nose

import numpy as np

from pandas.util.testing import (assert_almost_equal, assertRaises,
assertRaisesRegexp, assert_copy)
import pandas as pd

from pandas import (CategoricalIndex, DataFrame, Index, MultiIndex,
compat, date_range, period_range)
from pandas.compat import PY3, long, lrange, lzip, range, u
from pandas.core.common import PerformanceWarning, UnsortedIndexError
from pandas.indexes.base import InvalidIndexError
from pandas.lib import Timestamp

import pandas.util.testing as tm

import pandas as pd
from pandas.lib import Timestamp
from pandas.util.testing import (assertRaises, assertRaisesRegexp,
assert_almost_equal, assert_copy)


from .common import Base

Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,25 @@ def test_getitem_partial(self):
expected.columns = expected.columns.droplevel(0).droplevel(0)
assert_frame_equal(result, expected)

def test_series_index(self):
# GH14730
index = MultiIndex.from_product([[1, 2, 3], ['A', 'B', 'C']])
x = Series(index=index, data=range(9), dtype=np.float64)
y = Series([1, 3])
expected = Series(
data=[0, 1, 2, 6, 7, 8],
index=MultiIndex.from_product([[1, 3], ['A', 'B', 'C']]),
dtype=np.float64)
result = x.loc[y]
result2 = x.loc[[1, 3]]
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result2, expected)
empty_series = Series(data=[], dtype=np.float64)
expected2 = Series([], index=MultiIndex(
levels=index.levels, labels=[[], []], dtype=np.float64))
result3 = x.loc[empty_series]
tm.assert_series_equal(result3, expected2)

def test_getitem_slice_not_sorted(self):
df = self.frame.sortlevel(1).T

Expand Down