def test_contains(self): # GH 17717 p0 = Period("2017-09-01") p1 = Period("2017-09-02") p2 = Period("2017-09-03") p3 = Period("2017-09-04") ps0 = [p0, p1, p2] idx0 = PeriodIndex(ps0) ser = pd.Series(range(6, 9), index=idx0) for p in ps0: assert p in idx0 assert str(p) in idx0 # GH#31172 # Higher-resolution period-like are _not_ considered as contained key = "2017-09-01 00:00:01" assert key not in idx0 with pytest.raises(KeyError, match=key): idx0.get_loc(key) with pytest.raises(KeyError, match=key): idx0.get_value(ser, key) assert "2017-09" in idx0 assert p3 not in idx0
def test_get_value(self): # GH 17717 p0 = Period("2017-09-01") p1 = Period("2017-09-02") p2 = Period("2017-09-03") idx0 = PeriodIndex([p0, p1, p2]) input0 = pd.Series(np.array([1, 2, 3]), index=idx0) expected0 = 2 result0 = idx0.get_value(input0, p1) assert result0 == expected0 idx1 = PeriodIndex([p1, p1, p2]) input1 = pd.Series(np.array([1, 2, 3]), index=idx1) expected1 = input1.iloc[[0, 1]] result1 = idx1.get_value(input1, p1) tm.assert_series_equal(result1, expected1) idx2 = PeriodIndex([p1, p2, p1]) input2 = pd.Series(np.array([1, 2, 3]), index=idx2) expected2 = input2.iloc[[0, 2]] result2 = idx2.get_value(input2, p1) tm.assert_series_equal(result2, expected2)