-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: fix KeyError with list of a single, missing, element #27154
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
BUG: fix KeyError with list of a single, missing, element #27154
Conversation
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.
generally lgtm. a few minor queries.
|
||
# ... even when none is found: | ||
expected = df.iloc[[]] | ||
for l in [2], [2, 3]: # GH 27148 |
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.
better to use parametrisation than a for loop.
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.
(related to above)
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 agree here, would be nice to parameterize this, easier to grok; no big deal to copy-paste the setup (no fixture really needed here)
c8f308b
to
bbf761c
Compare
bbf761c
to
e29d261
Compare
expected = Series([0, 3, 6], index=MultiIndex.from_product( | ||
[['A', 'B', 'C'], ['foo']], names=['one', 'two'])).sort_index() | ||
elif level == -1: | ||
# Empty: use idx[:0] as index to have appropriate (hidden) labels | ||
expected = Series([], index=idx[:0], dtype='int64') |
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 think this is the same as the expected = s.iloc[[]]
above.
with the exception case now removed, I think that this whole test could be simplified..
@pytest.mark.parametrize('indexer, expected', [
([], []), # empty ok
(['A'], slice(3)),
(['A', 'D'], slice(3)),
(['D', 'E'], []), # no values found - fine
(['D'], []), # same, with single item list: GH 27148
(pd.IndexSlice[:, ['foo']], slice(2, None, 3)),
(pd.IndexSlice[:, ['foo', 'bah']], slice(2, None, 3))
])
def test_loc_getitem_duplicates_multiindex_missing_indexers(indexer, expected):
# GH 7866
# multi-index slicing with missing indexers
idx = MultiIndex.from_product([['A', 'B', 'C'],
['foo', 'bar', 'baz']],
names=['one', 'two'])
s = Series(np.arange(9, dtype='int64'), index=idx).sort_index()
expected = s.iloc[expected]
result = s.loc[indexer]
tm.assert_series_equal(result, expected)
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.
Right, thanks! I only changed the name of the second parameter to "pos".
7e37361
to
bf24e69
Compare
bf24e69
to
999b3e3
Compare
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.
thanks @toobaz |
git diff upstream/master -u -- "*.py" | flake8 --diff