Skip to content

Conversation

@bkandel
Copy link
Contributor

@bkandel bkandel commented Jan 3, 2017

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.

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])
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

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)

tm.assert_series_equal(expected, actual_from_series)

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

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.

@sinhrks sinhrks added Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Jan 3, 2017
@codecov-io
Copy link

codecov-io commented Jan 3, 2017

Current coverage is 84.75% (diff: 100%)

Merging #15041 into master will decrease coverage by 0.01%

@@ 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 

Powered by Codecov. Last update 74e20a0...3698087

@jreback jreback added this to the 0.20.0 milestone Jan 3, 2017
@jreback jreback closed this in 6eb705f Jan 4, 2017
@jreback
Copy link
Contributor

jreback commented Jan 4, 2017

thanks @bkandel (I moved the tests to the appropriate location)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

4 participants