Skip to content

Commit 9f140c9

Browse files
makbigcPingviinituutti
authored andcommitted
[BUG] exception handling of MultiIndex.__contains__ too narrow (pandas-dev#25268)
1 parent 11d84af commit 9f140c9

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,9 @@ Missing
149149
MultiIndex
150150
^^^^^^^^^^
151151

152+
- Bug in which incorrect exception raised by :meth:`pd.Timedelta` when testing the membership of :class:`MultiIndex` (:issue:`24570`)
152153
-
153154
-
154-
-
155-
156155

157156
I/O
158157
^^^

pandas/core/indexes/multi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ def __contains__(self, key):
840840
try:
841841
self.get_loc(key)
842842
return True
843-
except (LookupError, TypeError):
843+
except (LookupError, TypeError, ValueError):
844844
return False
845845

846846
contains = __contains__

pandas/tests/indexing/multiindex/test_multiindex.py

+8
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@ def test_multi_nan_indexing(self):
8484
name='a'),
8585
Index(['C1', 'C2', 'C3', 'C4'], name='b')])
8686
tm.assert_frame_equal(result, expected)
87+
88+
def test_contains(self):
89+
# GH 24570
90+
tx = pd.timedelta_range('09:30:00', '16:00:00', freq='30 min')
91+
idx = MultiIndex.from_arrays([tx, np.arange(len(tx))])
92+
assert tx[0] in idx
93+
assert 'element_not_exit' not in idx
94+
assert '0 day 09:30:00' in idx

0 commit comments

Comments
 (0)