Skip to content

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

Merged
merged 1 commit into from
Jul 8, 2019

Conversation

toobaz
Copy link
Member

@toobaz toobaz commented Jun 30, 2019

@toobaz toobaz added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Jun 30, 2019
Copy link
Member

@simonjayhawkins simonjayhawkins left a 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
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

(related to above)

Copy link
Contributor

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)

@toobaz toobaz force-pushed the single_missing_item_list_27148 branch from c8f308b to bbf761c Compare June 30, 2019 22:45
@toobaz toobaz force-pushed the single_missing_item_list_27148 branch from bbf761c to e29d261 Compare June 30, 2019 23:24
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')
Copy link
Member

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)

Copy link
Member Author

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".

@toobaz toobaz force-pushed the single_missing_item_list_27148 branch 3 times, most recently from 7e37361 to bf24e69 Compare July 4, 2019 10:22
@toobaz toobaz force-pushed the single_missing_item_list_27148 branch from bf24e69 to 999b3e3 Compare July 4, 2019 10:22
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

@jreback jreback added this to the 0.25.0 milestone Jul 8, 2019
@jreback jreback merged commit fd2146f into pandas-dev:master Jul 8, 2019
@jreback
Copy link
Contributor

jreback commented Jul 8, 2019

thanks @toobaz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Indexing single level with list of missing labels raises if (and only if) it has length 1
3 participants