From c1c212397a84c53abfa7799a9bbf2bac29ae3f9d Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 11 Feb 2019 21:28:38 +0800 Subject: [PATCH 1/4] Fix the bug (GH24570) --- pandas/core/indexes/multi.py | 2 +- pandas/tests/test_multilevel.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index e2237afbcac0f..e0eab47447e25 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -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__ diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index 4ea7e9b8ec9a4..a03867e97a6b4 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -1711,6 +1711,14 @@ def test_repeat(self): m_df = Series(data, index=m_idx) assert m_df.repeat(3).shape == (3 * len(data), ) + def test_in(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 + class TestSorted(Base): """ everything you wanted to test about sorting """ From ed7052c26eabe7e5bdd43155aca53a76d4f780f3 Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 11 Feb 2019 22:24:37 +0800 Subject: [PATCH 2/4] Add whatsnew entry --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 95362521f3b9f..ef0bd157101e0 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -148,7 +148,7 @@ Missing MultiIndex ^^^^^^^^^^ -- +- Bug in which the error raised by Timedelta is not handled in the exception of :meth:`__contains__` (:issue:`24570`) - - From e76a5f36b1b46e9aeb1d8385a887fe3a76d332c3 Mon Sep 17 00:00:00 2001 From: makbigc Date: Tue, 12 Feb 2019 21:26:55 +0800 Subject: [PATCH 3/4] Amend after review --- doc/source/whatsnew/v0.25.0.rst | 3 +-- pandas/tests/indexing/multiindex/test_multiindex.py | 8 ++++++++ pandas/tests/test_multilevel.py | 8 -------- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index ef0bd157101e0..9bcabb0944e72 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -148,11 +148,10 @@ Missing MultiIndex ^^^^^^^^^^ -- Bug in which the error raised by Timedelta is not handled in the exception of :meth:`__contains__` (:issue:`24570`) +- Bug in which incorrect exception raised by :meth:`pd.Timedelta` when testing the membership of :class:`MultiIndex` (:issue:`24570`) - - - I/O ^^^ diff --git a/pandas/tests/indexing/multiindex/test_multiindex.py b/pandas/tests/indexing/multiindex/test_multiindex.py index 4f5517f89e852..ccf017489e046 100644 --- a/pandas/tests/indexing/multiindex/test_multiindex.py +++ b/pandas/tests/indexing/multiindex/test_multiindex.py @@ -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 diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index a03867e97a6b4..4ea7e9b8ec9a4 100644 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -1711,14 +1711,6 @@ def test_repeat(self): m_df = Series(data, index=m_idx) assert m_df.repeat(3).shape == (3 * len(data), ) - def test_in(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 - class TestSorted(Base): """ everything you wanted to test about sorting """ From 8ec0e3110e787461ce23cb0cc836fd28b334874d Mon Sep 17 00:00:00 2001 From: Mak Sze Chun Date: Tue, 12 Feb 2019 22:17:30 +0800 Subject: [PATCH 4/4] remove trailing space --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 9bcabb0944e72..25a4c6e9ff1b3 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -148,7 +148,7 @@ Missing MultiIndex ^^^^^^^^^^ -- Bug in which incorrect exception raised by :meth:`pd.Timedelta` when testing the membership of :class:`MultiIndex` (:issue:`24570`) +- Bug in which incorrect exception raised by :meth:`pd.Timedelta` when testing the membership of :class:`MultiIndex` (:issue:`24570`) - -