-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Conversation
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.
# GH14730 | ||
index = MultiIndex.from_product([[1, 2, 3], ['A', 'B', 'C']]) | ||
x = Series(index=index, data=range(9)) | ||
y = Series([1, 3]) |
There was a problem hiding this comment.
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
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) |
There was a problem hiding this comment.
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)
actual_from_list = x.loc[[1, 3]] | ||
tm.assert_series_equal(expected, actual_from_list) | ||
tm.assert_series_equal(expected, actual_from_series) | ||
|
||
def test_set_levels_categorical(self): | ||
# GH13854 |
There was a problem hiding this comment.
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
@@ -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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Current coverage is 84.75% (diff: 100%)@@ master #15041 diff @@
==========================================
Files 145 145
Lines 51129 51141 +12
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 43343 43345 +2
- Misses 7786 7796 +10
Partials 0 0
|
thanks @bkandel (I moved the tests to the appropriate location) |
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.
git diff upstream/master | flake8 --diff