diff --git a/doc/source/whatsnew/v0.16.0.txt b/doc/source/whatsnew/v0.16.0.txt index 0468c220bcb98..b0e2147c20674 100644 --- a/doc/source/whatsnew/v0.16.0.txt +++ b/doc/source/whatsnew/v0.16.0.txt @@ -293,7 +293,7 @@ Bug Fixes - Bug in ``DataFrame.where`` and ``Series.where`` coerce numerics to string incorrectly (:issue:`9280`) - Bug in ``DataFrame.where`` and ``Series.where`` raise ``ValueError`` when string list-like is passed. (:issue:`9280`) - Accessing ``Series.str`` methods on with non-string values now raises ``TypeError`` instead of producing incorrect results (:issue:`9184`) - +- Bug in ``DatetimeIndex.__contains__`` when index has duplicates and is not monotonic increasing (:issue:`9512`) - Fixed division by zero error for ``Series.kurt()`` when all values are equal (:issue:`9197`) diff --git a/pandas/tseries/base.py b/pandas/tseries/base.py index 266e69cf3484b..a1904d38ab530 100644 --- a/pandas/tseries/base.py +++ b/pandas/tseries/base.py @@ -65,7 +65,7 @@ def _format_with_header(self, header, **kwargs): def __contains__(self, key): try: res = self.get_loc(key) - return np.isscalar(res) or type(res) == slice + return np.isscalar(res) or type(res) == slice or np.any(res) except (KeyError, TypeError): return False diff --git a/pandas/tseries/tests/test_base.py b/pandas/tseries/tests/test_base.py index b63f1fb0a031a..3c9a94286509a 100644 --- a/pandas/tseries/tests/test_base.py +++ b/pandas/tseries/tests/test_base.py @@ -292,6 +292,12 @@ def test_value_counts_unique(self): tm.assert_index_equal(idx.unique(), exp_idx) + def test_nonunique_contains(self): + # GH 9512 + for idx in map(DatetimeIndex, ([0, 1, 0], [0, 0, -1], [0, -1, -1], + ['2015', '2015', '2016'], ['2015', '2015', '2014'])): + tm.assertIn(idx[0], idx) + class TestTimedeltaIndexOps(Ops): @@ -684,6 +690,14 @@ def test_value_counts_unique(self): tm.assert_index_equal(idx.unique(), exp_idx) + def test_nonunique_contains(self): + # GH 9512 + for idx in map(TimedeltaIndex, ([0, 1, 0], [0, 0, -1], [0, -1, -1], + ['00:01:00', '00:01:00', '00:02:00'], + ['00:01:00', '00:01:00', '00:00:01'])): + tm.assertIn(idx[0], idx) + + class TestPeriodIndexOps(Ops): def setUp(self):