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 1 commit
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 = key.values.tolist()
key = tuple([key])

# an iterable multi-selection
Expand Down
40 changes: 28 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, Series,
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 Expand Up @@ -343,6 +346,19 @@ def test_set_levels_labels_names_bad_input(self):
with tm.assertRaisesRegexp(TypeError, 'string'):
self.index.set_names(names, level=0)

def test_series_index(self):
# GH14730
index = MultiIndex.from_product([[1, 2, 3], ['A', 'B', 'C']])
x = Series(index=index, data=range(9))
y = Series([1, 3])
Copy link
Contributor

Choose a reason for hiding this comment

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

put this is tests/test_multilevel or test_indexing

find a very similar test (iow indexing a mi with list/ndarray) and put after

expected = Series(
data=[0, 1, 2, 6, 7, 8],
index=MultiIndex.from_product([[1, 3], ['A', 'B', 'C']]))
actual_from_series = x.loc[y]
actual_from_list = x.loc[[1, 3]]
tm.assert_series_equal(expected, actual_from_list)
tm.assert_series_equal(expected, actual_from_series)
Copy link
Contributor

Choose a reason for hiding this comment

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

result = ...
expected = ....
assert_series_equal(result, expected)


def test_set_levels_categorical(self):
# GH13854
Copy link
Contributor

Choose a reason for hiding this comment

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

check empty series as well

index = MultiIndex.from_arrays([list("xyzx"), [0, 1, 2, 3]])
Expand Down