Skip to content

Commit 0412053

Browse files
committed
PeriodIndex test keys that aren't strings, DatetimeIndex tests Periods
1 parent 2113672 commit 0412053

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

doc/source/whatsnew/v0.17.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ Bug Fixes
629629
- Bug that caused segfault when resampling an empty Series (:issue:`10228`)
630630
- Bug in ``DatetimeIndex`` and ``PeriodIndex.value_counts`` resets name from its result, but retains in result's ``Index``. (:issue:`10150`)
631631
- Bug in ``pd.eval`` using ``numexpr`` engine coerces 1 element numpy array to scalar (:issue:`10546`)
632+
- Bug in ``PeriodIndex.__contains__`` & ``DatetimeIndex.__contains__`` that always returned False for each other's objects (:issue:`10798`)
632633
- Bug in ``pd.concat`` with ``axis=0`` when column is of dtype ``category`` (:issue:`10177`)
633634
- Bug in ``read_msgpack`` where input type is not always checked (:issue:`10369`, :issue:`10630`)
634635
- Bug in ``pd.read_csv`` with kwargs ``index_col=False``, ``index_col=['a', 'b']`` or ``dtype``

pandas/tests/test_index.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,7 +2817,31 @@ def test_view(self):
28172817
result = self._holder(i)
28182818
tm.assert_index_equal(result, i)
28192819

2820-
class TestDatetimeIndex(DatetimeLike, tm.TestCase):
2820+
class DatetimeAbsoluteLike(DatetimeLike):
2821+
2822+
# GH10801
2823+
def test_datetimeabsolute_contains(self):
2824+
2825+
i = self.create_index()
2826+
2827+
self.assertTrue(i[2] in i)
2828+
self.assertFalse('2012' in i)
2829+
2830+
# python datetime objects
2831+
self.assertTrue(datetime(2013,1,1) in i)
2832+
2833+
# strings
2834+
self.assertTrue('2013-1-1' in i)
2835+
2836+
# Timestamp # GH10801
2837+
self.assertTrue(pd.Timestamp('2013-1-1') in i)
2838+
2839+
# pandas Period
2840+
self.assertTrue(pd.Period('2013-1-1', 'D') in i)
2841+
self.assertFalse(pd.Period('2013-1-1', 'M') in i)
2842+
2843+
2844+
class TestDatetimeIndex(DatetimeAbsoluteLike, tm.TestCase):
28212845
_holder = DatetimeIndex
28222846
_multiprocess_can_split_ = True
28232847

@@ -2964,7 +2988,7 @@ def test_nat(self):
29642988
self.assertIs(DatetimeIndex([np.nan])[0], pd.NaT)
29652989

29662990

2967-
class TestPeriodIndex(DatetimeLike, tm.TestCase):
2991+
class TestPeriodIndex(DatetimeAbsoluteLike, tm.TestCase):
29682992
_holder = PeriodIndex
29692993
_multiprocess_can_split_ = True
29702994

pandas/tseries/index.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,12 @@ def get_loc(self, key, method=None):
12891289
'when key is a time object')
12901290
return self.indexer_at_time(key)
12911291

1292+
# check if it's a Period and the frequencies are the same - otherwise a monthly period would match for
1293+
# a daily timestamp at the beginning of the month. NB: 'B' and 'D' therefore won't match
1294+
if isinstance(key, com.ABCPeriod) and key.freq == self.freq:
1295+
key = key.to_timestamp()
1296+
return Index.get_loc(self, key, method=method)
1297+
12921298
try:
12931299
return Index.get_loc(self, key, method=method)
12941300
except (KeyError, ValueError, TypeError):

pandas/tseries/period.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,14 @@ def _na_value(self):
296296
return self._box_func(tslib.iNaT)
297297

298298
def __contains__(self, key):
299+
# if key isn't a Period of the same freq, rely on `get_loc` for the coercion.
299300
if not isinstance(key, Period) or key.freq != self.freq:
300-
if isinstance(key, compat.string_types):
301-
try:
302-
self.get_loc(key)
303-
return True
304-
except Exception:
305-
return False
306-
return False
301+
try:
302+
self.get_loc(key)
303+
return True
304+
except Exception:
305+
return False
306+
# If it is a Period of the same freq, go straight to the _engine
307307
return key.ordinal in self._engine
308308

309309
@property

0 commit comments

Comments
 (0)