Skip to content
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] exception handling of MultiIndex.__contains__ too narrow #25268

Merged
merged 5 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,9 @@ Missing
MultiIndex
^^^^^^^^^^

- Bug in which incorrect exception raised by :meth:`pd.Timedelta` when testing the membership of :class:`MultiIndex` (:issue:`24570`)
-
-
-


I/O
^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ def __contains__(self, key):
try:
self.get_loc(key)
return True
except (LookupError, TypeError):
except (LookupError, TypeError, ValueError):
return False

contains = __contains__
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexing/multiindex/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ def test_multi_nan_indexing(self):
name='a'),
Index(['C1', 'C2', 'C3', 'C4'], name='b')])
tm.assert_frame_equal(result, expected)

def test_contains(self):
# GH 24570
tx = pd.timedelta_range('09:30:00', '16:00:00', freq='30 min')
idx = MultiIndex.from_arrays([tx, np.arange(len(tx))])
assert tx[0] in idx
assert 'element_not_exit' not in idx
assert '0 day 09:30:00' in idx