From 18c76f4f8c71a50acf5e2f16729c033e9d9b03c6 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 18 May 2017 15:46:09 -0400 Subject: [PATCH 01/56] new interval and intervalIndex behavior spec --- pandas/tests/indexing/test_interval.py | 407 ++++++++++++++++++++++++- 1 file changed, 404 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index 2552fc066cc87..e419950b88aed 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -103,6 +103,97 @@ def test_with_interval(self): with pytest.raises(KeyError): s[Interval(5, 6)] + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_with_interval_updated_behavior(self): + + s = self.s + expected = 0 + + result = s.loc[Interval(0, 1)] + assert result == expected + + result = s[Interval(0, 1)] + assert result == expected + + # missing + with pytest.raises(KeyError): + s.loc[Interval(-2, 0)] + + with pytest.raises(KeyError): + s[Interval(-2, 0)] + + with pytest.raises(KeyError): + s.loc[Interval(5, 6)] + + with pytest.raises(KeyError): + s[Interval(5, 6)] + + # overlapping but not identical + # right: + with pytest.raises(KeyError): + s.loc[Interval(4, 6)] + + with pytest.raises(KeyError): + s.loc[[Interval(4, 6)]] + + # left: + with pytest.raises(KeyError): + s.loc[Interval(-1, 4)] + + with pytest.raises(KeyError): + s.loc[[Interval(-1, 4)]] + + with pytest.raises(KeyError): + s.loc[Interval(0, 1, closed='left')] + + with pytest.raises(KeyError): + s.loc[[Interval(0, 1, closed='left')]] + + with pytest.raises(KeyError): + s.loc[Interval(0, 1, closed='both')] + + with pytest.raises(KeyError): + s.loc[[Interval(0, 1, closed='both')]] + + # inner + with pytest.raises(KeyError): + s.loc[Interval(2, 4)] + + with pytest.raises(KeyError): + s.loc[[Interval(2, 4)]] + + with pytest.raises(KeyError): + s.loc[Interval(4, 5, closed='left')] + + with pytest.raises(KeyError): + s.loc[[Interval(4, 5, closed='left')]] + + with pytest.raises(KeyError): + s.loc[Interval(4, 5, closed='both')] + + with pytest.raises(KeyError): + s.loc[[Interval(4, 5, closed='both')]] + + # outer + with pytest.raises(KeyError): + s.loc[Interval(-1, 6)] + + with pytest.raises(KeyError): + s.loc[[Interval(-1, 6)]] + + with pytest.raises(KeyError): + s.loc[Interval(0, 6, closed='left')] + + with pytest.raises(KeyError): + s.loc[[Interval(0, 6, closed='left')]] + + with pytest.raises(KeyError): + s.loc[Interval(0, 5, closed='both')] + + with pytest.raises(KeyError): + s.loc[[Interval(0, 5, closed='both')]] + def test_with_slices(self): s = self.s @@ -122,6 +213,46 @@ def test_with_slices(self): with pytest.raises(ValueError): s[0:4:2] + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_with_slices_updated_behavior(self): + + s = self.s + + # slice of interval + expected = s.iloc[4:] + result = s.loc[Interval(3, 4):] + tm.assert_series_equal(expected, result) + + expected = s.iloc[4:] + result = s[Interval(3, 4):] + tm.assert_series_equal(expected, result) + + with pytest.raises(KeyError): + s.loc[Interval(3, 6):] + + with pytest.raises(KeyError): + s[Interval(3, 6):] + + with pytest.raises(KeyError): + s.loc[Interval(3, 4, closed='left'):] + + with pytest.raises(KeyError): + s[Interval(3, 4, closed='left'):] + + with pytest.raises(KeyError): + s.loc[Interval(3, 4, closed='both'):] + + with pytest.raises(KeyError): + s[Interval(3, 4, closed='both'):] + + # slice of scalar + with pytest.raises(NotImplementedError): + s[0:4] ## not sure what the behvaior should be here. + + # slice of scalar with step != 1 + with pytest.raises(ValueError): + s[0:4:2] ## This should probably definitely fail I guess? + def test_with_overlaps(self): s = self.s @@ -159,10 +290,41 @@ def test_with_overlaps(self): with pytest.raises(KeyError): s.loc[[Interval(3, 5)]] + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_with_overlaps_updated_behavior(self): + + idx = IntervalIndex.from_tuples([(1, 5), (3, 7)]) + s = Series(range(len(idx)), index=idx) + + # scalar + expected = s + result = s[4] + tm.assert_series_equal(expected, result) + + expected = s + result = s[[4]] + tm.assert_series_equal(expected, result) + + expected = s + result = s.loc[[4]] + tm.assert_series_equal(expected, result) + + # interval + with pytest.raises(KeyError): + s[Interval(3, 5)] + + with pytest.raises(KeyError): + s[[Interval(3, 5)]] + + with pytest.raises(KeyError): + s.loc[Interval(3, 5)] + + with pytest.raises(KeyError): + s.loc[[Interval(3, 5)]] + def test_non_unique(self): idx = IntervalIndex.from_tuples([(1, 3), (3, 7)]) - s = pd.Series(range(len(idx)), index=idx) result = s.loc[Interval(1, 3)] @@ -172,6 +334,12 @@ def test_non_unique(self): expected = s.iloc[0:1] tm.assert_series_equal(expected, result) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_non_unique_updated_behavior(self): + + # Actually I think we should remove this test? Not sure what exactly it's meant to gauge... + pass + def test_non_unique_moar(self): idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) @@ -192,11 +360,63 @@ def test_non_unique_moar(self): with pytest.raises(ValueError): s[[Interval(1, 3)]] + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_non_unique_moar_updated_behavior(self): + + idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) + s = Series(range(len(idx)), index=idx) + + expected = s.iloc[[0, 1]] + result = s.loc[Interval(1, 3)] + tm.assert_series_equal(expected, result) + + # non-unique index and slices not allowed + with pytest.raises(ValueError): + s.loc[Interval(1, 3):] + # this is confusing to me. I would have done: + # expected = s + # result = s.loc[Interval(1, 3):] + # tm.assert_series_equal(expected, result) + + with pytest.raises(ValueError): + s[Interval(1, 3):] + # Same here: + # expected = s + # result = s[Interval(1, 3):] + # tm.assert_series_equal(expected, result) + + # non-unique + with pytest.raises(ValueError): + s[[Interval(1, 3)]] + # not sure why the behavior for [[]] is different than []... + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_non_unique_moar_updated_behavior(self): + + idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) + s = Series(range(len(idx)), index=idx) + + result = s.loc[Interval(1, 3)] + expected = s.iloc[[0, 1]] + tm.assert_series_equal(expected, result) + + # non-unique index and slices not allowed + with pytest.raises(ValueError): + s.loc[Interval(1, 3):] + + with pytest.raises(ValueError): + s[Interval(1, 3):] + + # non-unique + with pytest.raises(ValueError): + s[[Interval(1, 3)]] + + def test_non_matching(self): + s = self.s - # this is a departure from our current - # indexin scheme, but simpler + # this is a departure from our current indexing scheme, but simpler with pytest.raises(KeyError): s.loc[[-1, 3, 4, 5]] @@ -243,3 +463,184 @@ def test_loc_getitem_frame(self): # partial missing with pytest.raises(KeyError): df.loc[[10, 4]] + + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_interval_covers(self): + + # class Interval: + # def covers(self, other: Interval) -> bool + # def covers(self, other: IntervalIndex) -> IntegerArray1D + + assert Interval(1, 3).covers(Interval(1.5, 2.5)) + assert Interval(1, 3).covers(Interval(1, 2)) + assert Interval(1, 3).covers(Interval(2, 3)) + assert not Interval(1, 3).covers(Interval(0.5, 2.5)) + assert not Interval(1, 3).covers(Interval(1.5, 3.5)) + + assert Interval(1, 3, closed='right').covers(Interval(1, 3, closed='right')) + assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='left')) + assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='both')) + + assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='left').covers(Interval(1, 3, closed='left')) + assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='both')) + + assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='both')) + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + + assert Interval(1, 3, closed='right').covers(idx) == np.array([1, 2]) + assert Interval(0, 3, closed='right').covers(idx) == np.array([0, 1, 2]) + assert Interval(0, 2, closed='right').covers(idx) == np.array([0]) + assert Interval(2, 4, closed='right').covers(idx) == np.array([1]) + + assert Interval(1, 3, closed='left').covers(idx) == np.array([]) + assert Interval(0, 3, closed='left').covers(idx) == np.array([0]) + assert Interval(0, 2, closed='left').covers(idx) == np.array([0]) + assert Interval(2, 4, closed='left').covers(idx) == np.array([1]) + + assert Interval(1, 3, closed='both').covers(idx) == np.array([1, 2]) + assert Interval(0, 5, closed='both').covers(idx) == np.array([0, 1, 2]) + assert Interval(0, 2, closed='both').covers(idx) == np.array([0]) + assert Interval(2, 4, closed='both').covers(idx) == np.array([1]) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_interval_overlaps(self): + + # class Interval: + # def overlaps(self, other: Interval) -> bool + # def overlaps(self, other: IntervalIndex) -> IntegerArray1D + + assert Interval(1, 3).overlaps(Interval(1.5, 2.5)) + assert Interval(1, 3).overlaps(Interval(1, 2)) + assert Interval(1, 3).overlaps(Interval(2, 3)) + assert Interval(1, 3).overlaps(Interval(0.5, 2.5)) + assert Interval(1, 3).overlaps(Interval(1.5, 3.5)) + + assert not Interval(1, 3).overlaps(Interval(-1, 1)) + assert not Interval(1, 3).overlaps(Interval(3, 5)) + + # right + assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='both')) + + assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='right')) + assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='left')) + assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='both')) + + assert not Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='right')) + assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='left')) + assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='both')) + + # left + assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='both')) + + assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='right')) + assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='left')) + assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='both')) + + assert not Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='right')) + assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='left')) + assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='both')) + + # both + assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='both')) + + assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='right')) + assert not Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='left')) + assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='both')) + + assert not Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='right')) + assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='left')) + assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='both')) + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + + assert Interval(1, 3, closed='right').overlaps(idx) == np.array([1, 2]) + assert Interval(1, 2, closed='right').overlaps(idx) == np.array([2]) + assert Interval(0, 2, closed='right').overlaps(idx) == np.array([0, 2]) + assert Interval(3, 4, closed='right').overlaps(idx) == np.array([]) + + assert Interval(1, 3, closed='left').overlaps(idx) == np.array([0, 1, 2]) + assert Interval(1, 2, closed='left').overlaps(idx) == np.array([0, 2]) + assert Interval(0, 2, closed='left').overlaps(idx) == np.array([0, 2]) + assert Interval(3, 4, closed='left').overlaps(idx) == np.array([3]) + + assert Interval(1, 3, closed='both').overlaps(idx) == np.array([0, 1, 2]) + assert Interval(1, 2, closed='both').overlaps(idx) == np.array([0, 2]) + assert Interval(0, 2, closed='both').overlaps(idx) == np.array([0, 2]) + assert Interval(3, 4, closed='both').overlaps(idx) == np.array([3]) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_intervalIndex_covers(self): + + # class IntervalIndex: + # def covers(self, other: Interval) -> IntegerArray1D + # def covers(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + + assert idx.covers(Interval(1, 3, closed='right')) == np.array([1, 2]) + assert idx.covers(Interval(0, 3, closed='right')) == np.array([0, 1, 2]) + assert idx.covers(Interval(0, 2, closed='right')) == np.array([0]) + assert idx.covers(Interval(2, 4, closed='right')) == np.array([1]) + + assert idx.covers(Interval(1, 3, closed='left')) == np.array([]) + assert idx.covers(Interval(0, 3, closed='left')) == np.array([0]) + assert idx.covers(Interval(0, 2, closed='left')) == np.array([0]) + assert idx.covers(Interval(2, 4, closed='left')) == np.array([1]) + + assert idx.covers(Interval(1, 3, closed='both')) == np.array([1, 2]) + assert idx.covers(Interval(0, 5, closed='both')) == np.array([0, 1, 2]) + assert idx.covers(Interval(0, 2, closed='both')) == np.array([0]) + assert idx.covers(Interval(2, 4, closed='both')) == np.array([1]) + + idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') + idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') + idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') + + assert idx.covers(idx1) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + assert idx.covers(idx2) == (np.array([2]), np.array([1])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + assert idx.covers(idx3) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_intervalIndex_overlaps(self): + + # class IntervalIndex: + # def overlaps(self, other: Interval) -> IntegerArray1D + # def overlaps(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + + assert idx.overlaps(Interval(1, 3, closed='right')) == np.array([1, 2]) + assert idx.overlaps(Interval(1, 2, closed='right')) == np.array([2]) + assert idx.overlaps(Interval(0, 2, closed='right')) == np.array([0, 2]) + assert idx.overlaps(Interval(3, 4, closed='right')) == np.array([]) + + assert idx.overlaps(Interval(1, 3, closed='left')) == np.array([0, 1, 2]) + assert idx.overlaps(Interval(1, 2, closed='left')) == np.array([0, 2]) + assert idx.overlaps(Interval(0, 2, closed='left')) == np.array([0, 2]) + assert idx.overlaps(Interval(3, 4, closed='left')) == np.array([3]) + + assert idx.overlaps(Interval(1, 3, closed='both')) == np.array([0, 1, 2]) + assert idx.overlaps(Interval(1, 2, closed='both')) == np.array([0, 2]) + assert idx.overlaps(Interval(0, 2, closed='both')) == np.array([0, 2]) + assert idx.overlaps(Interval(3, 4, closed='both')) == np.array([3]) + + idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') + idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') + idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') + + assert idx.overlaps(idx1) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + assert idx.overlaps(idx2) == (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + assert idx.overlaps(idx3) == (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + From ddc508c400c402d10feff6f02e7e1eb9217df929 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 25 May 2017 12:24:35 -0400 Subject: [PATCH 02/56] moved tests from indexing to indexes, and added new tests --- pandas/tests/indexes/test_interval.py | 285 +++++++++++++++++++++++++ pandas/tests/indexing/test_interval.py | 245 --------------------- 2 files changed, 285 insertions(+), 245 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 33745017fe3d6..61fd57a4077b7 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -348,6 +348,109 @@ def test_get_loc_value(self): idx = IntervalIndex.from_arrays([0, 2], [1, 3]) pytest.raises(KeyError, idx.get_loc, 1.5) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_get_loc_value(self): + + right = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='right') + + pytest.raises(KeyError, right.get_loc(-0.5) ) + pytest.raises(KeyError, right.get_loc(0) ) + assert right.get_loc(0.5) == 0 + assert right.get_loc(1) == 0 + pytest.raises(KeyError, right.get_loc(1.5) ) + pytest.raises(KeyError, right.get_loc(2) ) + assert right.get_loc(2.5) == 1 + assert right.get_loc(3) == 1 + pytest.raises(KeyError, right.get_loc(3.5) ) + + assert right.get_loc(Interval(0, 1, closed='right')) == 0 + pytest.raises(KeyError, right.get_loc(Interval(1, 2, closed='right')) ) + assert right.get_loc(Interval(2, 3, closed='right')) == 1 + pytest.raises(KeyError, right.get_loc(Interval(3, 4, closed='right')) ) + pytest.raises(KeyError, right.get_loc(Interval(0, 2, closed='right')) ) + pytest.raises(KeyError, right.get_loc(Interval(2.5, 3, closed='right')) ) + + pytest.raises(KeyError, right.get_loc(Interval(0, 1, closed='left')) ) + pytest.raises(KeyError, right.get_loc(Interval(1, 2, closed='left')) ) + pytest.raises(KeyError, right.get_loc(Interval(2, 3, closed='left')) ) + pytest.raises(KeyError, right.get_loc(Interval(3, 4, closed='left')) ) + pytest.raises(KeyError, right.get_loc(Interval(0, 2, closed='left')) ) + pytest.raises(KeyError, right.get_loc(Interval(2.5, 3, closed='left')) ) + + pytest.raises(KeyError, right.get_loc(Interval(0, 1, closed='both')) ) + pytest.raises(KeyError, right.get_loc(Interval(1, 2, closed='both')) ) + pytest.raises(KeyError, right.get_loc(Interval(2, 3, closed='both')) ) + pytest.raises(KeyError, right.get_loc(Interval(3, 4, closed='both')) ) + pytest.raises(KeyError, right.get_loc(Interval(0, 2, closed='both')) ) + pytest.raises(KeyError, right.get_loc(Interval(2.5, 3, closed='both')) ) + + left = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='left') + + pytest.raises(KeyError, left.get_loc(-0.5) ) + assert left.get_loc(0) == 0 + assert left.get_loc(0.5) == 0 + pytest.raises(KeyError, left.get_loc(1) ) + pytest.raises(KeyError, left.get_loc(1.5) ) + assert left.get_loc(2) == 1 + assert left.get_loc(2.5) == 1 + pytest.raises(KeyError, left.get_loc(3) ) + pytest.raises(KeyError, left.get_loc(3.5) ) + + pytest.raises(KeyError, left.get_loc(Interval(0, 1, closed='right')) ) + pytest.raises(KeyError, left.get_loc(Interval(1, 2, closed='right')) ) + pytest.raises(KeyError, left.get_loc(Interval(2, 3, closed='right')) ) + pytest.raises(KeyError, left.get_loc(Interval(3, 4, closed='right')) ) + pytest.raises(KeyError, left.get_loc(Interval(0, 2, closed='right')) ) + pytest.raises(KeyError, left.get_loc(Interval(2.5, 3, closed='right')) ) + + assert left.get_loc(Interval(0, 1, closed='left')) == 0 + pytest.raises(KeyError, left.get_loc(Interval(1, 2, closed='left')) ) + assert left.get_loc(Interval(2, 3, closed='left')) == 1 + pytest.raises(KeyError, left.get_loc(Interval(3, 4, closed='left')) ) + pytest.raises(KeyError, left.get_loc(Interval(0, 2, closed='left')) ) + pytest.raises(KeyError, left.get_loc(Interval(2.5, 3, closed='left')) ) + + pytest.raises(KeyError, left.get_loc(Interval(0, 1, closed='both')) ) + pytest.raises(KeyError, left.get_loc(Interval(1, 2, closed='both')) ) + pytest.raises(KeyError, left.get_loc(Interval(2, 3, closed='both')) ) + pytest.raises(KeyError, left.get_loc(Interval(3, 4, closed='both')) ) + pytest.raises(KeyError, left.get_loc(Interval(0, 2, closed='both')) ) + pytest.raises(KeyError, left.get_loc(Interval(2.5, 3, closed='both')) ) + + both = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='both') + + pytest.raises(KeyError, both.get_loc(-0.5) ) + assert both.get_loc(0) == 0 + assert both.get_loc(0.5) == 0 + assert both.get_loc(1) == 0 + pytest.raises(KeyError, both.get_loc(1.5) ) + assert both.get_loc(2) == 1 + assert both.get_loc(2.5) == 1 + assert both.get_loc(3) == 1 + pytest.raises(KeyError, both.get_loc(3.5) ) + + pytest.raises(KeyError, both.get_loc(Interval(0, 1, closed='right')) ) + pytest.raises(KeyError, both.get_loc(Interval(1, 2, closed='right')) ) + pytest.raises(KeyError, both.get_loc(Interval(2, 3, closed='right')) ) + pytest.raises(KeyError, both.get_loc(Interval(3, 4, closed='right')) ) + pytest.raises(KeyError, both.get_loc(Interval(0, 2, closed='right')) ) + pytest.raises(KeyError, both.get_loc(Interval(2.5, 3, closed='right')) ) + + pytest.raises(KeyError, both.get_loc(Interval(0, 1, closed='left')) ) + pytest.raises(KeyError, both.get_loc(Interval(1, 2, closed='left')) ) + pytest.raises(KeyError, both.get_loc(Interval(2, 3, closed='left')) ) + pytest.raises(KeyError, both.get_loc(Interval(3, 4, closed='left')) ) + pytest.raises(KeyError, both.get_loc(Interval(0, 2, closed='left')) ) + pytest.raises(KeyError, both.get_loc(Interval(2.5, 3, closed='left')) ) + + assert both.get_loc(Interval(0, 1, closed='both')) == 0 + pytest.raises(KeyError, both.get_loc(Interval(1, 2, closed='both')) ) + assert both.get_loc(Interval(2, 3, closed='both')) == 1 + pytest.raises(KeyError, both.get_loc(Interval(3, 4, closed='both')) ) + pytest.raises(KeyError, both.get_loc(Interval(0, 2, closed='both')) ) + pytest.raises(KeyError, both.get_loc(Interval(2.5, 3, closed='both')) ) + + def slice_locs_cases(self, breaks): # TODO: same tests for more index types index = IntervalIndex.from_breaks([0, 1, 2], closed='right') @@ -491,6 +594,188 @@ def testcontains(self): assert not i.contains(20) assert not i.contains(-20) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_interval_covers(self): + + # class Interval: + # def covers(self, other: Interval) -> bool + # def covers(self, other: IntervalIndex) -> IntegerArray1D + + assert Interval(1, 3).covers(Interval(1.5, 2.5)) + assert Interval(1, 3).covers(Interval(1, 2)) + assert Interval(1, 3).covers(Interval(2, 3)) + assert not Interval(1, 3).covers(Interval(0.5, 2.5)) + assert not Interval(1, 3).covers(Interval(1.5, 3.5)) + + assert Interval(1, 3, closed='right').covers(Interval(1, 3, closed='right')) + assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='left')) + assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='both')) + + assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='left').covers(Interval(1, 3, closed='left')) + assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='both')) + + assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='both')) + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + + assert Interval(1, 3, closed='right').covers(idx) == np.array([1, 2]) + assert Interval(0, 3, closed='right').covers(idx) == np.array([0, 1, 2]) + assert Interval(0, 2, closed='right').covers(idx) == np.array([0]) + assert Interval(2, 4, closed='right').covers(idx) == np.array([1]) + + assert Interval(1, 3, closed='left').covers(idx) == np.array([]) + assert Interval(0, 3, closed='left').covers(idx) == np.array([0]) + assert Interval(0, 2, closed='left').covers(idx) == np.array([0]) + assert Interval(2, 4, closed='left').covers(idx) == np.array([1]) + + assert Interval(1, 3, closed='both').covers(idx) == np.array([1, 2]) + assert Interval(0, 5, closed='both').covers(idx) == np.array([0, 1, 2]) + assert Interval(0, 2, closed='both').covers(idx) == np.array([0]) + assert Interval(2, 4, closed='both').covers(idx) == np.array([1]) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_interval_overlaps(self): + + # class Interval: + # def overlaps(self, other: Interval) -> bool + # def overlaps(self, other: IntervalIndex) -> IntegerArray1D + + assert Interval(1, 3).overlaps(Interval(1.5, 2.5)) + assert Interval(1, 3).overlaps(Interval(1, 2)) + assert Interval(1, 3).overlaps(Interval(2, 3)) + assert Interval(1, 3).overlaps(Interval(0.5, 2.5)) + assert Interval(1, 3).overlaps(Interval(1.5, 3.5)) + + assert not Interval(1, 3).overlaps(Interval(-1, 1)) + assert not Interval(1, 3).overlaps(Interval(3, 5)) + + # right + assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='both')) + + assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='right')) + assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='left')) + assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='both')) + + assert not Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='right')) + assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='left')) + assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='both')) + + # left + assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='both')) + + assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='right')) + assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='left')) + assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='both')) + + assert not Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='right')) + assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='left')) + assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='both')) + + # both + assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='both')) + + assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='right')) + assert not Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='left')) + assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='both')) + + assert not Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='right')) + assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='left')) + assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='both')) + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + + assert Interval(1, 3, closed='right').overlaps(idx) == np.array([1, 2]) + assert Interval(1, 2, closed='right').overlaps(idx) == np.array([2]) + assert Interval(0, 2, closed='right').overlaps(idx) == np.array([0, 2]) + assert Interval(3, 4, closed='right').overlaps(idx) == np.array([]) + + assert Interval(1, 3, closed='left').overlaps(idx) == np.array([0, 1, 2]) + assert Interval(1, 2, closed='left').overlaps(idx) == np.array([0, 2]) + assert Interval(0, 2, closed='left').overlaps(idx) == np.array([0, 2]) + assert Interval(3, 4, closed='left').overlaps(idx) == np.array([3]) + + assert Interval(1, 3, closed='both').overlaps(idx) == np.array([0, 1, 2]) + assert Interval(1, 2, closed='both').overlaps(idx) == np.array([0, 2]) + assert Interval(0, 2, closed='both').overlaps(idx) == np.array([0, 2]) + assert Interval(3, 4, closed='both').overlaps(idx) == np.array([3]) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_intervalIndex_covers(self): + + # class IntervalIndex: + # def covers(self, other: Interval) -> IntegerArray1D + # def covers(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + + assert idx.covers(Interval(1, 3, closed='right')) == np.array([1, 2]) + assert idx.covers(Interval(0, 3, closed='right')) == np.array([0, 1, 2]) + assert idx.covers(Interval(0, 2, closed='right')) == np.array([0]) + assert idx.covers(Interval(2, 4, closed='right')) == np.array([1]) + + assert idx.covers(Interval(1, 3, closed='left')) == np.array([]) + assert idx.covers(Interval(0, 3, closed='left')) == np.array([0]) + assert idx.covers(Interval(0, 2, closed='left')) == np.array([0]) + assert idx.covers(Interval(2, 4, closed='left')) == np.array([1]) + + assert idx.covers(Interval(1, 3, closed='both')) == np.array([1, 2]) + assert idx.covers(Interval(0, 5, closed='both')) == np.array([0, 1, 2]) + assert idx.covers(Interval(0, 2, closed='both')) == np.array([0]) + assert idx.covers(Interval(2, 4, closed='both')) == np.array([1]) + + idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') + idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') + idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') + + assert idx.covers(idx1) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + assert idx.covers(idx2) == (np.array([2]), np.array([1])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + assert idx.covers(idx3) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_intervalIndex_overlaps(self): + + # class IntervalIndex: + # def overlaps(self, other: Interval) -> IntegerArray1D + # def overlaps(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + + assert idx.overlaps(Interval(1, 3, closed='right')) == np.array([1, 2]) + assert idx.overlaps(Interval(1, 2, closed='right')) == np.array([2]) + assert idx.overlaps(Interval(0, 2, closed='right')) == np.array([0, 2]) + assert idx.overlaps(Interval(3, 4, closed='right')) == np.array([]) + + assert idx.overlaps(Interval(1, 3, closed='left')) == np.array([0, 1, 2]) + assert idx.overlaps(Interval(1, 2, closed='left')) == np.array([0, 2]) + assert idx.overlaps(Interval(0, 2, closed='left')) == np.array([0, 2]) + assert idx.overlaps(Interval(3, 4, closed='left')) == np.array([3]) + + assert idx.overlaps(Interval(1, 3, closed='both')) == np.array([0, 1, 2]) + assert idx.overlaps(Interval(1, 2, closed='both')) == np.array([0, 2]) + assert idx.overlaps(Interval(0, 2, closed='both')) == np.array([0, 2]) + assert idx.overlaps(Interval(3, 4, closed='both')) == np.array([3]) + + idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') + idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') + idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') + + assert idx.overlaps(idx1) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + assert idx.overlaps(idx2) == (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + assert idx.overlaps(idx3) == (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + + + def test_dropna(self): expected = IntervalIndex.from_tuples([(0.0, 1.0), (1.0, 2.0)]) diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index e419950b88aed..5a6128e840898 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -129,71 +129,6 @@ def test_with_interval_updated_behavior(self): with pytest.raises(KeyError): s[Interval(5, 6)] - # overlapping but not identical - # right: - with pytest.raises(KeyError): - s.loc[Interval(4, 6)] - - with pytest.raises(KeyError): - s.loc[[Interval(4, 6)]] - - # left: - with pytest.raises(KeyError): - s.loc[Interval(-1, 4)] - - with pytest.raises(KeyError): - s.loc[[Interval(-1, 4)]] - - with pytest.raises(KeyError): - s.loc[Interval(0, 1, closed='left')] - - with pytest.raises(KeyError): - s.loc[[Interval(0, 1, closed='left')]] - - with pytest.raises(KeyError): - s.loc[Interval(0, 1, closed='both')] - - with pytest.raises(KeyError): - s.loc[[Interval(0, 1, closed='both')]] - - # inner - with pytest.raises(KeyError): - s.loc[Interval(2, 4)] - - with pytest.raises(KeyError): - s.loc[[Interval(2, 4)]] - - with pytest.raises(KeyError): - s.loc[Interval(4, 5, closed='left')] - - with pytest.raises(KeyError): - s.loc[[Interval(4, 5, closed='left')]] - - with pytest.raises(KeyError): - s.loc[Interval(4, 5, closed='both')] - - with pytest.raises(KeyError): - s.loc[[Interval(4, 5, closed='both')]] - - # outer - with pytest.raises(KeyError): - s.loc[Interval(-1, 6)] - - with pytest.raises(KeyError): - s.loc[[Interval(-1, 6)]] - - with pytest.raises(KeyError): - s.loc[Interval(0, 6, closed='left')] - - with pytest.raises(KeyError): - s.loc[[Interval(0, 6, closed='left')]] - - with pytest.raises(KeyError): - s.loc[Interval(0, 5, closed='both')] - - with pytest.raises(KeyError): - s.loc[[Interval(0, 5, closed='both')]] - def test_with_slices(self): s = self.s @@ -464,183 +399,3 @@ def test_loc_getitem_frame(self): with pytest.raises(KeyError): df.loc[[10, 4]] - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_covers(self): - - # class Interval: - # def covers(self, other: Interval) -> bool - # def covers(self, other: IntervalIndex) -> IntegerArray1D - - assert Interval(1, 3).covers(Interval(1.5, 2.5)) - assert Interval(1, 3).covers(Interval(1, 2)) - assert Interval(1, 3).covers(Interval(2, 3)) - assert not Interval(1, 3).covers(Interval(0.5, 2.5)) - assert not Interval(1, 3).covers(Interval(1.5, 3.5)) - - assert Interval(1, 3, closed='right').covers(Interval(1, 3, closed='right')) - assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='left')) - assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='both')) - - assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='left').covers(Interval(1, 3, closed='left')) - assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='both')) - - assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='both')) - - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) - - assert Interval(1, 3, closed='right').covers(idx) == np.array([1, 2]) - assert Interval(0, 3, closed='right').covers(idx) == np.array([0, 1, 2]) - assert Interval(0, 2, closed='right').covers(idx) == np.array([0]) - assert Interval(2, 4, closed='right').covers(idx) == np.array([1]) - - assert Interval(1, 3, closed='left').covers(idx) == np.array([]) - assert Interval(0, 3, closed='left').covers(idx) == np.array([0]) - assert Interval(0, 2, closed='left').covers(idx) == np.array([0]) - assert Interval(2, 4, closed='left').covers(idx) == np.array([1]) - - assert Interval(1, 3, closed='both').covers(idx) == np.array([1, 2]) - assert Interval(0, 5, closed='both').covers(idx) == np.array([0, 1, 2]) - assert Interval(0, 2, closed='both').covers(idx) == np.array([0]) - assert Interval(2, 4, closed='both').covers(idx) == np.array([1]) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_overlaps(self): - - # class Interval: - # def overlaps(self, other: Interval) -> bool - # def overlaps(self, other: IntervalIndex) -> IntegerArray1D - - assert Interval(1, 3).overlaps(Interval(1.5, 2.5)) - assert Interval(1, 3).overlaps(Interval(1, 2)) - assert Interval(1, 3).overlaps(Interval(2, 3)) - assert Interval(1, 3).overlaps(Interval(0.5, 2.5)) - assert Interval(1, 3).overlaps(Interval(1.5, 3.5)) - - assert not Interval(1, 3).overlaps(Interval(-1, 1)) - assert not Interval(1, 3).overlaps(Interval(3, 5)) - - # right - assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='both')) - - assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='right')) - assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='left')) - assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='both')) - - assert not Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='right')) - assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='left')) - assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='both')) - - # left - assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='both')) - - assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='right')) - assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='left')) - assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='both')) - - assert not Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='right')) - assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='left')) - assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='both')) - - # both - assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='both')) - - assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='right')) - assert not Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='left')) - assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='both')) - - assert not Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='right')) - assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='left')) - assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='both')) - - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) - - assert Interval(1, 3, closed='right').overlaps(idx) == np.array([1, 2]) - assert Interval(1, 2, closed='right').overlaps(idx) == np.array([2]) - assert Interval(0, 2, closed='right').overlaps(idx) == np.array([0, 2]) - assert Interval(3, 4, closed='right').overlaps(idx) == np.array([]) - - assert Interval(1, 3, closed='left').overlaps(idx) == np.array([0, 1, 2]) - assert Interval(1, 2, closed='left').overlaps(idx) == np.array([0, 2]) - assert Interval(0, 2, closed='left').overlaps(idx) == np.array([0, 2]) - assert Interval(3, 4, closed='left').overlaps(idx) == np.array([3]) - - assert Interval(1, 3, closed='both').overlaps(idx) == np.array([0, 1, 2]) - assert Interval(1, 2, closed='both').overlaps(idx) == np.array([0, 2]) - assert Interval(0, 2, closed='both').overlaps(idx) == np.array([0, 2]) - assert Interval(3, 4, closed='both').overlaps(idx) == np.array([3]) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_covers(self): - - # class IntervalIndex: - # def covers(self, other: Interval) -> IntegerArray1D - # def covers(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] - - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) - - assert idx.covers(Interval(1, 3, closed='right')) == np.array([1, 2]) - assert idx.covers(Interval(0, 3, closed='right')) == np.array([0, 1, 2]) - assert idx.covers(Interval(0, 2, closed='right')) == np.array([0]) - assert idx.covers(Interval(2, 4, closed='right')) == np.array([1]) - - assert idx.covers(Interval(1, 3, closed='left')) == np.array([]) - assert idx.covers(Interval(0, 3, closed='left')) == np.array([0]) - assert idx.covers(Interval(0, 2, closed='left')) == np.array([0]) - assert idx.covers(Interval(2, 4, closed='left')) == np.array([1]) - - assert idx.covers(Interval(1, 3, closed='both')) == np.array([1, 2]) - assert idx.covers(Interval(0, 5, closed='both')) == np.array([0, 1, 2]) - assert idx.covers(Interval(0, 2, closed='both')) == np.array([0]) - assert idx.covers(Interval(2, 4, closed='both')) == np.array([1]) - - idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') - idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - - assert idx.covers(idx1) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - assert idx.covers(idx2) == (np.array([2]), np.array([1])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - assert idx.covers(idx3) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_overlaps(self): - - # class IntervalIndex: - # def overlaps(self, other: Interval) -> IntegerArray1D - # def overlaps(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] - - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) - - assert idx.overlaps(Interval(1, 3, closed='right')) == np.array([1, 2]) - assert idx.overlaps(Interval(1, 2, closed='right')) == np.array([2]) - assert idx.overlaps(Interval(0, 2, closed='right')) == np.array([0, 2]) - assert idx.overlaps(Interval(3, 4, closed='right')) == np.array([]) - - assert idx.overlaps(Interval(1, 3, closed='left')) == np.array([0, 1, 2]) - assert idx.overlaps(Interval(1, 2, closed='left')) == np.array([0, 2]) - assert idx.overlaps(Interval(0, 2, closed='left')) == np.array([0, 2]) - assert idx.overlaps(Interval(3, 4, closed='left')) == np.array([3]) - - assert idx.overlaps(Interval(1, 3, closed='both')) == np.array([0, 1, 2]) - assert idx.overlaps(Interval(1, 2, closed='both')) == np.array([0, 2]) - assert idx.overlaps(Interval(0, 2, closed='both')) == np.array([0, 2]) - assert idx.overlaps(Interval(3, 4, closed='both')) == np.array([3]) - - idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') - idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - - assert idx.overlaps(idx1) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - assert idx.overlaps(idx2) == (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - assert idx.overlaps(idx3) == (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - From a1c3e7afaa4d7fda08b4cd62d79e94bd3d49730c Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 26 May 2017 11:15:46 -0400 Subject: [PATCH 03/56] first pass at @shoyer's review --- pandas/tests/indexes/test_interval.py | 342 +++++++++++++------------ pandas/tests/indexing/test_interval.py | 6 - 2 files changed, 182 insertions(+), 166 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 61fd57a4077b7..29a8c4b3c9c0b 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -349,106 +349,112 @@ def test_get_loc_value(self): pytest.raises(KeyError, idx.get_loc, 1.5) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_loc_value(self): + def test_get_loc_value_closed_right(self): right = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='right') - pytest.raises(KeyError, right.get_loc(-0.5) ) - pytest.raises(KeyError, right.get_loc(0) ) - assert right.get_loc(0.5) == 0 - assert right.get_loc(1) == 0 - pytest.raises(KeyError, right.get_loc(1.5) ) - pytest.raises(KeyError, right.get_loc(2) ) - assert right.get_loc(2.5) == 1 - assert right.get_loc(3) == 1 - pytest.raises(KeyError, right.get_loc(3.5) ) - - assert right.get_loc(Interval(0, 1, closed='right')) == 0 - pytest.raises(KeyError, right.get_loc(Interval(1, 2, closed='right')) ) - assert right.get_loc(Interval(2, 3, closed='right')) == 1 - pytest.raises(KeyError, right.get_loc(Interval(3, 4, closed='right')) ) - pytest.raises(KeyError, right.get_loc(Interval(0, 2, closed='right')) ) - pytest.raises(KeyError, right.get_loc(Interval(2.5, 3, closed='right')) ) - - pytest.raises(KeyError, right.get_loc(Interval(0, 1, closed='left')) ) - pytest.raises(KeyError, right.get_loc(Interval(1, 2, closed='left')) ) - pytest.raises(KeyError, right.get_loc(Interval(2, 3, closed='left')) ) - pytest.raises(KeyError, right.get_loc(Interval(3, 4, closed='left')) ) - pytest.raises(KeyError, right.get_loc(Interval(0, 2, closed='left')) ) - pytest.raises(KeyError, right.get_loc(Interval(2.5, 3, closed='left')) ) - - pytest.raises(KeyError, right.get_loc(Interval(0, 1, closed='both')) ) - pytest.raises(KeyError, right.get_loc(Interval(1, 2, closed='both')) ) - pytest.raises(KeyError, right.get_loc(Interval(2, 3, closed='both')) ) - pytest.raises(KeyError, right.get_loc(Interval(3, 4, closed='both')) ) - pytest.raises(KeyError, right.get_loc(Interval(0, 2, closed='both')) ) - pytest.raises(KeyError, right.get_loc(Interval(2.5, 3, closed='both')) ) + pytest.raises(KeyError, right.get_loc, -0.5) + pytest.raises(KeyError, right.get_loc, 0) + assert right.get_loc(0.5) == 0 + assert right.get_loc(1) == 0 + pytest.raises(KeyError, right.get_loc, 1.5) + pytest.raises(KeyError, right.get_loc, 2) + assert right.get_loc(2.5) == 1 + assert right.get_loc(3) == 1 + pytest.raises(KeyError, right.get_loc, 3.5) + + assert right.get_loc(Interval(0, 1, closed='right')) == 0 + pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='right')) + assert right.get_loc(Interval(2, 3, closed='right')) == 1 + pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='right')) + pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='right')) + pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='right')) + + pytest.raises(KeyError, right.get_loc, Interval(0, 1, closed='left')) + pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='left')) + pytest.raises(KeyError, right.get_loc, Interval(2, 3, closed='left')) + pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='left')) + pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='left')) + pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='left')) + + pytest.raises(KeyError, right.get_loc, Interval(0, 1, closed='both')) + pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='both')) + pytest.raises(KeyError, right.get_loc, Interval(2, 3, closed='both')) + pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='both')) + pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='both')) + pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='both')) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_get_loc_value_closed_left(self): left = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='left') - pytest.raises(KeyError, left.get_loc(-0.5) ) - assert left.get_loc(0) == 0 - assert left.get_loc(0.5) == 0 - pytest.raises(KeyError, left.get_loc(1) ) - pytest.raises(KeyError, left.get_loc(1.5) ) - assert left.get_loc(2) == 1 - assert left.get_loc(2.5) == 1 - pytest.raises(KeyError, left.get_loc(3) ) - pytest.raises(KeyError, left.get_loc(3.5) ) - - pytest.raises(KeyError, left.get_loc(Interval(0, 1, closed='right')) ) - pytest.raises(KeyError, left.get_loc(Interval(1, 2, closed='right')) ) - pytest.raises(KeyError, left.get_loc(Interval(2, 3, closed='right')) ) - pytest.raises(KeyError, left.get_loc(Interval(3, 4, closed='right')) ) - pytest.raises(KeyError, left.get_loc(Interval(0, 2, closed='right')) ) - pytest.raises(KeyError, left.get_loc(Interval(2.5, 3, closed='right')) ) - - assert left.get_loc(Interval(0, 1, closed='left')) == 0 - pytest.raises(KeyError, left.get_loc(Interval(1, 2, closed='left')) ) - assert left.get_loc(Interval(2, 3, closed='left')) == 1 - pytest.raises(KeyError, left.get_loc(Interval(3, 4, closed='left')) ) - pytest.raises(KeyError, left.get_loc(Interval(0, 2, closed='left')) ) - pytest.raises(KeyError, left.get_loc(Interval(2.5, 3, closed='left')) ) - - pytest.raises(KeyError, left.get_loc(Interval(0, 1, closed='both')) ) - pytest.raises(KeyError, left.get_loc(Interval(1, 2, closed='both')) ) - pytest.raises(KeyError, left.get_loc(Interval(2, 3, closed='both')) ) - pytest.raises(KeyError, left.get_loc(Interval(3, 4, closed='both')) ) - pytest.raises(KeyError, left.get_loc(Interval(0, 2, closed='both')) ) - pytest.raises(KeyError, left.get_loc(Interval(2.5, 3, closed='both')) ) + pytest.raises(KeyError, left.get_loc, -0.5) + assert left.get_loc(0) == 0 + assert left.get_loc(0.5) == 0 + pytest.raises(KeyError, left.get_loc, 1) + pytest.raises(KeyError, left.get_loc, 1.5) + assert left.get_loc(2) == 1 + assert left.get_loc(2.5) == 1 + pytest.raises(KeyError, left.get_loc, 3) + pytest.raises(KeyError, left.get_loc, 3.5) + + pytest.raises(KeyError, left.get_loc, Interval(0, 1, closed='right')) + pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='right')) + pytest.raises(KeyError, left.get_loc, Interval(2, 3, closed='right')) + pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='right')) + pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='right')) + pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='right')) + + assert left.get_loc(Interval(0, 1, closed='left')) == 0 + pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='left')) + assert left.get_loc(Interval(2, 3, closed='left')) == 1 + pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='left')) + pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='left')) + pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='left')) + + pytest.raises(KeyError, left.get_loc, Interval(0, 1, closed='both')) + pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='both')) + pytest.raises(KeyError, left.get_loc, Interval(2, 3, closed='both')) + pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='both')) + pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='both')) + pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='both')) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_get_loc_value_closed_both(self): both = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='both') - pytest.raises(KeyError, both.get_loc(-0.5) ) - assert both.get_loc(0) == 0 - assert both.get_loc(0.5) == 0 - assert both.get_loc(1) == 0 - pytest.raises(KeyError, both.get_loc(1.5) ) - assert both.get_loc(2) == 1 - assert both.get_loc(2.5) == 1 - assert both.get_loc(3) == 1 - pytest.raises(KeyError, both.get_loc(3.5) ) - - pytest.raises(KeyError, both.get_loc(Interval(0, 1, closed='right')) ) - pytest.raises(KeyError, both.get_loc(Interval(1, 2, closed='right')) ) - pytest.raises(KeyError, both.get_loc(Interval(2, 3, closed='right')) ) - pytest.raises(KeyError, both.get_loc(Interval(3, 4, closed='right')) ) - pytest.raises(KeyError, both.get_loc(Interval(0, 2, closed='right')) ) - pytest.raises(KeyError, both.get_loc(Interval(2.5, 3, closed='right')) ) - - pytest.raises(KeyError, both.get_loc(Interval(0, 1, closed='left')) ) - pytest.raises(KeyError, both.get_loc(Interval(1, 2, closed='left')) ) - pytest.raises(KeyError, both.get_loc(Interval(2, 3, closed='left')) ) - pytest.raises(KeyError, both.get_loc(Interval(3, 4, closed='left')) ) - pytest.raises(KeyError, both.get_loc(Interval(0, 2, closed='left')) ) - pytest.raises(KeyError, both.get_loc(Interval(2.5, 3, closed='left')) ) - - assert both.get_loc(Interval(0, 1, closed='both')) == 0 - pytest.raises(KeyError, both.get_loc(Interval(1, 2, closed='both')) ) - assert both.get_loc(Interval(2, 3, closed='both')) == 1 - pytest.raises(KeyError, both.get_loc(Interval(3, 4, closed='both')) ) - pytest.raises(KeyError, both.get_loc(Interval(0, 2, closed='both')) ) - pytest.raises(KeyError, both.get_loc(Interval(2.5, 3, closed='both')) ) + pytest.raises(KeyError, both.get_loc, -0.5) + assert both.get_loc(0) == 0 + assert both.get_loc(0.5) == 0 + assert both.get_loc(1) == 0 + pytest.raises(KeyError, both.get_loc, 1.5) + assert both.get_loc(2) == 1 + assert both.get_loc(2.5) == 1 + assert both.get_loc(3) == 1 + pytest.raises(KeyError, both.get_loc, 3.5) + + pytest.raises(KeyError, both.get_loc, Interval(0, 1, closed='right')) + pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='right')) + pytest.raises(KeyError, both.get_loc, Interval(2, 3, closed='right')) + pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='right')) + pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='right')) + pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='right')) + + pytest.raises(KeyError, both.get_loc, Interval(0, 1, closed='left')) + pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='left')) + pytest.raises(KeyError, both.get_loc, Interval(2, 3, closed='left')) + pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='left')) + pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='left')) + pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='left')) + + assert both.get_loc(Interval(0, 1, closed='both')) == 0 + pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='both')) + assert both.get_loc(Interval(2, 3, closed='both')) == 1 + pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='both')) + pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='both')) + pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='both')) def slice_locs_cases(self, breaks): @@ -596,11 +602,10 @@ def testcontains(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_covers(self): + def test_interval_covers_interval(self): # class Interval: # def covers(self, other: Interval) -> bool - # def covers(self, other: IntervalIndex) -> IntegerArray1D assert Interval(1, 3).covers(Interval(1.5, 2.5)) assert Interval(1, 3).covers(Interval(1, 2)) @@ -620,29 +625,34 @@ def test_interval_covers(self): assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='left')) assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='both')) - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_interval_covers_intervalIndex(self): + + # class Interval: + # def covers(self, other: IntervalIndex) -> IntegerArray1D - assert Interval(1, 3, closed='right').covers(idx) == np.array([1, 2]) - assert Interval(0, 3, closed='right').covers(idx) == np.array([0, 1, 2]) - assert Interval(0, 2, closed='right').covers(idx) == np.array([0]) - assert Interval(2, 4, closed='right').covers(idx) == np.array([1]) + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - assert Interval(1, 3, closed='left').covers(idx) == np.array([]) - assert Interval(0, 3, closed='left').covers(idx) == np.array([0]) - assert Interval(0, 2, closed='left').covers(idx) == np.array([0]) - assert Interval(2, 4, closed='left').covers(idx) == np.array([1]) + tm.assert_numpy_array_equal(Interval(1, 3, closed='right').covers(idx), np.array([1, 2])) + tm.assert_numpy_array_equal(Interval(0, 3, closed='right').covers(idx), np.array([0, 1, 2])) + tm.assert_numpy_array_equal(Interval(0, 2, closed='right').covers(idx), np.array([0])) + tm.assert_numpy_array_equal(Interval(2, 4, closed='right').covers(idx), np.array([1])) - assert Interval(1, 3, closed='both').covers(idx) == np.array([1, 2]) - assert Interval(0, 5, closed='both').covers(idx) == np.array([0, 1, 2]) - assert Interval(0, 2, closed='both').covers(idx) == np.array([0]) - assert Interval(2, 4, closed='both').covers(idx) == np.array([1]) + tm.assert_numpy_array_equal(Interval(1, 3, closed='left').covers(idx), np.array([])) + tm.assert_numpy_array_equal(Interval(0, 3, closed='left').covers(idx), np.array([0])) + tm.assert_numpy_array_equal(Interval(0, 2, closed='left').covers(idx), np.array([0])) + tm.assert_numpy_array_equal(Interval(2, 4, closed='left').covers(idx), np.array([1])) + + tm.assert_numpy_array_equal(Interval(1, 3, closed='both').covers(idx), np.array([1, 2])) + tm.assert_numpy_array_equal(Interval(0, 5, closed='both').covers(idx), np.array([0, 1, 2])) + tm.assert_numpy_array_equal(Interval(0, 2, closed='both').covers(idx), np.array([0])) + tm.assert_numpy_array_equal(Interval(2, 4, closed='both').covers(idx), np.array([1])) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_overlaps(self): + def test_interval_overlaps_interval(self): # class Interval: # def overlaps(self, other: Interval) -> bool - # def overlaps(self, other: IntervalIndex) -> IntegerArray1D assert Interval(1, 3).overlaps(Interval(1.5, 2.5)) assert Interval(1, 3).overlaps(Interval(1, 2)) @@ -692,89 +702,101 @@ def test_interval_overlaps(self): assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='left')) assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='both')) - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_interval_overlaps_intervalIndex(self): + # class Interval: + # def overlaps(self, other: IntervalIndex) -> IntegerArray1D + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - assert Interval(1, 3, closed='right').overlaps(idx) == np.array([1, 2]) - assert Interval(1, 2, closed='right').overlaps(idx) == np.array([2]) - assert Interval(0, 2, closed='right').overlaps(idx) == np.array([0, 2]) - assert Interval(3, 4, closed='right').overlaps(idx) == np.array([]) + tm.assert_numpy_array_equal(Interval(1, 3, closed='right').overlaps(idx), np.array([1, 2])) + tm.assert_numpy_array_equal(Interval(1, 2, closed='right').overlaps(idx), np.array([2])) + tm.assert_numpy_array_equal(Interval(0, 2, closed='right').overlaps(idx), np.array([0, 2])) + tm.assert_numpy_array_equal(Interval(3, 4, closed='right').overlaps(idx), np.array([])) - assert Interval(1, 3, closed='left').overlaps(idx) == np.array([0, 1, 2]) - assert Interval(1, 2, closed='left').overlaps(idx) == np.array([0, 2]) - assert Interval(0, 2, closed='left').overlaps(idx) == np.array([0, 2]) - assert Interval(3, 4, closed='left').overlaps(idx) == np.array([3]) + tm.assert_numpy_array_equal(Interval(1, 3, closed='left').overlaps(idx), np.array([0, 1, 2])) + tm.assert_numpy_array_equal(Interval(1, 2, closed='left').overlaps(idx), np.array([0, 2])) + tm.assert_numpy_array_equal(Interval(0, 2, closed='left').overlaps(idx), np.array([0, 2])) + tm.assert_numpy_array_equal(Interval(3, 4, closed='left').overlaps(idx), np.array([3])) - assert Interval(1, 3, closed='both').overlaps(idx) == np.array([0, 1, 2]) - assert Interval(1, 2, closed='both').overlaps(idx) == np.array([0, 2]) - assert Interval(0, 2, closed='both').overlaps(idx) == np.array([0, 2]) - assert Interval(3, 4, closed='both').overlaps(idx) == np.array([3]) + tm.assert_numpy_array_equal(Interval(1, 3, closed='both').overlaps(idx), np.array([0, 1, 2])) + tm.assert_numpy_array_equal(Interval(1, 2, closed='both').overlaps(idx), np.array([0, 2])) + tm.assert_numpy_array_equal(Interval(0, 2, closed='both').overlaps(idx), np.array([0, 2])) + tm.assert_numpy_array_equal(Interval(3, 4, closed='both').overlaps(idx), np.array([3])) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_covers(self): + def test_intervalIndex_covers_interval(self): # class IntervalIndex: # def covers(self, other: Interval) -> IntegerArray1D - # def covers(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') + + tm.assert_numpy_array_equal(idx.covers(Interval(1, 3, closed='right')), np.array([1, 2])) + tm.assert_numpy_array_equal(idx.covers(Interval(0, 3, closed='right')), np.array([0, 1, 2])) + tm.assert_numpy_array_equal(idx.covers(Interval(0, 2, closed='right')), np.array([0])) + tm.assert_numpy_array_equal(idx.covers(Interval(2, 4, closed='right')), np.array([1])) - assert idx.covers(Interval(1, 3, closed='right')) == np.array([1, 2]) - assert idx.covers(Interval(0, 3, closed='right')) == np.array([0, 1, 2]) - assert idx.covers(Interval(0, 2, closed='right')) == np.array([0]) - assert idx.covers(Interval(2, 4, closed='right')) == np.array([1]) + tm.assert_numpy_array_equal(idx.covers(Interval(1, 3, closed='left')), np.array([])) + tm.assert_numpy_array_equal(idx.covers(Interval(0, 3, closed='left')), np.array([0])) + tm.assert_numpy_array_equal(idx.covers(Interval(0, 2, closed='left')), np.array([0])) + tm.assert_numpy_array_equal(idx.covers(Interval(2, 4, closed='left')), np.array([1])) - assert idx.covers(Interval(1, 3, closed='left')) == np.array([]) - assert idx.covers(Interval(0, 3, closed='left')) == np.array([0]) - assert idx.covers(Interval(0, 2, closed='left')) == np.array([0]) - assert idx.covers(Interval(2, 4, closed='left')) == np.array([1]) + tm.assert_numpy_array_equal(idx.covers(Interval(1, 3, closed='both')), np.array([1, 2])) + tm.assert_numpy_array_equal(idx.covers(Interval(0, 5, closed='both')), np.array([0, 1, 2])) + tm.assert_numpy_array_equal(idx.covers(Interval(0, 2, closed='both')), np.array([0])) + tm.assert_numpy_array_equal(idx.covers(Interval(2, 4, closed='both')), np.array([1])) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_intervalIndex_covers_intervalIndex(self): - assert idx.covers(Interval(1, 3, closed='both')) == np.array([1, 2]) - assert idx.covers(Interval(0, 5, closed='both')) == np.array([0, 1, 2]) - assert idx.covers(Interval(0, 2, closed='both')) == np.array([0]) - assert idx.covers(Interval(2, 4, closed='both')) == np.array([1]) + # class IntervalIndex: + # def covers(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - assert idx.covers(idx1) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - assert idx.covers(idx2) == (np.array([2]), np.array([1])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - assert idx.covers(idx3) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - + tm.assert_numpy_array_equal(idx.covers(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + tm.assert_numpy_array_equal(idx.covers(idx2), (np.array([2]), np.array([1]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + tm.assert_numpy_array_equal(idx.covers(idx3), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_overlaps(self): + def test_intervalIndex_overlaps_interval(self): # class IntervalIndex: # def overlaps(self, other: Interval) -> IntegerArray1D - # def overlaps(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)]) + idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - assert idx.overlaps(Interval(1, 3, closed='right')) == np.array([1, 2]) - assert idx.overlaps(Interval(1, 2, closed='right')) == np.array([2]) - assert idx.overlaps(Interval(0, 2, closed='right')) == np.array([0, 2]) - assert idx.overlaps(Interval(3, 4, closed='right')) == np.array([]) + tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 3, closed='right')), np.array([1, 2])) + tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 2, closed='right')), np.array([2])) + tm.assert_numpy_array_equal(idx.overlaps(Interval(0, 2, closed='right')), np.array([0, 2])) + tm.assert_numpy_array_equal(idx.overlaps(Interval(3, 4, closed='right')), np.array([])) - assert idx.overlaps(Interval(1, 3, closed='left')) == np.array([0, 1, 2]) - assert idx.overlaps(Interval(1, 2, closed='left')) == np.array([0, 2]) - assert idx.overlaps(Interval(0, 2, closed='left')) == np.array([0, 2]) - assert idx.overlaps(Interval(3, 4, closed='left')) == np.array([3]) + tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 3, closed='left')), np.array([0, 1, 2])) + tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 2, closed='left')), np.array([0, 2])) + tm.assert_numpy_array_equal(idx.overlaps(Interval(0, 2, closed='left')), np.array([0, 2])) + tm.assert_numpy_array_equal(idx.overlaps(Interval(3, 4, closed='left')), np.array([3])) - assert idx.overlaps(Interval(1, 3, closed='both')) == np.array([0, 1, 2]) - assert idx.overlaps(Interval(1, 2, closed='both')) == np.array([0, 2]) - assert idx.overlaps(Interval(0, 2, closed='both')) == np.array([0, 2]) - assert idx.overlaps(Interval(3, 4, closed='both')) == np.array([3]) + tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 3, closed='both')), np.array([0, 1, 2])) + tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 2, closed='both')), np.array([0, 2])) + tm.assert_numpy_array_equal(idx.overlaps(Interval(0, 2, closed='both')), np.array([0, 2])) + tm.assert_numpy_array_equal(idx.overlaps(Interval(3, 4, closed='both')), np.array([3])) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_intervalIndex_overlaps_intervalIndex(self): + + # class IntervalIndex: + # def overlaps(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - assert idx.overlaps(idx1) == (np.array([0,1,2,2]), np.array([0,1,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - assert idx.overlaps(idx2) == (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - assert idx.overlaps(idx3) == (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2])) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - - + tm.assert_numpy_array_equal(idx.overlaps(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + tm.assert_numpy_array_equal(idx.overlaps(idx2), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + tm.assert_numpy_array_equal(idx.overlaps(idx3), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... def test_dropna(self): diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index 5a6128e840898..f7bd9b82f4fa5 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -269,12 +269,6 @@ def test_non_unique(self): expected = s.iloc[0:1] tm.assert_series_equal(expected, result) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_non_unique_updated_behavior(self): - - # Actually I think we should remove this test? Not sure what exactly it's meant to gauge... - pass - def test_non_unique_moar(self): idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) From 557d701be1ddb9563dd40ab918f67618f623d63b Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 26 May 2017 11:33:50 -0400 Subject: [PATCH 04/56] quick update comments --- pandas/tests/indexes/test_interval.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 29a8c4b3c9c0b..3ae662245d461 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -757,9 +757,9 @@ def test_intervalIndex_covers_intervalIndex(self): idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - tm.assert_numpy_array_equal(idx.covers(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - tm.assert_numpy_array_equal(idx.covers(idx2), (np.array([2]), np.array([1]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - tm.assert_numpy_array_equal(idx.covers(idx3), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + tm.assert_numpy_array_equal(idx.covers(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays + tm.assert_numpy_array_equal(idx.covers(idx2), (np.array([2]), np.array([1]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays + tm.assert_numpy_array_equal(idx.covers(idx3), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_intervalIndex_overlaps_interval(self): @@ -794,9 +794,9 @@ def test_intervalIndex_overlaps_intervalIndex(self): idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - tm.assert_numpy_array_equal(idx.overlaps(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - tm.assert_numpy_array_equal(idx.overlaps(idx2), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... - tm.assert_numpy_array_equal(idx.overlaps(idx3), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) # note: I had to choose an arbitrary ordering. If this test fails, double check the test too... + tm.assert_numpy_array_equal(idx.overlaps(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays + tm.assert_numpy_array_equal(idx.overlaps(idx2), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays + tm.assert_numpy_array_equal(idx.overlaps(idx3), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays def test_dropna(self): From 7d28038a933574c67c9c578b0d29bd6a03d17817 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 6 Jun 2017 16:30:54 -0400 Subject: [PATCH 05/56] another quick pass --- pandas/tests/indexes/test_interval.py | 85 ++++++++++++++++++++------- 1 file changed, 64 insertions(+), 21 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 3ae662245d461..5dda6130325a8 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -349,7 +349,7 @@ def test_get_loc_value(self): pytest.raises(KeyError, idx.get_loc, 1.5) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_loc_value_closed_right(self): + def test_get_loc_value_closed_right_updated_behavior(self): right = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='right') @@ -385,7 +385,7 @@ def test_get_loc_value_closed_right(self): pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='both')) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_loc_value_closed_left(self): + def test_get_loc_value_closed_left_updated_behavior(self): left = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='left') @@ -421,7 +421,7 @@ def test_get_loc_value_closed_left(self): pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='both')) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_loc_value_closed_both(self): + def test_get_loc_value_closed_both_updated_behavior(self): both = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='both') @@ -459,7 +459,7 @@ def test_get_loc_value_closed_both(self): def slice_locs_cases(self, breaks): # TODO: same tests for more index types - index = IntervalIndex.from_breaks([0, 1, 2], closed='right') + index = IntervalIndex.from_breaks(breaks, closed='right') assert index.slice_locs() == (0, 2) assert index.slice_locs(0, 1) == (0, 1) assert index.slice_locs(1, 1) == (0, 1) @@ -473,14 +473,14 @@ def slice_locs_cases(self, breaks): assert index.slice_locs(end=1.0) == (0, 1) assert index.slice_locs(-1, -1) == (0, 0) - index = IntervalIndex.from_breaks([0, 1, 2], closed='neither') + index = IntervalIndex.from_breaks(breaks, closed='neither') assert index.slice_locs(0, 1) == (0, 1) assert index.slice_locs(0, 2) == (0, 2) assert index.slice_locs(0.5, 1.5) == (0, 2) assert index.slice_locs(1, 1) == (1, 1) assert index.slice_locs(1, 2) == (1, 2) - index = IntervalIndex.from_breaks([0, 1, 2], closed='both') + index = IntervalIndex.from_breaks(breaks, closed='both') assert index.slice_locs(1, 1) == (0, 2) assert index.slice_locs(1, 2) == (0, 2) @@ -514,14 +514,6 @@ def test_slice_locs_fails(self): with pytest.raises(KeyError): index.slice_locs(1, 2) - def test_get_loc_interval(self): - assert self.index.get_loc(Interval(0, 1)) == 0 - assert self.index.get_loc(Interval(0, 0.5)) == 0 - assert self.index.get_loc(Interval(0, 1, 'left')) == 0 - pytest.raises(KeyError, self.index.get_loc, Interval(2, 3)) - pytest.raises(KeyError, self.index.get_loc, - Interval(-1, 0, 'left')) - def test_get_indexer(self): actual = self.index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) expected = np.array([-1, -1, 0, 0, 1, 1, -1], dtype='intp') @@ -532,6 +524,7 @@ def test_get_indexer(self): tm.assert_numpy_array_equal(actual, expected) index = IntervalIndex.from_breaks([0, 1, 2], closed='left') + actual = index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) expected = np.array([-1, 0, 0, 1, 1, -1, -1], dtype='intp') tm.assert_numpy_array_equal(actual, expected) @@ -544,10 +537,35 @@ def test_get_indexer(self): expected = np.array([-1, 1], dtype='intp') tm.assert_numpy_array_equal(actual, expected) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_get_indexer_updated_behavior(self): + actual = self.index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) + expected = np.array([-1, -1, 0, 0, 1, 1, -1], dtype='intp') + tm.assert_numpy_array_equal(actual, expected) + + actual = self.index.get_indexer(self.index) + expected = np.array([0, 1], dtype='intp') + tm.assert_numpy_array_equal(actual, expected) + + index = IntervalIndex.from_breaks([0, 1, 2], closed='left') + + actual = index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) + expected = np.array([-1, 0, 0, 1, 1, -1, -1], dtype='intp') + tm.assert_numpy_array_equal(actual, expected) + + # actual = self.index.get_indexer(index[:1]) + # expected = np.array([0], dtype='intp') + # tm.assert_numpy_array_equal(actual, expected) + + # actual = self.index.get_indexer(index) + # expected = np.array([-1, 1], dtype='intp') + # tm.assert_numpy_array_equal(actual, expected) # these two cases need fixing. + def test_get_indexer_subintervals(self): # TODO: is this right? # return indexers for wholly contained subintervals + # When does this method even get called? target = IntervalIndex.from_breaks(np.linspace(0, 2, 5)) actual = self.index.get_indexer(target) expected = np.array([0, 0, 1, 1], dtype='p') @@ -567,6 +585,30 @@ def test_get_indexer_subintervals(self): expected = np.array([0, 0, 0], dtype='intp') tm.assert_numpy_array_equal(actual, expected) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_get_indexer_subintervals_updated_behavior(self): + + # target = IntervalIndex.from_breaks(np.linspace(0, 2, 5)) + # actual = self.index.get_indexer(target) + # expected = np.array([0, 0, 1, 1], dtype='p') + # tm.assert_numpy_array_equal(actual, expected) + + # target = IntervalIndex.from_breaks([0, 0.67, 1.33, 2]) + # actual = self.index.get_indexer(target) + # expected = np.array([0, 0, 1, 1], dtype='intp') + # tm.assert_numpy_array_equal(actual, expected) + + # actual = self.index.get_indexer(target[[0, -1]]) + # expected = np.array([0, 1], dtype='intp') + # tm.assert_numpy_array_equal(actual, expected) + + # target = IntervalIndex.from_breaks([0, 0.33, 0.67, 1], closed='left') + # actual = self.index.get_indexer(target) + # expected = np.array([0, 0, 0], dtype='intp') + # tm.assert_numpy_array_equal(actual, expected) + + # a good chance this whole method needs reworking. + def test_contains(self): # Only endpoints are valid. i = IntervalIndex.from_arrays([0, 1], [1, 2]) @@ -600,9 +642,10 @@ def testcontains(self): assert not i.contains(20) assert not i.contains(-20) + # Why are there two tests called test contains? this should be cleared up a little, no? @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_covers_interval(self): + def test_interval_covers_interval_updated_behavior(self): # class Interval: # def covers(self, other: Interval) -> bool @@ -626,7 +669,7 @@ def test_interval_covers_interval(self): assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='both')) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_covers_intervalIndex(self): + def test_interval_covers_intervalIndex_updated_behavior(self): # class Interval: # def covers(self, other: IntervalIndex) -> IntegerArray1D @@ -649,7 +692,7 @@ def test_interval_covers_intervalIndex(self): tm.assert_numpy_array_equal(Interval(2, 4, closed='both').covers(idx), np.array([1])) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_overlaps_interval(self): + def test_interval_overlaps_interval_updated_behavior(self): # class Interval: # def overlaps(self, other: Interval) -> bool @@ -703,7 +746,7 @@ def test_interval_overlaps_interval(self): assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='both')) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_overlaps_intervalIndex(self): + def test_interval_overlaps_intervalIndex_updated_behavior(self): # class Interval: # def overlaps(self, other: IntervalIndex) -> IntegerArray1D @@ -725,7 +768,7 @@ def test_interval_overlaps_intervalIndex(self): tm.assert_numpy_array_equal(Interval(3, 4, closed='both').overlaps(idx), np.array([3])) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_covers_interval(self): + def test_intervalIndex_covers_interval_updated_behavior(self): # class IntervalIndex: # def covers(self, other: Interval) -> IntegerArray1D @@ -762,7 +805,7 @@ def test_intervalIndex_covers_intervalIndex(self): tm.assert_numpy_array_equal(idx.covers(idx3), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_overlaps_interval(self): + def test_intervalIndex_overlaps_interval_updated_behavior(self): # class IntervalIndex: # def overlaps(self, other: Interval) -> IntegerArray1D @@ -785,7 +828,7 @@ def test_intervalIndex_overlaps_interval(self): tm.assert_numpy_array_equal(idx.overlaps(Interval(3, 4, closed='both')), np.array([3])) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_overlaps_intervalIndex(self): + def test_intervalIndex_overlaps_intervalIndex_updated_behavior(self): # class IntervalIndex: # def overlaps(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] From ff1fbf2b31b403ecee474c9a927f0ea13d6b295a Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 6 Jun 2017 17:17:42 -0400 Subject: [PATCH 06/56] sanity check --- pandas/tests/indexes/test_interval.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 5dda6130325a8..55f0444523eba 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -644,6 +644,7 @@ def testcontains(self): # Why are there two tests called test contains? this should be cleared up a little, no? + @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_interval_covers_interval_updated_behavior(self): From aadfdcd486f52d536ee6d3d96681c2e2d885b1a4 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Wed, 7 Jun 2017 09:34:48 -0400 Subject: [PATCH 07/56] fixed one little issue. --- pandas/tests/indexes/test_interval.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 55f0444523eba..8aee8a4c473fc 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -15,6 +15,13 @@ class TestIntervalIndex(Base): _holder = IntervalIndex + def _compare_tuple_of_numpy_array(self, result, expected): + lidx, ridx = result + lidx_expected, ridx_expected = expected + + tm.assert_numpy_array_equal(lidx, lidx_expected) + tm.assert_numpy_array_equal(ridx, ridx_expected) + def setup_method(self, method): self.index = IntervalIndex.from_arrays([0, 1], [1, 2]) self.index_with_nan = IntervalIndex.from_tuples( @@ -588,6 +595,8 @@ def test_get_indexer_subintervals(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_indexer_subintervals_updated_behavior(self): + # a good chance this whole method needs reworking. + # target = IntervalIndex.from_breaks(np.linspace(0, 2, 5)) # actual = self.index.get_indexer(target) # expected = np.array([0, 0, 1, 1], dtype='p') @@ -607,8 +616,6 @@ def test_get_indexer_subintervals_updated_behavior(self): # expected = np.array([0, 0, 0], dtype='intp') # tm.assert_numpy_array_equal(actual, expected) - # a good chance this whole method needs reworking. - def test_contains(self): # Only endpoints are valid. i = IntervalIndex.from_arrays([0, 1], [1, 2]) @@ -748,6 +755,7 @@ def test_interval_overlaps_interval_updated_behavior(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_interval_overlaps_intervalIndex_updated_behavior(self): + # class Interval: # def overlaps(self, other: IntervalIndex) -> IntegerArray1D @@ -801,9 +809,9 @@ def test_intervalIndex_covers_intervalIndex(self): idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - tm.assert_numpy_array_equal(idx.covers(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays - tm.assert_numpy_array_equal(idx.covers(idx2), (np.array([2]), np.array([1]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays - tm.assert_numpy_array_equal(idx.covers(idx3), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays + self._compare_tuple_of_numpy_array(idx.covers(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) + self._compare_tuple_of_numpy_array(idx.covers(idx2), (np.array([2]), np.array([1]))) + self._compare_tuple_of_numpy_array(idx.covers(idx3), (np.array([0,1,2,2]), np.array([0,1,1,2]))) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_intervalIndex_overlaps_interval_updated_behavior(self): @@ -838,9 +846,9 @@ def test_intervalIndex_overlaps_intervalIndex_updated_behavior(self): idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - tm.assert_numpy_array_equal(idx.overlaps(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays - tm.assert_numpy_array_equal(idx.overlaps(idx2), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays - tm.assert_numpy_array_equal(idx.overlaps(idx3), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) # note: assert_numpy_array_equal is the wrong thing to call here, since we're testing for equality with a tuple of np arrays + self._compare_tuple_of_numpy_array(idx.overlaps(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) + self._compare_tuple_of_numpy_array(idx.overlaps(idx2), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) + self._compare_tuple_of_numpy_array(idx.overlaps(idx3), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) def test_dropna(self): From c7f6fb8194775f18f514fa42cad316459dbc0ae2 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 19 Jun 2017 10:56:42 -0400 Subject: [PATCH 08/56] fixed most of @jorisvandenbossche's review --- pandas/tests/indexes/test_interval.py | 70 ++++++++++++++------------ pandas/tests/indexing/test_interval.py | 44 +++------------- 2 files changed, 46 insertions(+), 68 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 8aee8a4c473fc..bb078e63346cc 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -376,6 +376,7 @@ def test_get_loc_value_closed_right_updated_behavior(self): pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='right')) pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='right')) pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='right')) + pytest.raises(KeyError, right.get_loc, Interval(-1, 4, closed='right')) pytest.raises(KeyError, right.get_loc, Interval(0, 1, closed='left')) pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='left')) @@ -383,6 +384,7 @@ def test_get_loc_value_closed_right_updated_behavior(self): pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='left')) pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='left')) pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='left')) + pytest.raises(KeyError, right.get_loc, Interval(-1, 4, closed='left')) pytest.raises(KeyError, right.get_loc, Interval(0, 1, closed='both')) pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='both')) @@ -390,6 +392,7 @@ def test_get_loc_value_closed_right_updated_behavior(self): pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='both')) pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='both')) pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='both')) + pytest.raises(KeyError, right.get_loc, Interval(-1, 4, closed='both')) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_loc_value_closed_left_updated_behavior(self): @@ -412,6 +415,7 @@ def test_get_loc_value_closed_left_updated_behavior(self): pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='right')) pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='right')) pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='right')) + pytest.raises(KeyError, left.get_loc, Interval(-1, 4, closed='right')) assert left.get_loc(Interval(0, 1, closed='left')) == 0 pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='left')) @@ -419,6 +423,7 @@ def test_get_loc_value_closed_left_updated_behavior(self): pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='left')) pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='left')) pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='left')) + pytest.raises(KeyError, left.get_loc, Interval(-1, 4, closed='left')) pytest.raises(KeyError, left.get_loc, Interval(0, 1, closed='both')) pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='both')) @@ -426,6 +431,7 @@ def test_get_loc_value_closed_left_updated_behavior(self): pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='both')) pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='both')) pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='both')) + pytest.raises(KeyError, left.get_loc, Interval(-1, 4, closed='both')) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_loc_value_closed_both_updated_behavior(self): @@ -448,6 +454,7 @@ def test_get_loc_value_closed_both_updated_behavior(self): pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='right')) pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='right')) pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='right')) + pytest.raises(KeyError, both.get_loc, Interval(-1, 4, closed='right')) pytest.raises(KeyError, both.get_loc, Interval(0, 1, closed='left')) pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='left')) @@ -455,6 +462,7 @@ def test_get_loc_value_closed_both_updated_behavior(self): pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='left')) pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='left')) pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='left')) + pytest.raises(KeyError, both.get_loc, Interval(-1, 4, closed='left')) assert both.get_loc(Interval(0, 1, closed='both')) == 0 pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='both')) @@ -462,7 +470,7 @@ def test_get_loc_value_closed_both_updated_behavior(self): pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='both')) pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='both')) pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='both')) - + pytest.raises(KeyError, both.get_loc, Interval(-1, 4, closed='both')) def slice_locs_cases(self, breaks): # TODO: same tests for more index types @@ -658,23 +666,23 @@ def test_interval_covers_interval_updated_behavior(self): # class Interval: # def covers(self, other: Interval) -> bool - assert Interval(1, 3).covers(Interval(1.5, 2.5)) - assert Interval(1, 3).covers(Interval(1, 2)) - assert Interval(1, 3).covers(Interval(2, 3)) + assert Interval(1, 3).covers(Interval(1.5, 2.5)) + assert Interval(1, 3).covers(Interval(1, 2)) + assert Interval(1, 3).covers(Interval(2, 3)) assert not Interval(1, 3).covers(Interval(0.5, 2.5)) assert not Interval(1, 3).covers(Interval(1.5, 3.5)) - assert Interval(1, 3, closed='right').covers(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='right').covers(Interval(1, 3, closed='right')) assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='left')) assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='both')) assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='left').covers(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='left').covers(Interval(1, 3, closed='left')) assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='both')) - assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='both')) + assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='both')) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_interval_covers_intervalIndex_updated_behavior(self): @@ -705,53 +713,53 @@ def test_interval_overlaps_interval_updated_behavior(self): # class Interval: # def overlaps(self, other: Interval) -> bool - assert Interval(1, 3).overlaps(Interval(1.5, 2.5)) - assert Interval(1, 3).overlaps(Interval(1, 2)) - assert Interval(1, 3).overlaps(Interval(2, 3)) - assert Interval(1, 3).overlaps(Interval(0.5, 2.5)) - assert Interval(1, 3).overlaps(Interval(1.5, 3.5)) + assert Interval(1, 3).overlaps(Interval(1.5, 2.5)) + assert Interval(1, 3).overlaps(Interval(1, 2)) + assert Interval(1, 3).overlaps(Interval(2, 3)) + assert Interval(1, 3).overlaps(Interval(0.5, 2.5)) + assert Interval(1, 3).overlaps(Interval(1.5, 3.5)) assert not Interval(1, 3).overlaps(Interval(-1, 1)) assert not Interval(1, 3).overlaps(Interval(3, 5)) # right - assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='both')) + assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='both')) assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='right')) assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='left')) assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='both')) assert not Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='right')) - assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='left')) - assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='both')) + assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='left')) + assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='both')) # left - assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='both')) + assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='both')) assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='right')) assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='left')) assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='both')) assert not Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='right')) - assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='left')) - assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='both')) + assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='left')) + assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='both')) # both - assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='both')) + assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='right')) + assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='left')) + assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='both')) - assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='right')) + assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='right')) assert not Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='left')) - assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='both')) + assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='both')) assert not Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='right')) - assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='left')) - assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='both')) + assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='left')) + assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='both')) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_interval_overlaps_intervalIndex_updated_behavior(self): diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index f7bd9b82f4fa5..00f48c11958e7 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -299,48 +299,18 @@ def test_non_unique_moar_updated_behavior(self): result = s.loc[Interval(1, 3)] tm.assert_series_equal(expected, result) - # non-unique index and slices not allowed - with pytest.raises(ValueError): - s.loc[Interval(1, 3):] - # this is confusing to me. I would have done: - # expected = s - # result = s.loc[Interval(1, 3):] - # tm.assert_series_equal(expected, result) - - with pytest.raises(ValueError): - s[Interval(1, 3):] - # Same here: - # expected = s - # result = s[Interval(1, 3):] - # tm.assert_series_equal(expected, result) - - # non-unique - with pytest.raises(ValueError): - s[[Interval(1, 3)]] - # not sure why the behavior for [[]] is different than []... - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_non_unique_moar_updated_behavior(self): + expected = s + result = s.loc[Interval(1, 3):] + tm.assert_series_equal(expected, result) - idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) - s = Series(range(len(idx)), index=idx) + expected = s + result = s[Interval(1, 3):] + tm.assert_series_equal(expected, result) - result = s.loc[Interval(1, 3)] expected = s.iloc[[0, 1]] + result = s[[Interval(1, 3)]] tm.assert_series_equal(expected, result) - # non-unique index and slices not allowed - with pytest.raises(ValueError): - s.loc[Interval(1, 3):] - - with pytest.raises(ValueError): - s[Interval(1, 3):] - - # non-unique - with pytest.raises(ValueError): - s[[Interval(1, 3)]] - - def test_non_matching(self): s = self.s From 1379b08ea2e5d3696a3259f01a603507f376826c Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 19 Jun 2017 16:37:46 -0400 Subject: [PATCH 09/56] worked on 'contains' --- pandas/tests/indexes/test_interval.py | 65 +++++++++++++++------------ 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index bb078e63346cc..e3aeb8c641d25 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -568,19 +568,14 @@ def test_get_indexer_updated_behavior(self): expected = np.array([-1, 0, 0, 1, 1, -1, -1], dtype='intp') tm.assert_numpy_array_equal(actual, expected) - # actual = self.index.get_indexer(index[:1]) - # expected = np.array([0], dtype='intp') - # tm.assert_numpy_array_equal(actual, expected) - - # actual = self.index.get_indexer(index) - # expected = np.array([-1, 1], dtype='intp') - # tm.assert_numpy_array_equal(actual, expected) # these two cases need fixing. + # TODO: To be fleshed out: + # IntervalIndex.get_indexer(Interval) + # IntervalIndex.get_indexer(IntervalIndex) def test_get_indexer_subintervals(self): # TODO: is this right? # return indexers for wholly contained subintervals - # When does this method even get called? target = IntervalIndex.from_breaks(np.linspace(0, 2, 5)) actual = self.index.get_indexer(target) expected = np.array([0, 0, 1, 1], dtype='p') @@ -603,26 +598,9 @@ def test_get_indexer_subintervals(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_indexer_subintervals_updated_behavior(self): - # a good chance this whole method needs reworking. - - # target = IntervalIndex.from_breaks(np.linspace(0, 2, 5)) - # actual = self.index.get_indexer(target) - # expected = np.array([0, 0, 1, 1], dtype='p') - # tm.assert_numpy_array_equal(actual, expected) - - # target = IntervalIndex.from_breaks([0, 0.67, 1.33, 2]) - # actual = self.index.get_indexer(target) - # expected = np.array([0, 0, 1, 1], dtype='intp') - # tm.assert_numpy_array_equal(actual, expected) - - # actual = self.index.get_indexer(target[[0, -1]]) - # expected = np.array([0, 1], dtype='intp') - # tm.assert_numpy_array_equal(actual, expected) + # this whole method needs reworking. + pass - # target = IntervalIndex.from_breaks([0, 0.33, 0.67, 1], closed='left') - # actual = self.index.get_indexer(target) - # expected = np.array([0, 0, 0], dtype='intp') - # tm.assert_numpy_array_equal(actual, expected) def test_contains(self): # Only endpoints are valid. @@ -657,8 +635,39 @@ def testcontains(self): assert not i.contains(20) assert not i.contains(-20) - # Why are there two tests called test contains? this should be cleared up a little, no? + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_contains_updated_behavior(self): + + # this method will replace both of the above + + index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right') + + assert 0 not in index + assert 1 in index # not sure how we want to do this. + assert 2 in index # does this defer to "index.contains()" ? + + assert Interval(0, 1, closed='right') in index + assert Interval(0, 2, closed='right') not in index + assert Interval(0, 0.5, closed='right') not in index + assert Interval(3, 5, closed='right') not in index + assert Interval(-1, 0, closed='left') not in index + assert Interval(0, 1, closed='left') not in index + assert Interval(0, 1, closed='both') not in index + + assert not index.contains(0) + assert index.contains(0.1) + assert index.contains(0.5) + assert index.contains(1) + + assert index.contains(Interval(0, 1), closed='right') + assert not index.contains(Interval(0, 1), closed='left') + assert not index.contains(Interval(0, 2), closed='right') + + assert not index.contains(Interval(0, 3), closed='right') + assert not index.contains(Interval(1, 3), closed='right') + assert not index.contains(20) + assert not index.contains(-20) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_interval_covers_interval_updated_behavior(self): From 80ebeb3a5bdeab3227acfba0ede833b8e21b4ffa Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 19 Jun 2017 16:39:17 -0400 Subject: [PATCH 10/56] remove covers and overlaps -- to be added to a separate PR --- pandas/tests/indexes/test_interval.py | 198 -------------------------- 1 file changed, 198 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index e3aeb8c641d25..042e92681cb88 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -669,204 +669,6 @@ def test_contains_updated_behavior(self): assert not index.contains(20) assert not index.contains(-20) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_covers_interval_updated_behavior(self): - - # class Interval: - # def covers(self, other: Interval) -> bool - - assert Interval(1, 3).covers(Interval(1.5, 2.5)) - assert Interval(1, 3).covers(Interval(1, 2)) - assert Interval(1, 3).covers(Interval(2, 3)) - assert not Interval(1, 3).covers(Interval(0.5, 2.5)) - assert not Interval(1, 3).covers(Interval(1.5, 3.5)) - - assert Interval(1, 3, closed='right').covers(Interval(1, 3, closed='right')) - assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='left')) - assert not Interval(1, 3, closed='right').covers(Interval(1, 3, closed='both')) - - assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='left').covers(Interval(1, 3, closed='left')) - assert not Interval(1, 3, closed='left').covers(Interval(1, 3, closed='both')) - - assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='both').covers(Interval(1, 3, closed='both')) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_covers_intervalIndex_updated_behavior(self): - - # class Interval: - # def covers(self, other: IntervalIndex) -> IntegerArray1D - - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - - tm.assert_numpy_array_equal(Interval(1, 3, closed='right').covers(idx), np.array([1, 2])) - tm.assert_numpy_array_equal(Interval(0, 3, closed='right').covers(idx), np.array([0, 1, 2])) - tm.assert_numpy_array_equal(Interval(0, 2, closed='right').covers(idx), np.array([0])) - tm.assert_numpy_array_equal(Interval(2, 4, closed='right').covers(idx), np.array([1])) - - tm.assert_numpy_array_equal(Interval(1, 3, closed='left').covers(idx), np.array([])) - tm.assert_numpy_array_equal(Interval(0, 3, closed='left').covers(idx), np.array([0])) - tm.assert_numpy_array_equal(Interval(0, 2, closed='left').covers(idx), np.array([0])) - tm.assert_numpy_array_equal(Interval(2, 4, closed='left').covers(idx), np.array([1])) - - tm.assert_numpy_array_equal(Interval(1, 3, closed='both').covers(idx), np.array([1, 2])) - tm.assert_numpy_array_equal(Interval(0, 5, closed='both').covers(idx), np.array([0, 1, 2])) - tm.assert_numpy_array_equal(Interval(0, 2, closed='both').covers(idx), np.array([0])) - tm.assert_numpy_array_equal(Interval(2, 4, closed='both').covers(idx), np.array([1])) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_overlaps_interval_updated_behavior(self): - - # class Interval: - # def overlaps(self, other: Interval) -> bool - - assert Interval(1, 3).overlaps(Interval(1.5, 2.5)) - assert Interval(1, 3).overlaps(Interval(1, 2)) - assert Interval(1, 3).overlaps(Interval(2, 3)) - assert Interval(1, 3).overlaps(Interval(0.5, 2.5)) - assert Interval(1, 3).overlaps(Interval(1.5, 3.5)) - - assert not Interval(1, 3).overlaps(Interval(-1, 1)) - assert not Interval(1, 3).overlaps(Interval(3, 5)) - - # right - assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='right').overlaps(Interval(1, 3, closed='both')) - - assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='right')) - assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='left')) - assert not Interval(1, 3, closed='right').overlaps(Interval(-1, 1, closed='both')) - - assert not Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='right')) - assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='left')) - assert Interval(1, 3, closed='right').overlaps(Interval(3, 5, closed='both')) - - # left - assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='left').overlaps(Interval(1, 3, closed='both')) - - assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='right')) - assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='left')) - assert not Interval(1, 3, closed='left').overlaps(Interval(-1, 1, closed='both')) - - assert not Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='right')) - assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='left')) - assert Interval(1, 3, closed='left').overlaps(Interval(3, 5, closed='both')) - - # both - assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='right')) - assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='left')) - assert Interval(1, 3, closed='both').overlaps(Interval(1, 3, closed='both')) - - assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='right')) - assert not Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='left')) - assert Interval(1, 3, closed='both').overlaps(Interval(-1, 1, closed='both')) - - assert not Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='right')) - assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='left')) - assert Interval(1, 3, closed='both').overlaps(Interval(3, 5, closed='both')) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_interval_overlaps_intervalIndex_updated_behavior(self): - - # class Interval: - # def overlaps(self, other: IntervalIndex) -> IntegerArray1D - - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - - tm.assert_numpy_array_equal(Interval(1, 3, closed='right').overlaps(idx), np.array([1, 2])) - tm.assert_numpy_array_equal(Interval(1, 2, closed='right').overlaps(idx), np.array([2])) - tm.assert_numpy_array_equal(Interval(0, 2, closed='right').overlaps(idx), np.array([0, 2])) - tm.assert_numpy_array_equal(Interval(3, 4, closed='right').overlaps(idx), np.array([])) - - tm.assert_numpy_array_equal(Interval(1, 3, closed='left').overlaps(idx), np.array([0, 1, 2])) - tm.assert_numpy_array_equal(Interval(1, 2, closed='left').overlaps(idx), np.array([0, 2])) - tm.assert_numpy_array_equal(Interval(0, 2, closed='left').overlaps(idx), np.array([0, 2])) - tm.assert_numpy_array_equal(Interval(3, 4, closed='left').overlaps(idx), np.array([3])) - - tm.assert_numpy_array_equal(Interval(1, 3, closed='both').overlaps(idx), np.array([0, 1, 2])) - tm.assert_numpy_array_equal(Interval(1, 2, closed='both').overlaps(idx), np.array([0, 2])) - tm.assert_numpy_array_equal(Interval(0, 2, closed='both').overlaps(idx), np.array([0, 2])) - tm.assert_numpy_array_equal(Interval(3, 4, closed='both').overlaps(idx), np.array([3])) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_covers_interval_updated_behavior(self): - - # class IntervalIndex: - # def covers(self, other: Interval) -> IntegerArray1D - - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - - tm.assert_numpy_array_equal(idx.covers(Interval(1, 3, closed='right')), np.array([1, 2])) - tm.assert_numpy_array_equal(idx.covers(Interval(0, 3, closed='right')), np.array([0, 1, 2])) - tm.assert_numpy_array_equal(idx.covers(Interval(0, 2, closed='right')), np.array([0])) - tm.assert_numpy_array_equal(idx.covers(Interval(2, 4, closed='right')), np.array([1])) - - tm.assert_numpy_array_equal(idx.covers(Interval(1, 3, closed='left')), np.array([])) - tm.assert_numpy_array_equal(idx.covers(Interval(0, 3, closed='left')), np.array([0])) - tm.assert_numpy_array_equal(idx.covers(Interval(0, 2, closed='left')), np.array([0])) - tm.assert_numpy_array_equal(idx.covers(Interval(2, 4, closed='left')), np.array([1])) - - tm.assert_numpy_array_equal(idx.covers(Interval(1, 3, closed='both')), np.array([1, 2])) - tm.assert_numpy_array_equal(idx.covers(Interval(0, 5, closed='both')), np.array([0, 1, 2])) - tm.assert_numpy_array_equal(idx.covers(Interval(0, 2, closed='both')), np.array([0])) - tm.assert_numpy_array_equal(idx.covers(Interval(2, 4, closed='both')), np.array([1])) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_covers_intervalIndex(self): - - # class IntervalIndex: - # def covers(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] - - idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') - idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - - self._compare_tuple_of_numpy_array(idx.covers(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) - self._compare_tuple_of_numpy_array(idx.covers(idx2), (np.array([2]), np.array([1]))) - self._compare_tuple_of_numpy_array(idx.covers(idx3), (np.array([0,1,2,2]), np.array([0,1,1,2]))) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_overlaps_interval_updated_behavior(self): - - # class IntervalIndex: - # def overlaps(self, other: Interval) -> IntegerArray1D - - idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - - tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 3, closed='right')), np.array([1, 2])) - tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 2, closed='right')), np.array([2])) - tm.assert_numpy_array_equal(idx.overlaps(Interval(0, 2, closed='right')), np.array([0, 2])) - tm.assert_numpy_array_equal(idx.overlaps(Interval(3, 4, closed='right')), np.array([])) - - tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 3, closed='left')), np.array([0, 1, 2])) - tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 2, closed='left')), np.array([0, 2])) - tm.assert_numpy_array_equal(idx.overlaps(Interval(0, 2, closed='left')), np.array([0, 2])) - tm.assert_numpy_array_equal(idx.overlaps(Interval(3, 4, closed='left')), np.array([3])) - - tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 3, closed='both')), np.array([0, 1, 2])) - tm.assert_numpy_array_equal(idx.overlaps(Interval(1, 2, closed='both')), np.array([0, 2])) - tm.assert_numpy_array_equal(idx.overlaps(Interval(0, 2, closed='both')), np.array([0, 2])) - tm.assert_numpy_array_equal(idx.overlaps(Interval(3, 4, closed='both')), np.array([3])) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_intervalIndex_overlaps_intervalIndex_updated_behavior(self): - - # class IntervalIndex: - # def overlaps(self, other: IntervalIndex) -> Tuple[IntegerArray1D, IntegerArray1D] - - idx1 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='right') - idx2 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='left') - idx3 = IntervalIndex.from_tuples([(0, 1), (2, 3), (1, 3)], closed='both') - - self._compare_tuple_of_numpy_array(idx.overlaps(idx1), (np.array([0,1,2,2]), np.array([0,1,1,2]))) - self._compare_tuple_of_numpy_array(idx.overlaps(idx2), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) - self._compare_tuple_of_numpy_array(idx.overlaps(idx3), (np.array([0,0,1,1,2,2]), np.array([0,2,1,2,1,2]))) - def test_dropna(self): expected = IntervalIndex.from_tuples([(0.0, 1.0), (1.0, 2.0)]) From fef3187294fc8e9ea21e0776b563f9d8e83b805b Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 20 Jun 2017 12:52:39 -0400 Subject: [PATCH 11/56] take back changes involving .contains() and __contains__ --- pandas/tests/indexes/test_interval.py | 40 ++++++++++++++++---------- pandas/tests/indexing/test_interval.py | 4 +++ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 042e92681cb88..e59be17ba0047 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -334,6 +334,7 @@ def test_get_item(self): tm.assert_index_equal(result, expected) def test_get_loc_value(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE pytest.raises(KeyError, self.index.get_loc, 0) assert self.index.get_loc(0.5) == 0 assert self.index.get_loc(1) == 0 @@ -530,6 +531,7 @@ def test_slice_locs_fails(self): index.slice_locs(1, 2) def test_get_indexer(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE actual = self.index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) expected = np.array([-1, -1, 0, 0, 1, 1, -1], dtype='intp') tm.assert_numpy_array_equal(actual, expected) @@ -573,6 +575,7 @@ def test_get_indexer_updated_behavior(self): # IntervalIndex.get_indexer(IntervalIndex) def test_get_indexer_subintervals(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE # TODO: is this right? # return indexers for wholly contained subintervals @@ -603,6 +606,7 @@ def test_get_indexer_subintervals_updated_behavior(self): def test_contains(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE # Only endpoints are valid. i = IntervalIndex.from_arrays([0, 1], [1, 2]) @@ -618,7 +622,26 @@ def test_contains(self): assert Interval(3, 5) not in i assert Interval(-1, 0, closed='left') not in i + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_contains_updated_behavior(self): + + index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right') + + # __contains__ requires perfect matches to intervals. + assert 0 not in i + assert 1 not in i + assert 2 not in i + + assert Interval(0, 1, closed='right') in index + assert Interval(0, 2, closed='right') not in index + assert Interval(0, 0.5, closed='right') not in index + assert Interval(3, 5, closed='right') not in index + assert Interval(-1, 0, closed='left') not in index + assert Interval(0, 1, closed='left') not in index + assert Interval(0, 1, closed='both') not in index + def testcontains(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE # can select values that are IN the range of a value i = IntervalIndex.from_arrays([0, 1], [1, 2]) @@ -636,24 +659,10 @@ def testcontains(self): assert not i.contains(-20) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_contains_updated_behavior(self): - - # this method will replace both of the above + def testcontains_updated_behavior(self): index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right') - assert 0 not in index - assert 1 in index # not sure how we want to do this. - assert 2 in index # does this defer to "index.contains()" ? - - assert Interval(0, 1, closed='right') in index - assert Interval(0, 2, closed='right') not in index - assert Interval(0, 0.5, closed='right') not in index - assert Interval(3, 5, closed='right') not in index - assert Interval(-1, 0, closed='left') not in index - assert Interval(0, 1, closed='left') not in index - assert Interval(0, 1, closed='both') not in index - assert not index.contains(0) assert index.contains(0.1) assert index.contains(0.5) @@ -661,6 +670,7 @@ def test_contains_updated_behavior(self): assert index.contains(Interval(0, 1), closed='right') assert not index.contains(Interval(0, 1), closed='left') + assert not index.contains(Interval(0, 1), closed='both') assert not index.contains(Interval(0, 2), closed='right') assert not index.contains(Interval(0, 3), closed='right') diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index 00f48c11958e7..77f5323680780 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -68,6 +68,7 @@ def test_getitem_with_scalar(self): tm.assert_series_equal(expected, s[s >= 2]) def test_with_interval(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE s = self.s expected = 0 @@ -130,6 +131,7 @@ def test_with_interval_updated_behavior(self): s[Interval(5, 6)] def test_with_slices(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE s = self.s @@ -189,6 +191,7 @@ def test_with_slices_updated_behavior(self): s[0:4:2] ## This should probably definitely fail I guess? def test_with_overlaps(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE s = self.s expected = s.iloc[[3, 4, 3, 4]] @@ -270,6 +273,7 @@ def test_non_unique(self): tm.assert_series_equal(expected, result) def test_non_unique_moar(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) s = Series(range(len(idx)), index=idx) From e3a12fa87303832244e199710a324be70072e69f Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Wed, 21 Jun 2017 21:24:33 -0400 Subject: [PATCH 12/56] some updates discussed --- pandas/tests/indexes/test_interval.py | 358 ++++++++++++++++++++++--- pandas/tests/indexing/test_interval.py | 4 +- 2 files changed, 322 insertions(+), 40 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index e59be17ba0047..cb8c84e74f5b4 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -334,7 +334,7 @@ def test_get_item(self): tm.assert_index_equal(result, expected) def test_get_loc_value(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 pytest.raises(KeyError, self.index.get_loc, 0) assert self.index.get_loc(0.5) == 0 assert self.index.get_loc(1) == 0 @@ -395,6 +395,14 @@ def test_get_loc_value_closed_right_updated_behavior(self): pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='both')) pytest.raises(KeyError, right.get_loc, Interval(-1, 4, closed='both')) + pytest.raises(KeyError, right.get_loc, Interval(0, 1, closed='neither')) + pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='neither')) + pytest.raises(KeyError, right.get_loc, Interval(2, 3, closed='neither')) + pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='neither')) + pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='neither')) + pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='neither')) + pytest.raises(KeyError, right.get_loc, Interval(-1, 4, closed='neither')) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_loc_value_closed_left_updated_behavior(self): @@ -434,6 +442,14 @@ def test_get_loc_value_closed_left_updated_behavior(self): pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='both')) pytest.raises(KeyError, left.get_loc, Interval(-1, 4, closed='both')) + pytest.raises(KeyError, left.get_loc, Interval(0, 1, closed='neither')) + pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='neither')) + pytest.raises(KeyError, left.get_loc, Interval(2, 3, closed='neither')) + pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='neither')) + pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='neither')) + pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='neither')) + pytest.raises(KeyError, left.get_loc, Interval(-1, 4, closed='neither')) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_loc_value_closed_both_updated_behavior(self): @@ -473,9 +489,67 @@ def test_get_loc_value_closed_both_updated_behavior(self): pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='both')) pytest.raises(KeyError, both.get_loc, Interval(-1, 4, closed='both')) - def slice_locs_cases(self, breaks): + pytest.raises(KeyError, both.get_loc, Interval(0, 1, closed='neither')) + pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='neither')) + pytest.raises(KeyError, both.get_loc, Interval(2, 3, closed='neither')) + pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='neither')) + pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='neither')) + pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='neither')) + pytest.raises(KeyError, both.get_loc, Interval(-1, 4, closed='neither')) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_get_loc_value_closed_neither_updated_behavior(self): + + neither = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='neither') + + pytest.raises(KeyError, neither.get_loc, -0.5) + pytest.raises(KeyError, neither.get_loc, 0) + assert neither.get_loc(0.5) == 0 + pytest.raises(KeyError, neither.get_loc, 1) + pytest.raises(KeyError, neither.get_loc, 1.5) + pytest.raises(KeyError, neither.get_loc, 2) + assert neither.get_loc(2.5) == 1 + pytest.raises(KeyError, neither.get_loc, 3) + pytest.raises(KeyError, neither.get_loc, 3.5) + + pytest.raises(KeyError, neither.get_loc, Interval(0, 1, closed='right')) + pytest.raises(KeyError, neither.get_loc, Interval(1, 2, closed='right')) + pytest.raises(KeyError, neither.get_loc, Interval(2, 3, closed='right')) + pytest.raises(KeyError, neither.get_loc, Interval(3, 4, closed='right')) + pytest.raises(KeyError, neither.get_loc, Interval(0, 2, closed='right')) + pytest.raises(KeyError, neither.get_loc, Interval(2.5, 3, closed='right')) + pytest.raises(KeyError, neither.get_loc, Interval(-1, 4, closed='right')) + + pytest.raises(KeyError, neither.get_loc, Interval(0, 1, closed='left')) + pytest.raises(KeyError, neither.get_loc, Interval(1, 2, closed='left')) + pytest.raises(KeyError, neither.get_loc, Interval(2, 3, closed='left')) + pytest.raises(KeyError, neither.get_loc, Interval(3, 4, closed='left')) + pytest.raises(KeyError, neither.get_loc, Interval(0, 2, closed='left')) + pytest.raises(KeyError, neither.get_loc, Interval(2.5, 3, closed='left')) + pytest.raises(KeyError, neither.get_loc, Interval(-1, 4, closed='left')) + + pytest.raises(KeyError, neither.get_loc, Interval(0, 1 closed='both')) + pytest.raises(KeyError, neither.get_loc, Interval(1, 2, closed='both')) + pytest.raises(KeyError, neither.get_loc, Interval(2, 3, closed='both')) + pytest.raises(KeyError, neither.get_loc, Interval(3, 4, closed='both')) + pytest.raises(KeyError, neither.get_loc, Interval(0, 2, closed='both')) + pytest.raises(KeyError, neither.get_loc, Interval(2.5, 3, closed='both')) + pytest.raises(KeyError, neither.get_loc, Interval(-1, 4, closed='both')) + + assert neither.get_loc(Interval(0, 1, closed='neither')) == 0 + pytest.raises(KeyError, neither.get_loc, Interval(1, 2, closed='neither')) + assert neither.get_loc(Interval(2, 3, closed='neither')) == 1 + pytest.raises(KeyError, neither.get_loc, Interval(3, 4, closed='neither')) + pytest.raises(KeyError, neither.get_loc, Interval(0, 2, closed='neither')) + pytest.raises(KeyError, neither.get_loc, Interval(2.5, 3, closed='neither')) + pytest.raises(KeyError, neither.get_loc, Interval(-1, 4, closed='neither')) + + ### OLD SLICE LOCS BEHAVIOR + + def slice_locs_cases(self, tuples): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 # TODO: same tests for more index types - index = IntervalIndex.from_breaks(breaks, closed='right') + index = IntervalIndex.from_tuples(tuples, closed='right') assert index.slice_locs() == (0, 2) assert index.slice_locs(0, 1) == (0, 1) assert index.slice_locs(1, 1) == (0, 1) @@ -489,24 +563,27 @@ def slice_locs_cases(self, breaks): assert index.slice_locs(end=1.0) == (0, 1) assert index.slice_locs(-1, -1) == (0, 0) - index = IntervalIndex.from_breaks(breaks, closed='neither') + index = IntervalIndex.from_tuples(tuples, closed='neither') assert index.slice_locs(0, 1) == (0, 1) assert index.slice_locs(0, 2) == (0, 2) assert index.slice_locs(0.5, 1.5) == (0, 2) assert index.slice_locs(1, 1) == (1, 1) assert index.slice_locs(1, 2) == (1, 2) - index = IntervalIndex.from_breaks(breaks, closed='both') + index = IntervalIndex.from_tuples(tuples, closed='both') assert index.slice_locs(1, 1) == (0, 2) assert index.slice_locs(1, 2) == (0, 2) - def test_slice_locs_int64(self): - self.slice_locs_cases([0, 1, 2]) + def test_slice_locs_increasing_int64(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 + self.slice_locs_cases([(0, 2), (1, 3), (2, 4)]) - def test_slice_locs_float64(self): - self.slice_locs_cases([0.0, 1.0, 2.0]) + def test_slice_locs_increasing_float64(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 + self.slice_locs_cases([(0., 2.), (1., 3.), (2., 4.)]) def slice_locs_decreasing_cases(self, tuples): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 index = IntervalIndex.from_tuples(tuples) assert index.slice_locs(1.5, 0.5) == (1, 3) assert index.slice_locs(2, 0) == (1, 3) @@ -520,18 +597,129 @@ def slice_locs_decreasing_cases(self, tuples): assert slice_locs[0] == slice_locs[1] def test_slice_locs_decreasing_int64(self): - self.slice_locs_cases([(2, 4), (1, 3), (0, 2)]) + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 + self.slice_locs_decreasing_cases([(2, 4), (1, 3), (0, 2)]) def test_slice_locs_decreasing_float64(self): - self.slice_locs_cases([(2., 4.), (1., 3.), (0., 2.)]) + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 + self.slice_locs_decreasing_cases([(2., 4.), (1., 3.), (0., 2.)]) def test_slice_locs_fails(self): + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 index = IntervalIndex.from_tuples([(1, 2), (0, 1), (2, 3)]) with pytest.raises(KeyError): index.slice_locs(1, 2) + ### NEW SLICE LOCS BEHAVIOR + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def slice_locs_with_interval_updated_behavior(self): + # @jreback might prefer these be broken up into separate methods + + # increasing + index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) + + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 1) + assert index.slice_locs(start=Interval(0, 2)) == (0, 1, 2) + assert index.slice_locs(end=Interval(2, 4)) == (0, 1) + assert index.slice_locs(end=Interval(0, 2)) == () + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == () + + # decreasing + index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) + + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == () + assert index.slice_locs(start=Interval(0, 2)) == (2) + assert index.slice_locs(end=Interval(2, 4)) == () + assert index.slice_locs(end=Interval(0, 2)) == (0, 1) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (0, 1) + + # sorted duplicates + index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) + + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == # ? + assert index.slice_locs(start=Interval(0, 2)) == # ? + assert index.slice_locs(end=Interval(2, 4)) == # ? + assert index.slice_locs(end=Interval(0, 2)) == # ? + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == # ? + + # unsorted duplicates + index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) + + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == # ? + assert index.slice_locs(start=Interval(0, 2)) == # ? + assert index.slice_locs(end=Interval(2, 4)) == # ? + assert index.slice_locs(end=Interval(0, 2)) == # ? + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == # ? + + # different unsorted duplicates + index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) + + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == # ? + assert index.slice_locs(start=Interval(0, 2)) == # ? + assert index.slice_locs(end=Interval(2, 4)) == # ? + assert index.slice_locs(end=Interval(0, 2)) == # ? + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == # ? + + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def slice_locs_with_ints_and_floats_updated_behavior(self): + + # increasing + index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) + + assert index.slice_locs(0, 1) == #? + assert index.slice_locs(0, 2) == #? + assert index.slice_locs(0, 3) == #? + assert index.slice_locs(3, 1) == #? + assert index.slice_locs(3, 4) == #? + assert index.slice_locs(0, 4) == #? + + # decreasing + index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) + + assert index.slice_locs(0, 1) == #? + assert index.slice_locs(0, 2) == #? + assert index.slice_locs(0, 3) == #? + assert index.slice_locs(3, 1) == #? + assert index.slice_locs(3, 4) == #? + assert index.slice_locs(0, 4) == #? + + # sorted duplicates + index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) + + assert index.slice_locs(0, 1) == #? + assert index.slice_locs(0, 2) == #? + assert index.slice_locs(0, 3) == #? + assert index.slice_locs(3, 1) == #? + assert index.slice_locs(3, 4) == #? + assert index.slice_locs(0, 4) == #? + + # unsorted duplicates + index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) + + assert index.slice_locs(0, 1) == #? + assert index.slice_locs(0, 2) == #? + assert index.slice_locs(0, 3) == #? + assert index.slice_locs(3, 1) == #? + assert index.slice_locs(3, 4) == #? + assert index.slice_locs(0, 4) == #? + + # different unsorted duplicates + index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) + + assert index.slice_locs(0, 1) == #? + assert index.slice_locs(0, 2) == #? + assert index.slice_locs(0, 3) == #? + assert index.slice_locs(3, 1) == #? + assert index.slice_locs(3, 4) == #? + assert index.slice_locs(0, 4) == #? + + + ### OLD GET INDEXER BEHAVIOR + def test_get_indexer(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 actual = self.index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) expected = np.array([-1, -1, 0, 0, 1, 1, -1], dtype='intp') tm.assert_numpy_array_equal(actual, expected) @@ -554,28 +742,8 @@ def test_get_indexer(self): expected = np.array([-1, 1], dtype='intp') tm.assert_numpy_array_equal(actual, expected) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_indexer_updated_behavior(self): - actual = self.index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) - expected = np.array([-1, -1, 0, 0, 1, 1, -1], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - actual = self.index.get_indexer(self.index) - expected = np.array([0, 1], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - index = IntervalIndex.from_breaks([0, 1, 2], closed='left') - - actual = index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) - expected = np.array([-1, 0, 0, 1, 1, -1, -1], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - # TODO: To be fleshed out: - # IntervalIndex.get_indexer(Interval) - # IntervalIndex.get_indexer(IntervalIndex) - def test_get_indexer_subintervals(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 # TODO: is this right? # return indexers for wholly contained subintervals @@ -598,15 +766,129 @@ def test_get_indexer_subintervals(self): expected = np.array([0, 0, 0], dtype='intp') tm.assert_numpy_array_equal(actual, expected) + ### NEW GET INDEXER BEHAVIOR + @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_indexer_subintervals_updated_behavior(self): + def get_indexer_for_interval_updated_behavior(self): - # this whole method needs reworking. - pass + index = IntervalIndex.from_tuples([(0, 2.5), (1, 3), (2, 4)], closed='right') + + # single queries + result = index.get_indexer([Interval(1, 3, closed='right')]) + expected = np.array([1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([Interval(1, 3, closed='left')]) + expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([Interval(1, 3, closed='both')]) + expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([Interval(1, 3, closed='neither')]) + expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([Interval(1, 4, closed='right')]) + expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([Interval(0, 4, closed='right')]) + expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([Interval(1, 2, closed='right')]) + expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + # multiple queries + result = index.get_indexer([Interval(2, 4, closed='right'), + Interval(1, 3, closed='right') ]) + expected = np.array([2, 1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([Interval(1, 3, closed='right'), + Interval(0, 2, closed='right') ]) + expected = np.array([1, -1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([Interval(1, 3, closed='right'), + Interval(1, 3, closed='left') ]) + expected = np.array([1, -1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer(index) + expected = np.array([0, 1, 2], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def get_indexer_for_ints_and_floats_updated_behavior(self): + + index = IntervalIndex.from_tuples([(0, 2.5), (1, 3), (2, 4)], closed='right') + + result = index.get_indexer([-0.5]) + # expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([0]) + # expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([0.5]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([1]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([1.5]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([2]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([2.5]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([3]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([3.5]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([4]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([4.5]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([1, 2]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([1, 2, 3]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer([1, 2, 3, 4]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) + result = index.get_indexer([1, 2, 3, 4, 2]) + # expected = np.array([], dtype='intp') ?? + tm.assert_numpy_array_equal(result, expected) def test_contains(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 # Only endpoints are valid. i = IntervalIndex.from_arrays([0, 1], [1, 2]) @@ -641,7 +923,7 @@ def test_contains_updated_behavior(self): assert Interval(0, 1, closed='both') not in index def testcontains(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE + ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 # can select values that are IN the range of a value i = IntervalIndex.from_arrays([0, 1], [1, 2]) diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index 77f5323680780..c6248c959aaa1 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -67,7 +67,7 @@ def test_getitem_with_scalar(self): expected = s.iloc[2:5] tm.assert_series_equal(expected, s[s >= 2]) - def test_with_interval(self): + def test_loc_and_getitem_with_interval(self): ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE s = self.s @@ -106,7 +106,7 @@ def test_with_interval(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_with_interval_updated_behavior(self): + def test_loc_and_getitem_with_interval_updated_behavior(self): s = self.s expected = 0 From 06a28356dd8b185d6f95f5f72aee279596c9070c Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Wed, 21 Jun 2017 21:28:51 -0400 Subject: [PATCH 13/56] quick thought... --- pandas/tests/indexes/test_interval.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index cb8c84e74f5b4..0f5a3955da517 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -887,6 +887,9 @@ def get_indexer_for_ints_and_floats_updated_behavior(self): # expected = np.array([], dtype='intp') ?? tm.assert_numpy_array_equal(result, expected) + # we may also want to test get_indexer if the intervals are non-overlapping, + # decreasing, non-monotonic, etc.. + def test_contains(self): ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 # Only endpoints are valid. From 08bd9e4657979d0033498f54b21610294811a50e Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 29 Jun 2017 10:39:04 -0400 Subject: [PATCH 14/56] tiny update --- pandas/tests/indexes/test_interval.py | 45 ++++++++++++++------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 0f5a3955da517..c552c9c1b07a6 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -619,9 +619,9 @@ def slice_locs_with_interval_updated_behavior(self): # increasing index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 1) - assert index.slice_locs(start=Interval(0, 2)) == (0, 1, 2) - assert index.slice_locs(end=Interval(2, 4)) == (0, 1) + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 2) + assert index.slice_locs(start=Interval(0, 2)) == (0, 3) + assert index.slice_locs(end=Interval(2, 4)) == (0, 2) assert index.slice_locs(end=Interval(0, 2)) == () assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == () @@ -629,37 +629,37 @@ def slice_locs_with_interval_updated_behavior(self): index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == () - assert index.slice_locs(start=Interval(0, 2)) == (2) + assert index.slice_locs(start=Interval(0, 2)) == (2, 3) assert index.slice_locs(end=Interval(2, 4)) == () - assert index.slice_locs(end=Interval(0, 2)) == (0, 1) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (0, 1) + assert index.slice_locs(end=Interval(0, 2)) == (0, 2) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (0, 2) # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == # ? - assert index.slice_locs(start=Interval(0, 2)) == # ? - assert index.slice_locs(end=Interval(2, 4)) == # ? - assert index.slice_locs(end=Interval(0, 2)) == # ? - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == # ? + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 2) + assert index.slice_locs(start=Interval(0, 2)) == (0, 3) + assert index.slice_locs(end=Interval(2, 4)) == (0, 2) + assert index.slice_locs(end=Interval(0, 2)) == () # questionable + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == () # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == # ? - assert index.slice_locs(start=Interval(0, 2)) == # ? - assert index.slice_locs(end=Interval(2, 4)) == # ? - assert index.slice_locs(end=Interval(0, 2)) == # ? - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == # ? + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == # This should raise? + assert index.slice_locs(start=Interval(0, 2)) == # This should raise? + assert index.slice_locs(end=Interval(2, 4)) == (0, 2) + assert index.slice_locs(end=Interval(0, 2)) == # This should raise? + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == # This should raise? # different unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == # ? - assert index.slice_locs(start=Interval(0, 2)) == # ? - assert index.slice_locs(end=Interval(2, 4)) == # ? - assert index.slice_locs(end=Interval(0, 2)) == # ? - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == # ? + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(start=Interval(0, 2)) == (0, 4) + assert index.slice_locs(end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(end=Interval(0, 2)) == () + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == () @pytest.mark.xfail(reason="new indexing tests for issue 16316") @@ -887,7 +887,8 @@ def get_indexer_for_ints_and_floats_updated_behavior(self): # expected = np.array([], dtype='intp') ?? tm.assert_numpy_array_equal(result, expected) - # we may also want to test get_indexer if the intervals are non-overlapping, + # we may also want to test get_indexer for the case when + # the intervals are non-overlapping, duplicated, # decreasing, non-monotonic, etc.. def test_contains(self): From 63817448850466fa36f2d75cf05024ebd2aed16e Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 6 Jul 2017 10:52:39 -0400 Subject: [PATCH 15/56] An update to get_indexer behavior --- pandas/tests/indexes/test_interval.py | 104 +++++++++++++++++++++----- 1 file changed, 86 insertions(+), 18 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index c552c9c1b07a6..32848d98c65ff 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -825,71 +825,139 @@ def get_indexer_for_interval_updated_behavior(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def get_indexer_for_ints_and_floats_updated_behavior(self): - index = IntervalIndex.from_tuples([(0, 2.5), (1, 3), (2, 4)], closed='right') + index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)], closed='right') + # single queries result = index.get_indexer([-0.5]) - # expected = np.array([-1], dtype='intp') + expected = np.array([-1], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([0]) - # expected = np.array([-1], dtype='intp') + expected = np.array([-1], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([0.5]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([0], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([1]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([0], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([1.5]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([1], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([2]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([1], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([2.5]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([-1], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([3]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([-1], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([3.5]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([2], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([4]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([2], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([4.5]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([-1], dtype='intp') tm.assert_numpy_array_equal(result, expected) + # multiple queries result = index.get_indexer([1, 2]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([0, 1], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([1, 2, 3]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([0, 1, -1], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([1, 2, 3, 4]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([0, 1, -1, 2], dtype='intp') tm.assert_numpy_array_equal(result, expected) result = index.get_indexer([1, 2, 3, 4, 2]) - # expected = np.array([], dtype='intp') ?? + expected = np.array([0, 1, -1, 2, 1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def get_indexer_nonunique_for_ints_and_floats_updated_behavior(self): + + index = IntervalIndex.from_tuples([(0, 2.5), (1, 3), (2, 4)], closed='left') + + # single queries + result = index.get_indexer_nonunique([-0.5]) + expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([0]) + expected = np.array([0], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([0.5]) + expected = np.array([0], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([1]) + expected = np.array([0, 1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([1.5]) + expected = np.array([0, 1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([2]) + expected = np.array([0, 1, 2], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([2.5]) + expected = np.array([1, 2], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([3]) + expected = np.array([2], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([3.5]) + expected = np.array([2], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([4]) + expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([4.5]) + expected = np.array([-1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + # multiple queries + result = index.get_indexer_nonunique([1, 2]) + expected = np.array([0, 1, 0, 1, 2], dtype='intp') # maybe we could put these in tuples for people? [(0, 1), (0, 1, 2)] + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([1, 2, 3]) + expected = np.array([0, 1, 0, 1, 2, 2], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([1, 2, 3, 4]) + expected = np.array([0, 1, 0, 1, 2, 2, -1], dtype='intp') + tm.assert_numpy_array_equal(result, expected) + + result = index.get_indexer_nonunique([1, 2, 3, 4, 2]) + expected = np.array([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='intp') tm.assert_numpy_array_equal(result, expected) # we may also want to test get_indexer for the case when - # the intervals are non-overlapping, duplicated, - # decreasing, non-monotonic, etc.. + # the intervals are duplicated, decreasing, non-monotonic, etc.. def test_contains(self): ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 From b346af786d6a69bdfc92d6477079e14f993912e0 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 6 Jul 2017 12:00:39 -0400 Subject: [PATCH 16/56] update slice locs behavior --- pandas/tests/indexes/test_interval.py | 90 ++++++++++++++++----------- 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 32848d98c65ff..bba8fae0f928b 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -665,55 +665,75 @@ def slice_locs_with_interval_updated_behavior(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_ints_and_floats_updated_behavior(self): - # increasing - index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) + # increasing non-overlapping + index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)]) - assert index.slice_locs(0, 1) == #? - assert index.slice_locs(0, 2) == #? - assert index.slice_locs(0, 3) == #? - assert index.slice_locs(3, 1) == #? - assert index.slice_locs(3, 4) == #? - assert index.slice_locs(0, 4) == #? + assert index.slice_locs(0, 1) == (0) + assert index.slice_locs(0, 2) == (0, 1) + assert index.slice_locs(0, 3) == (0, 1) + assert index.slice_locs(3, 1) == () + assert index.slice_locs(3, 4) == (2) + assert index.slice_locs(0, 4) == (0, 1, 2) - # decreasing - index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) + # increasing overlapping + index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - assert index.slice_locs(0, 1) == #? - assert index.slice_locs(0, 2) == #? - assert index.slice_locs(0, 3) == #? - assert index.slice_locs(3, 1) == #? - assert index.slice_locs(3, 4) == #? - assert index.slice_locs(0, 4) == #? + assert index.slice_locs(0, 1) == # this raises an error because index is overlapping? + assert index.slice_locs(0, 2) == # this raises an error because index is overlapping? + assert index.slice_locs(0, 3) == # this raises an error because index is overlapping? + assert index.slice_locs(3, 1) == # this raises an error because index is overlapping? + assert index.slice_locs(3, 4) == # this raises an error because index is overlapping? + assert index.slice_locs(0, 4) == # this raises an error because index is overlapping? + + # decreasing non-overlapping + index = IntervalIndex.from_tuples([(4, 3), (2, 1), (1, 0)]) + # These were a little mind-bending to write, would appreciate a close review + assert index.slice_locs(0, 1) == (2) + assert index.slice_locs(0, 2) == (1, 2) + assert index.slice_locs(0, 3) == (1, 2) + assert index.slice_locs(3, 1) == (0, 1) + assert index.slice_locs(3, 4) == (0) + assert index.slice_locs(0, 4) == (0, 1, 2) + + # decreasing overlapping + index = IntervalIndex.from_tuples([(4, 2), (3, 1), (2, 0)]) + + assert index.slice_locs(0, 1) == # this raises an error because index is overlapping? + assert index.slice_locs(0, 2) == # this raises an error because index is overlapping? + assert index.slice_locs(0, 3) == # this raises an error because index is overlapping? + assert index.slice_locs(3, 1) == # this raises an error because index is overlapping? + assert index.slice_locs(3, 4) == # this raises an error because index is overlapping? + assert index.slice_locs(0, 4) == # this raises an error because index is overlapping? # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - assert index.slice_locs(0, 1) == #? - assert index.slice_locs(0, 2) == #? - assert index.slice_locs(0, 3) == #? - assert index.slice_locs(3, 1) == #? - assert index.slice_locs(3, 4) == #? - assert index.slice_locs(0, 4) == #? + assert index.slice_locs(0, 1) == # this raises an error because index is overlapping? + assert index.slice_locs(0, 2) == # this raises an error because index is overlapping? + assert index.slice_locs(0, 3) == # this raises an error because index is overlapping? + assert index.slice_locs(3, 1) == # this raises an error because index is overlapping? + assert index.slice_locs(3, 4) == # this raises an error because index is overlapping? + assert index.slice_locs(0, 4) == # this raises an error because index is overlapping? # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - assert index.slice_locs(0, 1) == #? - assert index.slice_locs(0, 2) == #? - assert index.slice_locs(0, 3) == #? - assert index.slice_locs(3, 1) == #? - assert index.slice_locs(3, 4) == #? - assert index.slice_locs(0, 4) == #? + assert index.slice_locs(0, 1) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(0, 2) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(0, 3) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(3, 1) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(3, 4) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(0, 4) == # this raises an error because index is overlapping/unsorted? # different unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) - assert index.slice_locs(0, 1) == #? - assert index.slice_locs(0, 2) == #? - assert index.slice_locs(0, 3) == #? - assert index.slice_locs(3, 1) == #? - assert index.slice_locs(3, 4) == #? - assert index.slice_locs(0, 4) == #? + assert index.slice_locs(0, 1) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(0, 2) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(0, 3) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(3, 1) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(3, 4) == # this raises an error because index is overlapping/unsorted? + assert index.slice_locs(0, 4) == # this raises an error because index is overlapping/unsorted? ### OLD GET INDEXER BEHAVIOR @@ -941,7 +961,7 @@ def get_indexer_nonunique_for_ints_and_floats_updated_behavior(self): # multiple queries result = index.get_indexer_nonunique([1, 2]) - expected = np.array([0, 1, 0, 1, 2], dtype='intp') # maybe we could put these in tuples for people? [(0, 1), (0, 1, 2)] + expected = np.array([0, 1, 0, 1, 2], dtype='intp') # maybe we could put these in tuples? [(0, 1), (0, 1, 2)] tm.assert_numpy_array_equal(result, expected) result = index.get_indexer_nonunique([1, 2, 3]) From ccd23aacfc9a8d71a74b24671551f93ea171eb18 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 14 Jul 2017 12:14:10 -0400 Subject: [PATCH 17/56] remove tests flagged for removal. --- pandas/tests/indexes/test_interval.py | 179 -------------------------- 1 file changed, 179 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index bba8fae0f928b..ca25d62d3e1a8 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -333,29 +333,6 @@ def test_get_item(self): closed='right') tm.assert_index_equal(result, expected) - def test_get_loc_value(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - pytest.raises(KeyError, self.index.get_loc, 0) - assert self.index.get_loc(0.5) == 0 - assert self.index.get_loc(1) == 0 - assert self.index.get_loc(1.5) == 1 - assert self.index.get_loc(2) == 1 - pytest.raises(KeyError, self.index.get_loc, -1) - pytest.raises(KeyError, self.index.get_loc, 3) - - idx = IntervalIndex.from_tuples([(0, 2), (1, 3)]) - assert idx.get_loc(0.5) == 0 - assert idx.get_loc(1) == 0 - tm.assert_numpy_array_equal(idx.get_loc(1.5), - np.array([0, 1], dtype='int64')) - tm.assert_numpy_array_equal(np.sort(idx.get_loc(2)), - np.array([0, 1], dtype='int64')) - assert idx.get_loc(3) == 1 - pytest.raises(KeyError, idx.get_loc, 3.5) - - idx = IntervalIndex.from_arrays([0, 2], [1, 3]) - pytest.raises(KeyError, idx.get_loc, 1.5) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_loc_value_closed_right_updated_behavior(self): @@ -544,74 +521,6 @@ def test_get_loc_value_closed_neither_updated_behavior(self): pytest.raises(KeyError, neither.get_loc, Interval(2.5, 3, closed='neither')) pytest.raises(KeyError, neither.get_loc, Interval(-1, 4, closed='neither')) - ### OLD SLICE LOCS BEHAVIOR - - def slice_locs_cases(self, tuples): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - # TODO: same tests for more index types - index = IntervalIndex.from_tuples(tuples, closed='right') - assert index.slice_locs() == (0, 2) - assert index.slice_locs(0, 1) == (0, 1) - assert index.slice_locs(1, 1) == (0, 1) - assert index.slice_locs(0, 2) == (0, 2) - assert index.slice_locs(0.5, 1.5) == (0, 2) - assert index.slice_locs(0, 0.5) == (0, 1) - assert index.slice_locs(start=1) == (0, 2) - assert index.slice_locs(start=1.2) == (1, 2) - assert index.slice_locs(end=1) == (0, 1) - assert index.slice_locs(end=1.1) == (0, 2) - assert index.slice_locs(end=1.0) == (0, 1) - assert index.slice_locs(-1, -1) == (0, 0) - - index = IntervalIndex.from_tuples(tuples, closed='neither') - assert index.slice_locs(0, 1) == (0, 1) - assert index.slice_locs(0, 2) == (0, 2) - assert index.slice_locs(0.5, 1.5) == (0, 2) - assert index.slice_locs(1, 1) == (1, 1) - assert index.slice_locs(1, 2) == (1, 2) - - index = IntervalIndex.from_tuples(tuples, closed='both') - assert index.slice_locs(1, 1) == (0, 2) - assert index.slice_locs(1, 2) == (0, 2) - - def test_slice_locs_increasing_int64(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - self.slice_locs_cases([(0, 2), (1, 3), (2, 4)]) - - def test_slice_locs_increasing_float64(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - self.slice_locs_cases([(0., 2.), (1., 3.), (2., 4.)]) - - def slice_locs_decreasing_cases(self, tuples): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - index = IntervalIndex.from_tuples(tuples) - assert index.slice_locs(1.5, 0.5) == (1, 3) - assert index.slice_locs(2, 0) == (1, 3) - assert index.slice_locs(2, 1) == (1, 3) - assert index.slice_locs(3, 1.1) == (0, 3) - assert index.slice_locs(3, 3) == (0, 2) - assert index.slice_locs(3.5, 3.3) == (0, 1) - assert index.slice_locs(1, -3) == (2, 3) - - slice_locs = index.slice_locs(-1, -1) - assert slice_locs[0] == slice_locs[1] - - def test_slice_locs_decreasing_int64(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - self.slice_locs_decreasing_cases([(2, 4), (1, 3), (0, 2)]) - - def test_slice_locs_decreasing_float64(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - self.slice_locs_decreasing_cases([(2., 4.), (1., 3.), (0., 2.)]) - - def test_slice_locs_fails(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - index = IntervalIndex.from_tuples([(1, 2), (0, 1), (2, 3)]) - with pytest.raises(KeyError): - index.slice_locs(1, 2) - - ### NEW SLICE LOCS BEHAVIOR - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_interval_updated_behavior(self): # @jreback might prefer these be broken up into separate methods @@ -735,59 +644,6 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): assert index.slice_locs(3, 4) == # this raises an error because index is overlapping/unsorted? assert index.slice_locs(0, 4) == # this raises an error because index is overlapping/unsorted? - - ### OLD GET INDEXER BEHAVIOR - - def test_get_indexer(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - actual = self.index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) - expected = np.array([-1, -1, 0, 0, 1, 1, -1], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - actual = self.index.get_indexer(self.index) - expected = np.array([0, 1], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - index = IntervalIndex.from_breaks([0, 1, 2], closed='left') - - actual = index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) - expected = np.array([-1, 0, 0, 1, 1, -1, -1], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - actual = self.index.get_indexer(index[:1]) - expected = np.array([0], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - actual = self.index.get_indexer(index) - expected = np.array([-1, 1], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - def test_get_indexer_subintervals(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - - # TODO: is this right? - # return indexers for wholly contained subintervals - target = IntervalIndex.from_breaks(np.linspace(0, 2, 5)) - actual = self.index.get_indexer(target) - expected = np.array([0, 0, 1, 1], dtype='p') - tm.assert_numpy_array_equal(actual, expected) - - target = IntervalIndex.from_breaks([0, 0.67, 1.33, 2]) - actual = self.index.get_indexer(target) - expected = np.array([0, 0, 1, 1], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - actual = self.index.get_indexer(target[[0, -1]]) - expected = np.array([0, 1], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - target = IntervalIndex.from_breaks([0, 0.33, 0.67, 1], closed='left') - actual = self.index.get_indexer(target) - expected = np.array([0, 0, 0], dtype='intp') - tm.assert_numpy_array_equal(actual, expected) - - ### NEW GET INDEXER BEHAVIOR - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def get_indexer_for_interval_updated_behavior(self): @@ -979,23 +835,6 @@ def get_indexer_nonunique_for_ints_and_floats_updated_behavior(self): # we may also want to test get_indexer for the case when # the intervals are duplicated, decreasing, non-monotonic, etc.. - def test_contains(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - # Only endpoints are valid. - i = IntervalIndex.from_arrays([0, 1], [1, 2]) - - # Invalid - assert 0 not in i - assert 1 not in i - assert 2 not in i - - # Valid - assert Interval(0, 1) in i - assert Interval(0, 2) in i - assert Interval(0, 0.5) in i - assert Interval(3, 5) not in i - assert Interval(-1, 0, closed='left') not in i - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_contains_updated_behavior(self): @@ -1014,24 +853,6 @@ def test_contains_updated_behavior(self): assert Interval(0, 1, closed='left') not in index assert Interval(0, 1, closed='both') not in index - def testcontains(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE IN 0.21 - # can select values that are IN the range of a value - i = IntervalIndex.from_arrays([0, 1], [1, 2]) - - assert i.contains(0.1) - assert i.contains(0.5) - assert i.contains(1) - assert i.contains(Interval(0, 1)) - assert i.contains(Interval(0, 2)) - - # these overlaps completely - assert i.contains(Interval(0, 3)) - assert i.contains(Interval(1, 3)) - - assert not i.contains(20) - assert not i.contains(-20) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def testcontains_updated_behavior(self): From ac818f9bef147461e53a909772527bfd336e5fde Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 17 Jul 2017 14:01:43 -0400 Subject: [PATCH 18/56] left side of interval must be <= right side --- pandas/tests/indexes/test_interval.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index ca25d62d3e1a8..b41dfdaf6e410 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -595,7 +595,7 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): assert index.slice_locs(0, 4) == # this raises an error because index is overlapping? # decreasing non-overlapping - index = IntervalIndex.from_tuples([(4, 3), (2, 1), (1, 0)]) + index = IntervalIndex.from_tuples([(3, 4), (1, 2), (0, 1)]) # These were a little mind-bending to write, would appreciate a close review assert index.slice_locs(0, 1) == (2) assert index.slice_locs(0, 2) == (1, 2) @@ -605,7 +605,7 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): assert index.slice_locs(0, 4) == (0, 1, 2) # decreasing overlapping - index = IntervalIndex.from_tuples([(4, 2), (3, 1), (2, 0)]) + index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) assert index.slice_locs(0, 1) == # this raises an error because index is overlapping? assert index.slice_locs(0, 2) == # this raises an error because index is overlapping? From 4a100079e0e6b352e7249c0c23749a426cfac552 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 17 Jul 2017 14:09:40 -0400 Subject: [PATCH 19/56] move to throwing errors for slice locs with overlapping stuff --- pandas/tests/indexes/test_interval.py | 64 ++++++++++++++------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index b41dfdaf6e410..c0484d97d3087 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -587,12 +587,12 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): # increasing overlapping index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - assert index.slice_locs(0, 1) == # this raises an error because index is overlapping? - assert index.slice_locs(0, 2) == # this raises an error because index is overlapping? - assert index.slice_locs(0, 3) == # this raises an error because index is overlapping? - assert index.slice_locs(3, 1) == # this raises an error because index is overlapping? - assert index.slice_locs(3, 4) == # this raises an error because index is overlapping? - assert index.slice_locs(0, 4) == # this raises an error because index is overlapping? + pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? # decreasing non-overlapping index = IntervalIndex.from_tuples([(3, 4), (1, 2), (0, 1)]) @@ -607,42 +607,46 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): # decreasing overlapping index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) - assert index.slice_locs(0, 1) == # this raises an error because index is overlapping? - assert index.slice_locs(0, 2) == # this raises an error because index is overlapping? - assert index.slice_locs(0, 3) == # this raises an error because index is overlapping? - assert index.slice_locs(3, 1) == # this raises an error because index is overlapping? - assert index.slice_locs(3, 4) == # this raises an error because index is overlapping? - assert index.slice_locs(0, 4) == # this raises an error because index is overlapping? + pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? + # should there be a test for get_indexer_nonunique here? # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - assert index.slice_locs(0, 1) == # this raises an error because index is overlapping? - assert index.slice_locs(0, 2) == # this raises an error because index is overlapping? - assert index.slice_locs(0, 3) == # this raises an error because index is overlapping? - assert index.slice_locs(3, 1) == # this raises an error because index is overlapping? - assert index.slice_locs(3, 4) == # this raises an error because index is overlapping? - assert index.slice_locs(0, 4) == # this raises an error because index is overlapping? + pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? + # should there be a test for get_indexer_nonunique here? # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - assert index.slice_locs(0, 1) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(0, 2) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(0, 3) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(3, 1) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(3, 4) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(0, 4) == # this raises an error because index is overlapping/unsorted? + pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? + # should there be a test for get_indexer_nonunique here? # different unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) - assert index.slice_locs(0, 1) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(0, 2) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(0, 3) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(3, 1) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(3, 4) == # this raises an error because index is overlapping/unsorted? - assert index.slice_locs(0, 4) == # this raises an error because index is overlapping/unsorted? + pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? + pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? + # should there be a test for get_indexer_nonunique here? @pytest.mark.xfail(reason="new indexing tests for issue 16316") def get_indexer_for_interval_updated_behavior(self): From e549c3dff59409d8395bae22a08fbada1011f6b3 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 17 Jul 2017 14:52:46 -0400 Subject: [PATCH 20/56] compress test cases --- pandas/tests/indexes/test_interval.py | 203 ++++++-------------------- 1 file changed, 47 insertions(+), 156 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index c0484d97d3087..aabe50ae9c299 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -570,7 +570,6 @@ def slice_locs_with_interval_updated_behavior(self): assert index.slice_locs(end=Interval(0, 2)) == () assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == () - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_ints_and_floats_updated_behavior(self): @@ -654,53 +653,33 @@ def get_indexer_for_interval_updated_behavior(self): index = IntervalIndex.from_tuples([(0, 2.5), (1, 3), (2, 4)], closed='right') # single queries - result = index.get_indexer([Interval(1, 3, closed='right')]) - expected = np.array([1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([Interval(1, 3, closed='left')]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([Interval(1, 3, closed='both')]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([Interval(1, 3, closed='neither')]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([Interval(1, 4, closed='right')]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([Interval(0, 4, closed='right')]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([Interval(1, 2, closed='right')]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + queries = [ Interval(1, 3, closed='right'), + Interval(1, 3, closed='left'), + Interval(1, 3, closed='both'), + Interval(1, 3, closed='neither'), + Interval(1, 4, closed='right'), + Interval(0, 4, closed='right'), + Interval(1, 2, closed='right') ] + expected = [1, -1, -1, -1, -1, -1, -1] + + for query, expected_result in zip(queries, expected): + result = index.get_indexer([query]) + expect = np.array([expected_result], dtype='intp') + tm.assert_numpy_array_equal(result, expect) # multiple queries - result = index.get_indexer([Interval(2, 4, closed='right'), - Interval(1, 3, closed='right') ]) - expected = np.array([2, 1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([Interval(1, 3, closed='right'), - Interval(0, 2, closed='right') ]) - expected = np.array([1, -1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + queries = [ + [Interval(2, 4, closed='right'), Interval(1, 3, closed='right')], + [Interval(1, 3, closed='right'), Interval(0, 2, closed='right')], + [Interval(1, 3, closed='right'), Interval(1, 3, closed='left')], + index ] + expected = [[2, 1], [1, -1], [1, -1], [0, 1, 2]] - result = index.get_indexer([Interval(1, 3, closed='right'), - Interval(1, 3, closed='left') ]) - expected = np.array([1, -1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + for query, expected_result in zip(queries, expected): + result = index.get_indexer(query) + expect = np.array(expected_result, dtype='intp') + tm.assert_numpy_array_equal(result, expect) - result = index.get_indexer(index) - expected = np.array([0, 1, 2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def get_indexer_for_ints_and_floats_updated_behavior(self): @@ -708,66 +687,22 @@ def get_indexer_for_ints_and_floats_updated_behavior(self): index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)], closed='right') # single queries - result = index.get_indexer([-0.5]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([0]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([0.5]) - expected = np.array([0], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([1]) - expected = np.array([0], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([1.5]) - expected = np.array([1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([2]) - expected = np.array([1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([2.5]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([3]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] + expected = [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1] - result = index.get_indexer([3.5]) - expected = np.array([2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([4]) - expected = np.array([2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([4.5]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + for query, expected_result in zip(queries, expected): + result = index.get_indexer([query]) + expect = np.array([expected_result], dtype='intp') + tm.assert_numpy_array_equal(result, expect) # multiple queries - result = index.get_indexer([1, 2]) - expected = np.array([0, 1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([1, 2, 3]) - expected = np.array([0, 1, -1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer([1, 2, 3, 4]) - expected = np.array([0, 1, -1, 2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + queries = [[1, 2],[1, 2, 3],[1, 2, 3, 4],[1, 2, 3, 4, 2]] + expected = [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]] - result = index.get_indexer([1, 2, 3, 4, 2]) - expected = np.array([0, 1, -1, 2, 1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + for query, expected_result in zip(queries, expected): + result = index.get_indexer(query) + expect = np.array(expected_result, dtype='intp') + tm.assert_numpy_array_equal(result, expect) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def get_indexer_nonunique_for_ints_and_floats_updated_behavior(self): @@ -775,66 +710,22 @@ def get_indexer_nonunique_for_ints_and_floats_updated_behavior(self): index = IntervalIndex.from_tuples([(0, 2.5), (1, 3), (2, 4)], closed='left') # single queries - result = index.get_indexer_nonunique([-0.5]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([0]) - expected = np.array([0], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([0.5]) - expected = np.array([0], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([1]) - expected = np.array([0, 1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([1.5]) - expected = np.array([0, 1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] + expected = [[-1], [0], [0], [0, 1], [0, 1], [0, 1, 2], [1, 2], [2], [2], [-1], [-1]] - result = index.get_indexer_nonunique([2]) - expected = np.array([0, 1, 2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + for query, expected_result in zip(queries, expected): + result = index.get_indexer_nonunique([query]) + expect = np.array(expected_result, dtype='intp') + tm.assert_numpy_array_equal(result, expect) - result = index.get_indexer_nonunique([2.5]) - expected = np.array([1, 2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([3]) - expected = np.array([2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([3.5]) - expected = np.array([2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([4]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([4.5]) - expected = np.array([-1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] + expected = [[0, 1, 0, 1, 2], [0, 1, 0, 1, 2, 2], [0, 1, 0, 1, 2, 2, -1], [0, 1, 0, 1, 2, 2, -1, 0, 1, 2]] # should we use tuples here? # multiple queries - result = index.get_indexer_nonunique([1, 2]) - expected = np.array([0, 1, 0, 1, 2], dtype='intp') # maybe we could put these in tuples? [(0, 1), (0, 1, 2)] - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([1, 2, 3]) - expected = np.array([0, 1, 0, 1, 2, 2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([1, 2, 3, 4]) - expected = np.array([0, 1, 0, 1, 2, 2, -1], dtype='intp') - tm.assert_numpy_array_equal(result, expected) - - result = index.get_indexer_nonunique([1, 2, 3, 4, 2]) - expected = np.array([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='intp') - tm.assert_numpy_array_equal(result, expected) + for query, expected_result in zip(queries, expected): + result = index.get_indexer_nonunique(query) + expect = np.array(expected_result, dtype='intp') + tm.assert_numpy_array_equal(result, expect) # we may also want to test get_indexer for the case when # the intervals are duplicated, decreasing, non-monotonic, etc.. From d5a8287d4ea771580061b07d0d8a9cae73050237 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 17 Jul 2017 16:16:27 -0400 Subject: [PATCH 21/56] Change to InvalidIndexError --- pandas/tests/indexes/test_interval.py | 60 +++++++++++++-------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index aabe50ae9c299..d551570e49c22 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -586,12 +586,12 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): # increasing overlapping index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? + pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) # decreasing non-overlapping index = IntervalIndex.from_tuples([(3, 4), (1, 2), (0, 1)]) @@ -606,45 +606,45 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): # decreasing overlapping index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) - pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? + pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) # should there be a test for get_indexer_nonunique here? # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? + pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) # should there be a test for get_indexer_nonunique here? # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? + pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) # should there be a test for get_indexer_nonunique here? # different unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) - pytest.raises(KeyError, index.slice_locs, [0, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 2]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 3]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 1]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [3, 4]) # is KeyError correct here? - pytest.raises(KeyError, index.slice_locs, [0, 4]) # is KeyError correct here? + pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) + pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) + pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) # should there be a test for get_indexer_nonunique here? @pytest.mark.xfail(reason="new indexing tests for issue 16316") From 0e5072905479e2323feedf05ea7809cf7fb21313 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 17 Jul 2017 16:49:05 -0400 Subject: [PATCH 22/56] compressing tests even more --- pandas/tests/indexes/test_interval.py | 212 +++++--------------------- 1 file changed, 34 insertions(+), 178 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index d551570e49c22..915841fc25b77 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -334,7 +334,27 @@ def test_get_item(self): tm.assert_index_equal(result, expected) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_loc_value_closed_right_updated_behavior(self): + def test_get_loc_interval_updated_behavior(self): + + for idx_side in ['right', 'left, both, neither']: + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) + + for bound in [[0, 1],[1, 2],[2, 3],[3, 4],[0, 2],[2.5, 3],[-1, 4]]: + for side in ['right', 'left, both, neither']: + + if idx_side == side: + if bound == [0, 1]: + assert idx.get_loc(Interval(0, 1, closed=side)) == 0 + elif bound == [2, 3]: + assert idx.get_loc(Interval(2, 3, closed=side)) == 1 + else: + pytest.raises(KeyError, idx.get_loc, Interval(*bound, closed=side)) + else: + pytest.raises(KeyError, idx.get_loc, Interval(*bound, closed=side)) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_get_loc_scalar_updated_behavior(self): right = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='right') @@ -348,41 +368,6 @@ def test_get_loc_value_closed_right_updated_behavior(self): assert right.get_loc(3) == 1 pytest.raises(KeyError, right.get_loc, 3.5) - assert right.get_loc(Interval(0, 1, closed='right')) == 0 - pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='right')) - assert right.get_loc(Interval(2, 3, closed='right')) == 1 - pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='right')) - pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='right')) - pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='right')) - pytest.raises(KeyError, right.get_loc, Interval(-1, 4, closed='right')) - - pytest.raises(KeyError, right.get_loc, Interval(0, 1, closed='left')) - pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='left')) - pytest.raises(KeyError, right.get_loc, Interval(2, 3, closed='left')) - pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='left')) - pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='left')) - pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='left')) - pytest.raises(KeyError, right.get_loc, Interval(-1, 4, closed='left')) - - pytest.raises(KeyError, right.get_loc, Interval(0, 1, closed='both')) - pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='both')) - pytest.raises(KeyError, right.get_loc, Interval(2, 3, closed='both')) - pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='both')) - pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='both')) - pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='both')) - pytest.raises(KeyError, right.get_loc, Interval(-1, 4, closed='both')) - - pytest.raises(KeyError, right.get_loc, Interval(0, 1, closed='neither')) - pytest.raises(KeyError, right.get_loc, Interval(1, 2, closed='neither')) - pytest.raises(KeyError, right.get_loc, Interval(2, 3, closed='neither')) - pytest.raises(KeyError, right.get_loc, Interval(3, 4, closed='neither')) - pytest.raises(KeyError, right.get_loc, Interval(0, 2, closed='neither')) - pytest.raises(KeyError, right.get_loc, Interval(2.5, 3, closed='neither')) - pytest.raises(KeyError, right.get_loc, Interval(-1, 4, closed='neither')) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_loc_value_closed_left_updated_behavior(self): - left = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='left') pytest.raises(KeyError, left.get_loc, -0.5) @@ -395,41 +380,6 @@ def test_get_loc_value_closed_left_updated_behavior(self): pytest.raises(KeyError, left.get_loc, 3) pytest.raises(KeyError, left.get_loc, 3.5) - pytest.raises(KeyError, left.get_loc, Interval(0, 1, closed='right')) - pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='right')) - pytest.raises(KeyError, left.get_loc, Interval(2, 3, closed='right')) - pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='right')) - pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='right')) - pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='right')) - pytest.raises(KeyError, left.get_loc, Interval(-1, 4, closed='right')) - - assert left.get_loc(Interval(0, 1, closed='left')) == 0 - pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='left')) - assert left.get_loc(Interval(2, 3, closed='left')) == 1 - pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='left')) - pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='left')) - pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='left')) - pytest.raises(KeyError, left.get_loc, Interval(-1, 4, closed='left')) - - pytest.raises(KeyError, left.get_loc, Interval(0, 1, closed='both')) - pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='both')) - pytest.raises(KeyError, left.get_loc, Interval(2, 3, closed='both')) - pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='both')) - pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='both')) - pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='both')) - pytest.raises(KeyError, left.get_loc, Interval(-1, 4, closed='both')) - - pytest.raises(KeyError, left.get_loc, Interval(0, 1, closed='neither')) - pytest.raises(KeyError, left.get_loc, Interval(1, 2, closed='neither')) - pytest.raises(KeyError, left.get_loc, Interval(2, 3, closed='neither')) - pytest.raises(KeyError, left.get_loc, Interval(3, 4, closed='neither')) - pytest.raises(KeyError, left.get_loc, Interval(0, 2, closed='neither')) - pytest.raises(KeyError, left.get_loc, Interval(2.5, 3, closed='neither')) - pytest.raises(KeyError, left.get_loc, Interval(-1, 4, closed='neither')) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_loc_value_closed_both_updated_behavior(self): - both = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='both') pytest.raises(KeyError, both.get_loc, -0.5) @@ -442,41 +392,6 @@ def test_get_loc_value_closed_both_updated_behavior(self): assert both.get_loc(3) == 1 pytest.raises(KeyError, both.get_loc, 3.5) - pytest.raises(KeyError, both.get_loc, Interval(0, 1, closed='right')) - pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='right')) - pytest.raises(KeyError, both.get_loc, Interval(2, 3, closed='right')) - pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='right')) - pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='right')) - pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='right')) - pytest.raises(KeyError, both.get_loc, Interval(-1, 4, closed='right')) - - pytest.raises(KeyError, both.get_loc, Interval(0, 1, closed='left')) - pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='left')) - pytest.raises(KeyError, both.get_loc, Interval(2, 3, closed='left')) - pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='left')) - pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='left')) - pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='left')) - pytest.raises(KeyError, both.get_loc, Interval(-1, 4, closed='left')) - - assert both.get_loc(Interval(0, 1, closed='both')) == 0 - pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='both')) - assert both.get_loc(Interval(2, 3, closed='both')) == 1 - pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='both')) - pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='both')) - pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='both')) - pytest.raises(KeyError, both.get_loc, Interval(-1, 4, closed='both')) - - pytest.raises(KeyError, both.get_loc, Interval(0, 1, closed='neither')) - pytest.raises(KeyError, both.get_loc, Interval(1, 2, closed='neither')) - pytest.raises(KeyError, both.get_loc, Interval(2, 3, closed='neither')) - pytest.raises(KeyError, both.get_loc, Interval(3, 4, closed='neither')) - pytest.raises(KeyError, both.get_loc, Interval(0, 2, closed='neither')) - pytest.raises(KeyError, both.get_loc, Interval(2.5, 3, closed='neither')) - pytest.raises(KeyError, both.get_loc, Interval(-1, 4, closed='neither')) - - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_loc_value_closed_neither_updated_behavior(self): - neither = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='neither') pytest.raises(KeyError, neither.get_loc, -0.5) @@ -489,41 +404,9 @@ def test_get_loc_value_closed_neither_updated_behavior(self): pytest.raises(KeyError, neither.get_loc, 3) pytest.raises(KeyError, neither.get_loc, 3.5) - pytest.raises(KeyError, neither.get_loc, Interval(0, 1, closed='right')) - pytest.raises(KeyError, neither.get_loc, Interval(1, 2, closed='right')) - pytest.raises(KeyError, neither.get_loc, Interval(2, 3, closed='right')) - pytest.raises(KeyError, neither.get_loc, Interval(3, 4, closed='right')) - pytest.raises(KeyError, neither.get_loc, Interval(0, 2, closed='right')) - pytest.raises(KeyError, neither.get_loc, Interval(2.5, 3, closed='right')) - pytest.raises(KeyError, neither.get_loc, Interval(-1, 4, closed='right')) - - pytest.raises(KeyError, neither.get_loc, Interval(0, 1, closed='left')) - pytest.raises(KeyError, neither.get_loc, Interval(1, 2, closed='left')) - pytest.raises(KeyError, neither.get_loc, Interval(2, 3, closed='left')) - pytest.raises(KeyError, neither.get_loc, Interval(3, 4, closed='left')) - pytest.raises(KeyError, neither.get_loc, Interval(0, 2, closed='left')) - pytest.raises(KeyError, neither.get_loc, Interval(2.5, 3, closed='left')) - pytest.raises(KeyError, neither.get_loc, Interval(-1, 4, closed='left')) - - pytest.raises(KeyError, neither.get_loc, Interval(0, 1 closed='both')) - pytest.raises(KeyError, neither.get_loc, Interval(1, 2, closed='both')) - pytest.raises(KeyError, neither.get_loc, Interval(2, 3, closed='both')) - pytest.raises(KeyError, neither.get_loc, Interval(3, 4, closed='both')) - pytest.raises(KeyError, neither.get_loc, Interval(0, 2, closed='both')) - pytest.raises(KeyError, neither.get_loc, Interval(2.5, 3, closed='both')) - pytest.raises(KeyError, neither.get_loc, Interval(-1, 4, closed='both')) - - assert neither.get_loc(Interval(0, 1, closed='neither')) == 0 - pytest.raises(KeyError, neither.get_loc, Interval(1, 2, closed='neither')) - assert neither.get_loc(Interval(2, 3, closed='neither')) == 1 - pytest.raises(KeyError, neither.get_loc, Interval(3, 4, closed='neither')) - pytest.raises(KeyError, neither.get_loc, Interval(0, 2, closed='neither')) - pytest.raises(KeyError, neither.get_loc, Interval(2.5, 3, closed='neither')) - pytest.raises(KeyError, neither.get_loc, Interval(-1, 4, closed='neither')) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_interval_updated_behavior(self): - # @jreback might prefer these be broken up into separate methods # increasing index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) @@ -573,6 +456,8 @@ def slice_locs_with_interval_updated_behavior(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_ints_and_floats_updated_behavior(self): + queries = [[0, 1], [0, 2], [0, 3], [3, 1], [3, 4], [0, 4]] + # increasing non-overlapping index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)]) @@ -585,13 +470,8 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): # increasing overlapping index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - - pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) + for query in queries: + pytest.raises(InvalidIndexError, index.slice_locs, query) # decreasing non-overlapping index = IntervalIndex.from_tuples([(3, 4), (1, 2), (0, 1)]) @@ -605,47 +485,23 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): # decreasing overlapping index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) - - pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) - # should there be a test for get_indexer_nonunique here? + for query in queries: + pytest.raises(InvalidIndexError, index.slice_locs, query) # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - - pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) - # should there be a test for get_indexer_nonunique here? + for query in queries: + pytest.raises(InvalidIndexError, index.slice_locs, query) # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - - pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) - # should there be a test for get_indexer_nonunique here? + for query in queries: + pytest.raises(InvalidIndexError, index.slice_locs, query) # different unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) - - pytest.raises(InvalidIndexError, index.slice_locs, [0, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 2]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 3]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 1]) - pytest.raises(InvalidIndexError, index.slice_locs, [3, 4]) - pytest.raises(InvalidIndexError, index.slice_locs, [0, 4]) - # should there be a test for get_indexer_nonunique here? + for query in queries: + pytest.raises(InvalidIndexError, index.slice_locs, query) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def get_indexer_for_interval_updated_behavior(self): @@ -719,7 +575,7 @@ def get_indexer_nonunique_for_ints_and_floats_updated_behavior(self): tm.assert_numpy_array_equal(result, expect) queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] - expected = [[0, 1, 0, 1, 2], [0, 1, 0, 1, 2, 2], [0, 1, 0, 1, 2, 2, -1], [0, 1, 0, 1, 2, 2, -1, 0, 1, 2]] # should we use tuples here? + expected = [[0, 1, 0, 1, 2], [0, 1, 0, 1, 2, 2], [0, 1, 0, 1, 2, 2, -1], [0, 1, 0, 1, 2, 2, -1, 0, 1, 2]] # should we use tuples here, e.g. [(0, 1), (0, 1, 2)]? # multiple queries for query, expected_result in zip(queries, expected): From 2c953b65cfbaba110d8bf0143f3de6fa688354c5 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 17 Jul 2017 16:54:29 -0400 Subject: [PATCH 23/56] some lint issues --- pandas/tests/indexes/test_interval.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 915841fc25b77..15d6fad2abab7 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -340,7 +340,7 @@ def test_get_loc_interval_updated_behavior(self): idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) - for bound in [[0, 1],[1, 2],[2, 3],[3, 4],[0, 2],[2.5, 3],[-1, 4]]: + for bound in [[0, 1], [1, 2], [2, 3], [3, 4], [0, 2], [2.5, 3], [-1, 4]]: for side in ['right', 'left, both, neither']: if idx_side == side: @@ -361,21 +361,21 @@ def test_get_loc_scalar_updated_behavior(self): pytest.raises(KeyError, right.get_loc, -0.5) pytest.raises(KeyError, right.get_loc, 0) assert right.get_loc(0.5) == 0 - assert right.get_loc(1) == 0 + assert right.get_loc(1) == 0 pytest.raises(KeyError, right.get_loc, 1.5) pytest.raises(KeyError, right.get_loc, 2) assert right.get_loc(2.5) == 1 - assert right.get_loc(3) == 1 + assert right.get_loc(3) == 1 pytest.raises(KeyError, right.get_loc, 3.5) left = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='left') pytest.raises(KeyError, left.get_loc, -0.5) - assert left.get_loc(0) == 0 + assert left.get_loc(0) == 0 assert left.get_loc(0.5) == 0 pytest.raises(KeyError, left.get_loc, 1) pytest.raises(KeyError, left.get_loc, 1.5) - assert left.get_loc(2) == 1 + assert left.get_loc(2) == 1 assert left.get_loc(2.5) == 1 pytest.raises(KeyError, left.get_loc, 3) pytest.raises(KeyError, left.get_loc, 3.5) @@ -383,13 +383,13 @@ def test_get_loc_scalar_updated_behavior(self): both = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='both') pytest.raises(KeyError, both.get_loc, -0.5) - assert both.get_loc(0) == 0 + assert both.get_loc(0) == 0 assert both.get_loc(0.5) == 0 - assert both.get_loc(1) == 0 + assert both.get_loc(1) == 0 pytest.raises(KeyError, both.get_loc, 1.5) - assert both.get_loc(2) == 1 + assert both.get_loc(2) == 1 assert both.get_loc(2.5) == 1 - assert both.get_loc(3) == 1 + assert both.get_loc(3) == 1 pytest.raises(KeyError, both.get_loc, 3.5) neither = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='neither') @@ -552,7 +552,7 @@ def get_indexer_for_ints_and_floats_updated_behavior(self): tm.assert_numpy_array_equal(result, expect) # multiple queries - queries = [[1, 2],[1, 2, 3],[1, 2, 3, 4],[1, 2, 3, 4, 2]] + queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] expected = [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]] for query, expected_result in zip(queries, expected): From 08d315c2add71c6a6e5b44370494ed77182943cc Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 18 Jul 2017 12:00:10 -0400 Subject: [PATCH 24/56] I believe all the test cases are complete. --- pandas/tests/indexes/test_interval.py | 40 +++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 15d6fad2abab7..6df720fce1d61 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -408,41 +408,41 @@ def test_get_loc_scalar_updated_behavior(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_interval_updated_behavior(self): - # increasing + # increasing overlapping index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 2) + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 3) - assert index.slice_locs(end=Interval(2, 4)) == (0, 2) - assert index.slice_locs(end=Interval(0, 2)) == () - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == () + assert index.slice_locs(end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(end=Interval(0, 2)) == (0, 1) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 1) - # decreasing + # decreasing overlapping index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == () + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (2, 1) assert index.slice_locs(start=Interval(0, 2)) == (2, 3) - assert index.slice_locs(end=Interval(2, 4)) == () - assert index.slice_locs(end=Interval(0, 2)) == (0, 2) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (0, 2) + assert index.slice_locs(end=Interval(2, 4)) == (0, 1) + assert index.slice_locs(end=Interval(0, 2)) == (0, 3) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (0, 3) # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 2) + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 3) - assert index.slice_locs(end=Interval(2, 4)) == (0, 2) - assert index.slice_locs(end=Interval(0, 2)) == () # questionable - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == () + assert index.slice_locs(end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(end=Interval(0, 2)) == (0, 2) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == # This should raise? - assert index.slice_locs(start=Interval(0, 2)) == # This should raise? + pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2), end=Interval(2, 4))) + pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2))) assert index.slice_locs(end=Interval(2, 4)) == (0, 2) - assert index.slice_locs(end=Interval(0, 2)) == # This should raise? - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == # This should raise? + pytest.raises(KeyError, index.slice_locs(end=Interval(0, 2))) + pytest.raises(KeyError, index.slice_locs(start=Interval(2, 4), end=Interval(0, 2))) # different unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) @@ -450,8 +450,8 @@ def slice_locs_with_interval_updated_behavior(self): assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 4) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) - assert index.slice_locs(end=Interval(0, 2)) == () - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == () + assert index.slice_locs(end=Interval(0, 2)) == (0, 2) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_ints_and_floats_updated_behavior(self): From 9f905a8487604a75e98ebda1232d0e2bde318286 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 18 Jul 2017 12:02:15 -0400 Subject: [PATCH 25/56] autopep --- pandas/tests/indexes/test_interval.py | 87 ++++++++++++++++----------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 6df720fce1d61..ac6754a189764 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -345,13 +345,17 @@ def test_get_loc_interval_updated_behavior(self): if idx_side == side: if bound == [0, 1]: - assert idx.get_loc(Interval(0, 1, closed=side)) == 0 + assert idx.get_loc( + Interval(0, 1, closed=side)) == 0 elif bound == [2, 3]: - assert idx.get_loc(Interval(2, 3, closed=side)) == 1 + assert idx.get_loc( + Interval(2, 3, closed=side)) == 1 else: - pytest.raises(KeyError, idx.get_loc, Interval(*bound, closed=side)) + pytest.raises(KeyError, idx.get_loc, + Interval(*bound, closed=side)) else: - pytest.raises(KeyError, idx.get_loc, Interval(*bound, closed=side)) + pytest.raises(KeyError, idx.get_loc, + Interval(*bound, closed=side)) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_loc_scalar_updated_behavior(self): @@ -404,54 +408,63 @@ def test_get_loc_scalar_updated_behavior(self): pytest.raises(KeyError, neither.get_loc, 3) pytest.raises(KeyError, neither.get_loc, 3.5) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_interval_updated_behavior(self): # increasing overlapping index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(start=Interval( + 0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 3) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) assert index.slice_locs(end=Interval(0, 2)) == (0, 1) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 1) + assert index.slice_locs(start=Interval( + 2, 4), end=Interval(0, 2)) == (2, 1) # decreasing overlapping index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (2, 1) + assert index.slice_locs(start=Interval( + 0, 2), end=Interval(2, 4)) == (2, 1) assert index.slice_locs(start=Interval(0, 2)) == (2, 3) assert index.slice_locs(end=Interval(2, 4)) == (0, 1) assert index.slice_locs(end=Interval(0, 2)) == (0, 3) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (0, 3) + assert index.slice_locs(start=Interval( + 2, 4), end=Interval(0, 2)) == (0, 3) # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(start=Interval( + 0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 3) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) assert index.slice_locs(end=Interval(0, 2)) == (0, 2) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) + assert index.slice_locs(start=Interval( + 2, 4), end=Interval(0, 2)) == (2, 2) # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2), end=Interval(2, 4))) + pytest.raises(KeyError, index.slice_locs( + start=Interval(0, 2), end=Interval(2, 4))) pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2))) assert index.slice_locs(end=Interval(2, 4)) == (0, 2) pytest.raises(KeyError, index.slice_locs(end=Interval(0, 2))) - pytest.raises(KeyError, index.slice_locs(start=Interval(2, 4), end=Interval(0, 2))) + pytest.raises(KeyError, index.slice_locs( + start=Interval(2, 4), end=Interval(0, 2))) # different unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(start=Interval( + 0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 4) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) assert index.slice_locs(end=Interval(0, 2)) == (0, 2) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) + assert index.slice_locs(start=Interval( + 2, 4), end=Interval(0, 2)) == (2, 2) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_ints_and_floats_updated_behavior(self): @@ -475,7 +488,8 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): # decreasing non-overlapping index = IntervalIndex.from_tuples([(3, 4), (1, 2), (0, 1)]) - # These were a little mind-bending to write, would appreciate a close review + # These were a little mind-bending to write, would appreciate a close + # review assert index.slice_locs(0, 1) == (2) assert index.slice_locs(0, 2) == (1, 2) assert index.slice_locs(0, 3) == (1, 2) @@ -506,16 +520,17 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def get_indexer_for_interval_updated_behavior(self): - index = IntervalIndex.from_tuples([(0, 2.5), (1, 3), (2, 4)], closed='right') + index = IntervalIndex.from_tuples( + [(0, 2.5), (1, 3), (2, 4)], closed='right') # single queries - queries = [ Interval(1, 3, closed='right'), - Interval(1, 3, closed='left'), - Interval(1, 3, closed='both'), - Interval(1, 3, closed='neither'), - Interval(1, 4, closed='right'), - Interval(0, 4, closed='right'), - Interval(1, 2, closed='right') ] + queries = [Interval(1, 3, closed='right'), + Interval(1, 3, closed='left'), + Interval(1, 3, closed='both'), + Interval(1, 3, closed='neither'), + Interval(1, 4, closed='right'), + Interval(0, 4, closed='right'), + Interval(1, 2, closed='right')] expected = [1, -1, -1, -1, -1, -1, -1] for query, expected_result in zip(queries, expected): @@ -528,7 +543,7 @@ def get_indexer_for_interval_updated_behavior(self): [Interval(2, 4, closed='right'), Interval(1, 3, closed='right')], [Interval(1, 3, closed='right'), Interval(0, 2, closed='right')], [Interval(1, 3, closed='right'), Interval(1, 3, closed='left')], - index ] + index] expected = [[2, 1], [1, -1], [1, -1], [0, 1, 2]] for query, expected_result in zip(queries, expected): @@ -536,14 +551,14 @@ def get_indexer_for_interval_updated_behavior(self): expect = np.array(expected_result, dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def get_indexer_for_ints_and_floats_updated_behavior(self): - index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)], closed='right') + index = IntervalIndex.from_tuples( + [(0, 1), (1, 2), (3, 4)], closed='right') # single queries - queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] + queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] expected = [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1] for query, expected_result in zip(queries, expected): @@ -552,7 +567,7 @@ def get_indexer_for_ints_and_floats_updated_behavior(self): tm.assert_numpy_array_equal(result, expect) # multiple queries - queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] + queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] expected = [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]] for query, expected_result in zip(queries, expected): @@ -563,19 +578,22 @@ def get_indexer_for_ints_and_floats_updated_behavior(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def get_indexer_nonunique_for_ints_and_floats_updated_behavior(self): - index = IntervalIndex.from_tuples([(0, 2.5), (1, 3), (2, 4)], closed='left') + index = IntervalIndex.from_tuples( + [(0, 2.5), (1, 3), (2, 4)], closed='left') # single queries - queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] - expected = [[-1], [0], [0], [0, 1], [0, 1], [0, 1, 2], [1, 2], [2], [2], [-1], [-1]] + queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] + expected = [[-1], [0], [0], [0, 1], [0, 1], + [0, 1, 2], [1, 2], [2], [2], [-1], [-1]] for query, expected_result in zip(queries, expected): result = index.get_indexer_nonunique([query]) expect = np.array(expected_result, dtype='intp') tm.assert_numpy_array_equal(result, expect) - queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] - expected = [[0, 1, 0, 1, 2], [0, 1, 0, 1, 2, 2], [0, 1, 0, 1, 2, 2, -1], [0, 1, 0, 1, 2, 2, -1, 0, 1, 2]] # should we use tuples here, e.g. [(0, 1), (0, 1, 2)]? + queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] + expected = [[0, 1, 0, 1, 2], [0, 1, 0, 1, 2, 2], + [0, 1, 0, 1, 2, 2, -1], [0, 1, 0, 1, 2, 2, -1, 0, 1, 2]] # should we use tuples here, e.g. [(0, 1), (0, 1, 2)]? # multiple queries for query, expected_result in zip(queries, expected): @@ -855,6 +873,7 @@ def f(): class TestIntervalTree(object): + def setup_method(self, method): gentree = lambda dtype: IntervalTree(np.arange(5, dtype=dtype), np.arange(5, dtype=dtype) + 2) From e1eeb59c57fba8fb23b42d26a7435a96dffebc63 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 18 Jul 2017 12:04:19 -0400 Subject: [PATCH 26/56] more pep8 --- pandas/tests/indexes/test_interval.py | 4 ++-- pandas/tests/indexing/test_interval.py | 14 ++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index ac6754a189764..f4ff6f8fbcc7a 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -559,7 +559,7 @@ def get_indexer_for_ints_and_floats_updated_behavior(self): # single queries queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] - expected = [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1] + expected = [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1] for query, expected_result in zip(queries, expected): result = index.get_indexer([query]) @@ -582,7 +582,7 @@ def get_indexer_nonunique_for_ints_and_floats_updated_behavior(self): [(0, 2.5), (1, 3), (2, 4)], closed='left') # single queries - queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] + queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] expected = [[-1], [0], [0], [0, 1], [0, 1], [0, 1, 2], [1, 2], [2], [2], [-1], [-1]] diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index c6248c959aaa1..6b0a4f4ce21b9 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -68,7 +68,7 @@ def test_getitem_with_scalar(self): tm.assert_series_equal(expected, s[s >= 2]) def test_loc_and_getitem_with_interval(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE + # THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE s = self.s expected = 0 @@ -104,7 +104,6 @@ def test_loc_and_getitem_with_interval(self): with pytest.raises(KeyError): s[Interval(5, 6)] - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_loc_and_getitem_with_interval_updated_behavior(self): @@ -131,7 +130,7 @@ def test_loc_and_getitem_with_interval_updated_behavior(self): s[Interval(5, 6)] def test_with_slices(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE + # THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE s = self.s @@ -184,14 +183,14 @@ def test_with_slices_updated_behavior(self): # slice of scalar with pytest.raises(NotImplementedError): - s[0:4] ## not sure what the behvaior should be here. + s[0:4] # not sure what the behvaior should be here. # slice of scalar with step != 1 with pytest.raises(ValueError): - s[0:4:2] ## This should probably definitely fail I guess? + s[0:4:2] # This should probably definitely fail I guess? def test_with_overlaps(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE + # THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE s = self.s expected = s.iloc[[3, 4, 3, 4]] @@ -273,7 +272,7 @@ def test_non_unique(self): tm.assert_series_equal(expected, result) def test_non_unique_moar(self): - ### THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE + # THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) s = Series(range(len(idx)), index=idx) @@ -366,4 +365,3 @@ def test_loc_getitem_frame(self): # partial missing with pytest.raises(KeyError): df.loc[[10, 4]] - From 4c54f332b4d62e0d90c68afd64514cd80b0855f5 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 18 Jul 2017 12:07:39 -0400 Subject: [PATCH 27/56] remove some stuff --- pandas/tests/indexing/test_interval.py | 116 ------------------------- 1 file changed, 116 deletions(-) diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index 6b0a4f4ce21b9..161254e027d86 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -67,43 +67,6 @@ def test_getitem_with_scalar(self): expected = s.iloc[2:5] tm.assert_series_equal(expected, s[s >= 2]) - def test_loc_and_getitem_with_interval(self): - # THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE - - s = self.s - expected = 0 - - result = s.loc[Interval(0, 1)] - assert result == expected - - result = s[Interval(0, 1)] - assert result == expected - - expected = s.iloc[3:5] - result = s.loc[Interval(3, 6)] - tm.assert_series_equal(expected, result) - - expected = s.iloc[3:5] - result = s.loc[[Interval(3, 6)]] - tm.assert_series_equal(expected, result) - - expected = s.iloc[3:5] - result = s.loc[[Interval(3, 5)]] - tm.assert_series_equal(expected, result) - - # missing - with pytest.raises(KeyError): - s.loc[Interval(-2, 0)] - - with pytest.raises(KeyError): - s[Interval(-2, 0)] - - with pytest.raises(KeyError): - s.loc[Interval(5, 6)] - - with pytest.raises(KeyError): - s[Interval(5, 6)] - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_loc_and_getitem_with_interval_updated_behavior(self): @@ -129,26 +92,6 @@ def test_loc_and_getitem_with_interval_updated_behavior(self): with pytest.raises(KeyError): s[Interval(5, 6)] - def test_with_slices(self): - # THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE - - s = self.s - - # slice of interval - with pytest.raises(NotImplementedError): - result = s.loc[Interval(3, 6):] - - with pytest.raises(NotImplementedError): - result = s[Interval(3, 6):] - - expected = s.iloc[3:5] - result = s[[Interval(3, 6)]] - tm.assert_series_equal(expected, result) - - # slice of scalar with step != 1 - with pytest.raises(ValueError): - s[0:4:2] - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_with_slices_updated_behavior(self): @@ -189,44 +132,6 @@ def test_with_slices_updated_behavior(self): with pytest.raises(ValueError): s[0:4:2] # This should probably definitely fail I guess? - def test_with_overlaps(self): - # THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE - - s = self.s - expected = s.iloc[[3, 4, 3, 4]] - result = s.loc[[Interval(3, 6), Interval(3, 6)]] - tm.assert_series_equal(expected, result) - - idx = IntervalIndex.from_tuples([(1, 5), (3, 7)]) - s = Series(range(len(idx)), index=idx) - - result = s[4] - expected = s - tm.assert_series_equal(expected, result) - - result = s[[4]] - expected = s - tm.assert_series_equal(expected, result) - - result = s.loc[[4]] - expected = s - tm.assert_series_equal(expected, result) - - result = s[Interval(3, 5)] - expected = s - tm.assert_series_equal(expected, result) - - result = s.loc[Interval(3, 5)] - expected = s - tm.assert_series_equal(expected, result) - - # doesn't intersect unique set of intervals - with pytest.raises(KeyError): - s[[Interval(3, 5)]] - - with pytest.raises(KeyError): - s.loc[[Interval(3, 5)]] - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_with_overlaps_updated_behavior(self): @@ -271,27 +176,6 @@ def test_non_unique(self): expected = s.iloc[0:1] tm.assert_series_equal(expected, result) - def test_non_unique_moar(self): - # THIS METHOD TO BE REMOVED FOR BEHAVIOR UPDATE - - idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) - s = Series(range(len(idx)), index=idx) - - result = s.loc[Interval(1, 3)] - expected = s.iloc[[0, 1]] - tm.assert_series_equal(expected, result) - - # non-unique index and slices not allowed - with pytest.raises(ValueError): - s.loc[Interval(1, 3):] - - with pytest.raises(ValueError): - s[Interval(1, 3):] - - # non-unique - with pytest.raises(ValueError): - s[[Interval(1, 3)]] - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_non_unique_moar_updated_behavior(self): From ca04cb2bf92825da9d9d1ed4f6a58204c93b00ae Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 18 Jul 2017 12:16:10 -0400 Subject: [PATCH 28/56] tiny update --- pandas/tests/indexing/test_interval.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index 161254e027d86..2f6670b548e13 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -125,12 +125,13 @@ def test_with_slices_updated_behavior(self): s[Interval(3, 4, closed='both'):] # slice of scalar - with pytest.raises(NotImplementedError): - s[0:4] # not sure what the behvaior should be here. + expected = s.iloc[:4] # maybe [:5] ? + result = s[0:4] + tm.assert_series_equal(expected, result) # slice of scalar with step != 1 - with pytest.raises(ValueError): - s[0:4:2] # This should probably definitely fail I guess? + with pytest.raises(NotImplementedError): + s[0:4:2] @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_with_overlaps_updated_behavior(self): From bfaefef7c88b190f901532f8086be41ad48f106b Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Wed, 26 Jul 2017 11:18:46 -0400 Subject: [PATCH 29/56] update get_indexer_non_unique tests --- pandas/tests/indexes/test_interval.py | 40 ++++++++++++++++----------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index f4ff6f8fbcc7a..12ee4394ca392 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -3,9 +3,8 @@ import pytest import numpy as np -from pandas import (Interval, IntervalIndex, Index, isnull, - interval_range, Timestamp, Timedelta, - compat) +from pandas import (Interval, IntervalIndex, Int64Index, Index, isnull, + interval_range, Timestamp, Timedelta, compat) from pandas._libs.interval import IntervalTree from pandas.tests.indexes.common import Base import pandas.util.testing as tm @@ -576,30 +575,39 @@ def get_indexer_for_ints_and_floats_updated_behavior(self): tm.assert_numpy_array_equal(result, expect) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def get_indexer_nonunique_for_ints_and_floats_updated_behavior(self): + def get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='left') # single queries queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] - expected = [[-1], [0], [0], [0, 1], [0, 1], - [0, 1, 2], [1, 2], [2], [2], [-1], [-1]] + expected = [(Int64Index([], dtype='int64'), array([0])) + (Int64Index([0], dtype='int64'), array([])) + (Int64Index([0], dtype='int64'), array([])) + (Int64Index([0, 1], dtype='int64'), array([])) + (Int64Index([0, 1], dtype='int64'), array([])) + (Int64Index([0, 1, 2], dtype='int64'), array([])) + (Int64Index([1, 2], dtype='int64'), array([])) + (Int64Index([2], dtype='int64'), array([])) + (Int64Index([2], dtype='int64'), array([])) + (Int64Index([], dtype='int64'), array([0])) + (Int64Index([], dtype='int64'), array([0])) ] for query, expected_result in zip(queries, expected): - result = index.get_indexer_nonunique([query]) - expect = np.array(expected_result, dtype='intp') - tm.assert_numpy_array_equal(result, expect) + result = index.get_indexer_non_unique([query]) + tm.assert_numpy_array_equal(result, expected_result) + # multiple queries queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] - expected = [[0, 1, 0, 1, 2], [0, 1, 0, 1, 2, 2], - [0, 1, 0, 1, 2, 2, -1], [0, 1, 0, 1, 2, 2, -1, 0, 1, 2]] # should we use tuples here, e.g. [(0, 1), (0, 1, 2)]? + expected = [(Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])) + (Int64Index([0, 1, 0, 1, 2, 2], dtype='int64'), np.array([])) + (Int64Index([0, 1, 0, 1, 2, 2, -1], dtype='int64'), np.array([3])) + (Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='int64'), np.array([3])) ] - # multiple queries for query, expected_result in zip(queries, expected): - result = index.get_indexer_nonunique(query) - expect = np.array(expected_result, dtype='intp') - tm.assert_numpy_array_equal(result, expect) + result = index.get_indexer_non_unique(query) + tm.assert_numpy_array_equal(result, expected_result) # we may also want to test get_indexer for the case when # the intervals are duplicated, decreasing, non-monotonic, etc.. @@ -871,7 +879,6 @@ def f(): pytest.raises(ValueError, f) - class TestIntervalTree(object): def setup_method(self, method): @@ -899,6 +906,7 @@ def test_get_indexer(self): tree.get_indexer(np.array([3.0])) def test_get_indexer_non_unique(self): + # this might not be consistent with the new behavior anymore... indexer, missing = self.tree.get_indexer_non_unique( np.array([1.0, 2.0, 6.5])) tm.assert_numpy_array_equal(indexer[:1], From ce5074a3663a7f5b20f08003e2b3b6e13aef3f1c Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Wed, 26 Jul 2017 11:20:27 -0400 Subject: [PATCH 30/56] autopep --- pandas/tests/indexes/test_interval.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 12ee4394ca392..a5f330a9b4486 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -592,7 +592,7 @@ def get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): (Int64Index([2], dtype='int64'), array([])) (Int64Index([2], dtype='int64'), array([])) (Int64Index([], dtype='int64'), array([0])) - (Int64Index([], dtype='int64'), array([0])) ] + (Int64Index([], dtype='int64'), array([0]))] for query, expected_result in zip(queries, expected): result = index.get_indexer_non_unique([query]) @@ -601,9 +601,12 @@ def get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): # multiple queries queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] expected = [(Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])) - (Int64Index([0, 1, 0, 1, 2, 2], dtype='int64'), np.array([])) - (Int64Index([0, 1, 0, 1, 2, 2, -1], dtype='int64'), np.array([3])) - (Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='int64'), np.array([3])) ] + (Int64Index([0, 1, 0, 1, 2, 2], + dtype='int64'), np.array([])) + (Int64Index([0, 1, 0, 1, 2, 2, -1], + dtype='int64'), np.array([3])) + (Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], + dtype='int64'), np.array([3]))] for query, expected_result in zip(queries, expected): result = index.get_indexer_non_unique(query) @@ -879,6 +882,7 @@ def f(): pytest.raises(ValueError, f) + class TestIntervalTree(object): def setup_method(self, method): From 4c5496e4299d44cc08e3296fd17d62cf773c4e6e Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 3 Aug 2017 14:11:41 -0400 Subject: [PATCH 31/56] hopefully this is valid python --- pandas/tests/indexes/test_interval.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index a5f330a9b4486..377a5785fac9c 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -339,7 +339,8 @@ def test_get_loc_interval_updated_behavior(self): idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) - for bound in [[0, 1], [1, 2], [2, 3], [3, 4], [0, 2], [2.5, 3], [-1, 4]]: + for bound in [[0, 1], [1, 2], [2, 3], [3, 4], + [0, 2], [2.5, 3], [-1, 4]]: for side in ['right', 'left, both, neither']: if idx_side == side: From 7f4c5e5193fa4ad3a66f9ccfebd6d2f0b74fb383 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 15 Sep 2017 10:34:18 -0400 Subject: [PATCH 32/56] a couple updates, and a bug fix --- pandas/tests/indexes/test_interval.py | 67 +++++++-------------------- 1 file changed, 18 insertions(+), 49 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 0c099f8230b96..adb93ca7d6aef 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -338,13 +338,13 @@ def test_get_item(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_loc_interval_updated_behavior(self): - for idx_side in ['right', 'left, both, neither']: + for idx_side in ['right', 'left', 'both', 'neither']: idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) for bound in [[0, 1], [1, 2], [2, 3], [3, 4], [0, 2], [2.5, 3], [-1, 4]]: - for side in ['right', 'left, both, neither']: + for side in ['right', 'left', 'both', 'neither']: if idx_side == side: if bound == [0, 1]: @@ -363,53 +363,22 @@ def test_get_loc_interval_updated_behavior(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_loc_scalar_updated_behavior(self): - right = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='right') - - pytest.raises(KeyError, right.get_loc, -0.5) - pytest.raises(KeyError, right.get_loc, 0) - assert right.get_loc(0.5) == 0 - assert right.get_loc(1) == 0 - pytest.raises(KeyError, right.get_loc, 1.5) - pytest.raises(KeyError, right.get_loc, 2) - assert right.get_loc(2.5) == 1 - assert right.get_loc(3) == 1 - pytest.raises(KeyError, right.get_loc, 3.5) - - left = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='left') - - pytest.raises(KeyError, left.get_loc, -0.5) - assert left.get_loc(0) == 0 - assert left.get_loc(0.5) == 0 - pytest.raises(KeyError, left.get_loc, 1) - pytest.raises(KeyError, left.get_loc, 1.5) - assert left.get_loc(2) == 1 - assert left.get_loc(2.5) == 1 - pytest.raises(KeyError, left.get_loc, 3) - pytest.raises(KeyError, left.get_loc, 3.5) - - both = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='both') - - pytest.raises(KeyError, both.get_loc, -0.5) - assert both.get_loc(0) == 0 - assert both.get_loc(0.5) == 0 - assert both.get_loc(1) == 0 - pytest.raises(KeyError, both.get_loc, 1.5) - assert both.get_loc(2) == 1 - assert both.get_loc(2.5) == 1 - assert both.get_loc(3) == 1 - pytest.raises(KeyError, both.get_loc, 3.5) - - neither = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed='neither') - - pytest.raises(KeyError, neither.get_loc, -0.5) - pytest.raises(KeyError, neither.get_loc, 0) - assert neither.get_loc(0.5) == 0 - pytest.raises(KeyError, neither.get_loc, 1) - pytest.raises(KeyError, neither.get_loc, 1.5) - pytest.raises(KeyError, neither.get_loc, 2) - assert neither.get_loc(2.5) == 1 - pytest.raises(KeyError, neither.get_loc, 3) - pytest.raises(KeyError, neither.get_loc, 3.5) + scalars = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5] + correct = {'right': {0.5: 0, 1: 0, 2.5: 1, 3: 1} + 'left': {0: 0, 0.5: 0, 2: 1, 2.5: 1} + 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1} + 'neither': {0.5: 0, 2.5: 1} } + + for idx_side in ['right', 'left', 'both', 'neither']: + + idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) + + for scalar in scalars: + + if scalar in correct[idx_side].keys(): + assert idx.get_loc(scalar) == correct[idx_side][scalar] + else: + pytest.raises(KeyError, idx.get_loc, scalar) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def slice_locs_with_interval_updated_behavior(self): From 0aaaddf429be6dec9283b1a31ae480719b8c36f1 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 15 Sep 2017 10:36:54 -0400 Subject: [PATCH 33/56] oops --- pandas/tests/indexes/test_interval.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index adb93ca7d6aef..d95eded9e9eca 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -364,10 +364,10 @@ def test_get_loc_interval_updated_behavior(self): def test_get_loc_scalar_updated_behavior(self): scalars = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5] - correct = {'right': {0.5: 0, 1: 0, 2.5: 1, 3: 1} - 'left': {0: 0, 0.5: 0, 2: 1, 2.5: 1} - 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1} - 'neither': {0.5: 0, 2.5: 1} } + correct = {'right': {0.5: 0, 1: 0, 2.5: 1, 3: 1}, + 'left': {0: 0, 0.5: 0, 2: 1, 2.5: 1}, + 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1}, + 'neither': {0.5: 0, 2.5: 1}} for idx_side in ['right', 'left', 'both', 'neither']: From a44c926e8af742bba006863bbf5a1dc44cee1e9d Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 15 Sep 2017 18:09:54 -0400 Subject: [PATCH 34/56] fix some lint issues --- pandas/tests/indexes/test_interval.py | 32 +++++++++++++------------- pandas/tests/indexing/test_interval.py | 1 - 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index d95eded9e9eca..6de027091ee2a 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -4,8 +4,8 @@ import numpy as np from datetime import timedelta -from pandas import (Interval, IntervalIndex, Index, isna, - interval_range, Timestamp, Timedelta, +from pandas import (Interval, IntervalIndex, Index, Int64Index, isna, + interval_range, Timestamp, Timedelta, InvalidIndexError, compat, date_range, timedelta_range, DateOffset) from pandas.tseries.offsets import Day from pandas._libs.interval import IntervalTree @@ -555,17 +555,17 @@ def get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): # single queries queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] - expected = [(Int64Index([], dtype='int64'), array([0])) - (Int64Index([0], dtype='int64'), array([])) - (Int64Index([0], dtype='int64'), array([])) - (Int64Index([0, 1], dtype='int64'), array([])) - (Int64Index([0, 1], dtype='int64'), array([])) - (Int64Index([0, 1, 2], dtype='int64'), array([])) - (Int64Index([1, 2], dtype='int64'), array([])) - (Int64Index([2], dtype='int64'), array([])) - (Int64Index([2], dtype='int64'), array([])) - (Int64Index([], dtype='int64'), array([0])) - (Int64Index([], dtype='int64'), array([0]))] + expected = [(Int64Index([], dtype='int64'), np.array([0])) + (Int64Index([0], dtype='int64'), np.array([])) + (Int64Index([0], dtype='int64'), np.array([])) + (Int64Index([0, 1], dtype='int64'), np.array([])) + (Int64Index([0, 1], dtype='int64'), np.array([])) + (Int64Index([0, 1, 2], dtype='int64'), np.array([])) + (Int64Index([1, 2], dtype='int64'), np.array([])) + (Int64Index([2], dtype='int64'), np.array([])) + (Int64Index([2], dtype='int64'), np.array([])) + (Int64Index([], dtype='int64'), np.array([0])) + (Int64Index([], dtype='int64'), np.array([0]))] for query, expected_result in zip(queries, expected): result = index.get_indexer_non_unique([query]) @@ -594,9 +594,9 @@ def test_contains_updated_behavior(self): index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right') # __contains__ requires perfect matches to intervals. - assert 0 not in i - assert 1 not in i - assert 2 not in i + assert 0 not in index + assert 1 not in index + assert 2 not in index assert Interval(0, 1, closed='right') in index assert Interval(0, 2, closed='right') not in index diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index 22b4b55e0e4b9..ffb3d5767e0a9 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -48,7 +48,6 @@ def test_getitem_with_scalar(self): expected = s.iloc[2:5] tm.assert_series_equal(expected, s[s >= 2]) - @pytest.mark.parametrize('direction, closed', product(('increasing', 'decreasing'), ('left', 'right', 'neither', 'both'))) From 4cef0402c250d1c0db46582ceb479f34d5652dd4 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 15 Sep 2017 18:17:19 -0400 Subject: [PATCH 35/56] start working on the review --- pandas/tests/indexes/test_interval.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 6de027091ee2a..84b6722ff3121 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -381,7 +381,7 @@ def test_get_loc_scalar_updated_behavior(self): pytest.raises(KeyError, idx.get_loc, scalar) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def slice_locs_with_interval_updated_behavior(self): + def test_slice_locs_with_interval_updated_behavior(self): # increasing overlapping index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) @@ -439,7 +439,7 @@ def slice_locs_with_interval_updated_behavior(self): 2, 4), end=Interval(0, 2)) == (2, 2) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def slice_locs_with_ints_and_floats_updated_behavior(self): + def test_slice_locs_with_ints_and_floats_updated_behavior(self): queries = [[0, 1], [0, 2], [0, 3], [3, 1], [3, 4], [0, 4]] @@ -490,7 +490,7 @@ def slice_locs_with_ints_and_floats_updated_behavior(self): pytest.raises(InvalidIndexError, index.slice_locs, query) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def get_indexer_for_interval_updated_behavior(self): + def test_get_indexer_for_interval_updated_behavior(self): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='right') @@ -524,7 +524,7 @@ def get_indexer_for_interval_updated_behavior(self): tm.assert_numpy_array_equal(result, expect) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def get_indexer_for_ints_and_floats_updated_behavior(self): + def test_get_indexer_for_ints_and_floats_updated_behavior(self): index = IntervalIndex.from_tuples( [(0, 1), (1, 2), (3, 4)], closed='right') @@ -547,8 +547,10 @@ def get_indexer_for_ints_and_floats_updated_behavior(self): expect = np.array(expected_result, dtype='intp') tm.assert_numpy_array_equal(result, expect) + # what about an overlapping intervalindex? + @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): + def test_get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='left') @@ -607,7 +609,7 @@ def test_contains_updated_behavior(self): assert Interval(0, 1, closed='both') not in index @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def testcontains_updated_behavior(self): + def test_contains_method_updated_behavior(self): index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right') From 66486d0053831b7304eb148ec760d7d8a5367f0d Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 15 Sep 2017 21:48:40 -0400 Subject: [PATCH 36/56] fix some slice-locs stuff --- pandas/tests/indexes/test_interval.py | 86 ++++++++++++--------------- 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 84b6722ff3121..9699ab2ba62e5 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -5,7 +5,7 @@ from datetime import timedelta from pandas import (Interval, IntervalIndex, Index, Int64Index, isna, - interval_range, Timestamp, Timedelta, InvalidIndexError, + interval_range, Timestamp, Timedelta, compat, date_range, timedelta_range, DateOffset) from pandas.tseries.offsets import Day from pandas._libs.interval import IntervalTree @@ -383,60 +383,50 @@ def test_get_loc_scalar_updated_behavior(self): @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_slice_locs_with_interval_updated_behavior(self): - # increasing overlapping + # increasing monotonically index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - assert index.slice_locs(start=Interval( - 0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 3) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) assert index.slice_locs(end=Interval(0, 2)) == (0, 1) - assert index.slice_locs(start=Interval( - 2, 4), end=Interval(0, 2)) == (2, 1) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 1) - # decreasing overlapping + # decreasing monotonically index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) - assert index.slice_locs(start=Interval( - 0, 2), end=Interval(2, 4)) == (2, 1) + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (2, 1) assert index.slice_locs(start=Interval(0, 2)) == (2, 3) assert index.slice_locs(end=Interval(2, 4)) == (0, 1) assert index.slice_locs(end=Interval(0, 2)) == (0, 3) - assert index.slice_locs(start=Interval( - 2, 4), end=Interval(0, 2)) == (0, 3) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (0, 3) # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - assert index.slice_locs(start=Interval( - 0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 3) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) assert index.slice_locs(end=Interval(0, 2)) == (0, 2) - assert index.slice_locs(start=Interval( - 2, 4), end=Interval(0, 2)) == (2, 2) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - pytest.raises(KeyError, index.slice_locs( - start=Interval(0, 2), end=Interval(2, 4))) + pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2), end=Interval(2, 4))) pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2))) assert index.slice_locs(end=Interval(2, 4)) == (0, 2) pytest.raises(KeyError, index.slice_locs(end=Interval(0, 2))) - pytest.raises(KeyError, index.slice_locs( - start=Interval(2, 4), end=Interval(0, 2))) + pytest.raises(KeyError, index.slice_locs(start=Interval(2, 4), end=Interval(0, 2))) - # different unsorted duplicates + # another unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) - assert index.slice_locs(start=Interval( - 0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 4) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) assert index.slice_locs(end=Interval(0, 2)) == (0, 2) - assert index.slice_locs(start=Interval( - 2, 4), end=Interval(0, 2)) == (2, 2) + assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_slice_locs_with_ints_and_floats_updated_behavior(self): @@ -446,48 +436,46 @@ def test_slice_locs_with_ints_and_floats_updated_behavior(self): # increasing non-overlapping index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)]) - assert index.slice_locs(0, 1) == (0) - assert index.slice_locs(0, 2) == (0, 1) - assert index.slice_locs(0, 3) == (0, 1) - assert index.slice_locs(3, 1) == () - assert index.slice_locs(3, 4) == (2) - assert index.slice_locs(0, 4) == (0, 1, 2) + assert index.slice_locs(0, 1) == (0, 1) + assert index.slice_locs(0, 2) == (0, 2) + assert index.slice_locs(0, 3) == (0, 2) + assert index.slice_locs(3, 1) == (2, 1) + assert index.slice_locs(3, 4) == (2, 3) + assert index.slice_locs(0, 4) == (0, 3) + + # decreasing non-overlapping + index = IntervalIndex.from_tuples([(3, 4), (1, 2), (0, 1)]) + assert index.slice_locs(0, 1) == (3, 2) + assert index.slice_locs(0, 2) == (3, 1) + assert index.slice_locs(0, 3) == (3, 1) + assert index.slice_locs(3, 1) == (1, 2) + assert index.slice_locs(3, 4) == (1, 0) + assert index.slice_locs(0, 4) == (3, 0) # increasing overlapping index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) for query in queries: - pytest.raises(InvalidIndexError, index.slice_locs, query) - - # decreasing non-overlapping - index = IntervalIndex.from_tuples([(3, 4), (1, 2), (0, 1)]) - # These were a little mind-bending to write, would appreciate a close - # review - assert index.slice_locs(0, 1) == (2) - assert index.slice_locs(0, 2) == (1, 2) - assert index.slice_locs(0, 3) == (1, 2) - assert index.slice_locs(3, 1) == (0, 1) - assert index.slice_locs(3, 4) == (0) - assert index.slice_locs(0, 4) == (0, 1, 2) + pytest.raises(KeyError, index.slice_locs, query) # decreasing overlapping index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) for query in queries: - pytest.raises(InvalidIndexError, index.slice_locs, query) + pytest.raises(KeyError, index.slice_locs, query) # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) for query in queries: - pytest.raises(InvalidIndexError, index.slice_locs, query) + pytest.raises(KeyError, index.slice_locs, query) # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) for query in queries: - pytest.raises(InvalidIndexError, index.slice_locs, query) + pytest.raises(KeyError, index.slice_locs, query) - # different unsorted duplicates + # another unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) for query in queries: - pytest.raises(InvalidIndexError, index.slice_locs, query) + pytest.raises(KeyError, index.slice_locs, query) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_indexer_for_interval_updated_behavior(self): @@ -547,7 +535,9 @@ def test_get_indexer_for_ints_and_floats_updated_behavior(self): expect = np.array(expected_result, dtype='intp') tm.assert_numpy_array_equal(result, expect) - # what about an overlapping intervalindex? + index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): From c0dfef84b3b19582845fca9a5ac5a504e4ba18da Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 15 Sep 2017 21:59:10 -0400 Subject: [PATCH 37/56] lint issues mostly --- pandas/tests/indexes/test_interval.py | 35 +++++++++++++++++---------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 9699ab2ba62e5..3f1432fd50685 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -386,47 +386,57 @@ def test_slice_locs_with_interval_updated_behavior(self): # increasing monotonically index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs( + start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 3) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) assert index.slice_locs(end=Interval(0, 2)) == (0, 1) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 1) + assert index.slice_locs( + start=Interval(2, 4), end=Interval(0, 2)) == (2, 1) # decreasing monotonically index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (2, 1) + assert index.slice_locs( + start=Interval(0, 2), end=Interval(2, 4)) == (2, 1) assert index.slice_locs(start=Interval(0, 2)) == (2, 3) assert index.slice_locs(end=Interval(2, 4)) == (0, 1) assert index.slice_locs(end=Interval(0, 2)) == (0, 3) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (0, 3) + assert index.slice_locs( + start=Interval(2, 4), end=Interval(0, 2)) == (0, 3) # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs( + start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 3) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) assert index.slice_locs(end=Interval(0, 2)) == (0, 2) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) + assert index.slice_locs( + start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2), end=Interval(2, 4))) + pytest.raises(KeyError, index.slice_locs( + start=Interval(0, 2), end=Interval(2, 4))) pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2))) assert index.slice_locs(end=Interval(2, 4)) == (0, 2) pytest.raises(KeyError, index.slice_locs(end=Interval(0, 2))) - pytest.raises(KeyError, index.slice_locs(start=Interval(2, 4), end=Interval(0, 2))) + pytest.raises(KeyError, index.slice_locs( + start=Interval(2, 4), end=Interval(0, 2))) # another unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) - assert index.slice_locs(start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) + assert index.slice_locs( + start=Interval(0, 2), end=Interval(2, 4)) == (0, 3) assert index.slice_locs(start=Interval(0, 2)) == (0, 4) assert index.slice_locs(end=Interval(2, 4)) == (0, 3) assert index.slice_locs(end=Interval(0, 2)) == (0, 2) - assert index.slice_locs(start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) + assert index.slice_locs( + start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_slice_locs_with_ints_and_floats_updated_behavior(self): @@ -536,8 +546,7 @@ def test_get_indexer_for_ints_and_floats_updated_behavior(self): tm.assert_numpy_array_equal(result, expect) index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - - + # TODO: @shoyer believes this should raise, master branch doesn't @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): @@ -577,7 +586,7 @@ def test_get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): result = index.get_indexer_non_unique(query) tm.assert_numpy_array_equal(result, expected_result) - # we may also want to test get_indexer for the case when + # TODO we may also want to test get_indexer for the case when # the intervals are duplicated, decreasing, non-monotonic, etc.. @pytest.mark.xfail(reason="new indexing tests for issue 16316") From 5301dd5eab922dbdbb2a4a8d5f335358234ddabc Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Sat, 16 Sep 2017 10:32:40 -0400 Subject: [PATCH 38/56] try to satisfy pep --- pandas/tests/indexes/test_interval.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 3f1432fd50685..f7a7edc12b36f 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -365,9 +365,9 @@ def test_get_loc_scalar_updated_behavior(self): scalars = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5] correct = {'right': {0.5: 0, 1: 0, 2.5: 1, 3: 1}, - 'left': {0: 0, 0.5: 0, 2: 1, 2.5: 1}, - 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1}, - 'neither': {0.5: 0, 2.5: 1}} + 'left': {0: 0, 0.5: 0, 2: 1, 2.5: 1}, + 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1}, + 'neither': {0.5: 0, 2.5: 1}} for idx_side in ['right', 'left', 'both', 'neither']: From 57c9ba792f98a6c0f5d2c8b66923c748fedb7307 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Sat, 16 Sep 2017 10:35:21 -0400 Subject: [PATCH 39/56] for some reason that didn't make PEP happy --- pandas/tests/indexes/test_interval.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index f7a7edc12b36f..17bab58d8c75d 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -365,9 +365,10 @@ def test_get_loc_scalar_updated_behavior(self): scalars = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5] correct = {'right': {0.5: 0, 1: 0, 2.5: 1, 3: 1}, - 'left': {0: 0, 0.5: 0, 2: 1, 2.5: 1}, - 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1}, - 'neither': {0.5: 0, 2.5: 1}} + 'left': {0: 0, 0.5: 0, 2: 1, 2.5: 1}, + 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1}, + 'neither': {0.5: 0, 2.5: 1}} + for idx_side in ['right', 'left', 'both', 'neither']: From 4cbbf2d4f9e9bff6f0ef6c003b96cf0b5b4d1b80 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Sat, 16 Sep 2017 12:19:14 -0400 Subject: [PATCH 40/56] pep --- pandas/tests/indexes/test_interval.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 17bab58d8c75d..fc6f0257457b6 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -369,7 +369,6 @@ def test_get_loc_scalar_updated_behavior(self): 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1}, 'neither': {0.5: 0, 2.5: 1}} - for idx_side in ['right', 'left', 'both', 'neither']: idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) From 6126662b9b591ab6d031d6441fe500a333be1615 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Mon, 25 Sep 2017 12:52:55 -0400 Subject: [PATCH 41/56] parameterize and add a couple comments. --- pandas/tests/indexes/test_interval.py | 52 +++++++++++++-------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index fc6f0257457b6..0bcbe6a456b0d 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -336,31 +336,30 @@ def test_get_item(self): tm.assert_index_equal(result, expected) @pytest.mark.xfail(reason="new indexing tests for issue 16316") + @pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither']) def test_get_loc_interval_updated_behavior(self): - for idx_side in ['right', 'left', 'both', 'neither']: - - idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) - - for bound in [[0, 1], [1, 2], [2, 3], [3, 4], - [0, 2], [2.5, 3], [-1, 4]]: - for side in ['right', 'left', 'both', 'neither']: - - if idx_side == side: - if bound == [0, 1]: - assert idx.get_loc( - Interval(0, 1, closed=side)) == 0 - elif bound == [2, 3]: - assert idx.get_loc( - Interval(2, 3, closed=side)) == 1 - else: - pytest.raises(KeyError, idx.get_loc, - Interval(*bound, closed=side)) + idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) + + for bound in [[0, 1], [1, 2], [2, 3], [3, 4], + [0, 2], [2.5, 3], [-1, 4]]: + for side in ['right', 'left', 'both', 'neither']: + # if get_loc is supplied an interval, it should only search + # for exact matches, not overlaps or covers, else KeyError. + if idx_side == side: + if bound == [0, 1]: + assert idx.get_loc(Interval(0, 1, closed=side)) == 0 + elif bound == [2, 3]: + assert idx.get_loc(Interval(2, 3, closed=side)) == 1 else: pytest.raises(KeyError, idx.get_loc, Interval(*bound, closed=side)) + else: + pytest.raises(KeyError, idx.get_loc, + Interval(*bound, closed=side)) @pytest.mark.xfail(reason="new indexing tests for issue 16316") + @pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither']) def test_get_loc_scalar_updated_behavior(self): scalars = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5] @@ -369,16 +368,15 @@ def test_get_loc_scalar_updated_behavior(self): 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1}, 'neither': {0.5: 0, 2.5: 1}} - for idx_side in ['right', 'left', 'both', 'neither']: + idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) - idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) - - for scalar in scalars: - - if scalar in correct[idx_side].keys(): - assert idx.get_loc(scalar) == correct[idx_side][scalar] - else: - pytest.raises(KeyError, idx.get_loc, scalar) + for scalar in scalars: + # if get_loc is supplied a scalar, it should return the index of + # the interval which contains the scalar, or KeyError. + if scalar in correct[idx_side].keys(): + assert idx.get_loc(scalar) == correct[idx_side][scalar] + else: + pytest.raises(KeyError, idx.get_loc, scalar) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_slice_locs_with_interval_updated_behavior(self): From dc00af6487fdca01e855391dd9977c3bc6bef255 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 26 Sep 2017 07:53:34 -0400 Subject: [PATCH 42/56] hopefully this works. --- pandas/tests/api/test_api.py | 2 +- pandas/tests/dtypes/test_common.py | 10 +- pandas/tests/dtypes/test_dtypes.py | 141 ++++- pandas/tests/frame/test_analytics.py | 11 +- pandas/tests/frame/test_api.py | 4 +- .../tests/frame/test_axis_select_reindex.py | 35 ++ pandas/tests/frame/test_block_internals.py | 12 +- pandas/tests/frame/test_constructors.py | 5 +- pandas/tests/frame/test_mutate_columns.py | 16 +- pandas/tests/frame/test_operators.py | 33 + pandas/tests/frame/test_timeseries.py | 5 + pandas/tests/groupby/test_timegrouper.py | 13 + pandas/tests/indexes/common.py | 332 +++++----- pandas/tests/indexes/conftest.py | 24 + pandas/tests/indexes/datetimelike.py | 4 +- .../indexes/datetimes/test_date_range.py | 2 +- pandas/tests/indexes/datetimes/test_setops.py | 4 +- pandas/tests/indexes/datetimes/test_tools.py | 7 + pandas/tests/indexes/test_base.py | 4 +- pandas/tests/indexes/test_category.py | 10 +- pandas/tests/indexes/test_interval.py | 574 +++++++++--------- pandas/tests/indexes/test_interval_new.py | 315 ++++++++++ pandas/tests/indexes/test_multi.py | 83 ++- pandas/tests/indexes/test_numeric.py | 4 +- pandas/tests/indexes/test_range.py | 25 +- pandas/tests/indexing/test_interval.py | 113 ++-- pandas/tests/indexing/test_interval_new.py | 123 ++++ pandas/tests/internals/test_internals.py | 7 +- pandas/tests/io/formats/test_to_html.py | 8 +- .../tests/io/json/test_json_table_schema.py | 5 +- pandas/tests/io/parser/common.py | 14 - pandas/tests/io/parser/dtypes.py | 9 +- pandas/tests/io/parser/mangle_dupes.py | 46 +- pandas/tests/io/test_parquet.py | 46 +- pandas/tests/io/test_pytables.py | 12 +- pandas/tests/io/test_sql.py | 4 +- pandas/tests/io/test_stata.py | 352 ++++++----- pandas/tests/plotting/test_datetimelike.py | 120 +++- pandas/tests/plotting/test_frame.py | 53 +- pandas/tests/plotting/test_series.py | 44 +- pandas/tests/reshape/test_merge.py | 4 +- pandas/tests/scalar/test_period.py | 12 +- pandas/tests/scalar/test_timedelta.py | 21 +- pandas/tests/scalar/test_timestamp.py | 4 +- pandas/tests/series/test_analytics.py | 11 +- pandas/tests/series/test_constructors.py | 21 + pandas/tests/series/test_dtypes.py | 34 +- pandas/tests/series/test_indexing.py | 2 +- pandas/tests/sparse/test_frame.py | 5 +- pandas/tests/test_algos.py | 72 +-- pandas/tests/test_base.py | 5 +- pandas/tests/test_categorical.py | 235 ++++++- pandas/tests/test_resample.py | 28 +- pandas/tests/test_window.py | 2 +- pandas/tests/tseries/test_offsets.py | 3 +- pandas/tests/tseries/test_timezones.py | 43 +- pandas/tests/util/test_util.py | 16 +- 57 files changed, 2225 insertions(+), 924 deletions(-) create mode 100644 pandas/tests/indexes/conftest.py create mode 100644 pandas/tests/indexes/test_interval_new.py create mode 100644 pandas/tests/indexing/test_interval_new.py diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index 09cccd54b74f8..cbc73615811a2 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -63,7 +63,7 @@ class TestPDApi(Base): # top-level functions funcs = ['bdate_range', 'concat', 'crosstab', 'cut', 'date_range', 'interval_range', 'eval', - 'factorize', 'get_dummies', + 'factorize', 'get_dummies', 'cdate_range', 'infer_freq', 'isna', 'isnull', 'lreshape', 'melt', 'notna', 'notnull', 'offsets', 'merge', 'merge_ordered', 'merge_asof', diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 8a36f234484b4..e0be34b14a97d 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -545,10 +545,12 @@ def test_is_complex_dtype(): (pd.Index([1, 2]), np.dtype('int64')), (pd.Index(['a', 'b']), np.dtype(object)), ('category', 'category'), - (pd.Categorical(['a', 'b']).dtype, CategoricalDtype()), - (pd.Categorical(['a', 'b']), CategoricalDtype()), - (pd.CategoricalIndex(['a', 'b']).dtype, CategoricalDtype()), - (pd.CategoricalIndex(['a', 'b']), CategoricalDtype()), + (pd.Categorical(['a', 'b']).dtype, CategoricalDtype(['a', 'b'])), + (pd.Categorical(['a', 'b']), CategoricalDtype(['a', 'b'])), + (pd.CategoricalIndex(['a', 'b']).dtype, CategoricalDtype(['a', 'b'])), + (pd.CategoricalIndex(['a', 'b']), CategoricalDtype(['a', 'b'])), + (CategoricalDtype(), CategoricalDtype()), + (CategoricalDtype(['a', 'b']), CategoricalDtype()), (pd.DatetimeIndex([1, 2]), np.dtype('= 2]) + # TODO: check this behavior is consistent def test_getitem_with_scalar(self): s = self.s @@ -48,6 +50,7 @@ def test_getitem_with_scalar(self): expected = s.iloc[2:5] tm.assert_series_equal(expected, s[s >= 2]) + # TODO: check this behavior is consistent @pytest.mark.parametrize('direction, closed', product(('increasing', 'decreasing'), ('left', 'right', 'neither', 'both'))) @@ -83,6 +86,7 @@ def test_nonoverlapping_monotonic(self, direction, closed): assert s[key] == expected assert s.loc[key] == expected + # TODO: check this behavior is consistent def test_with_interval(self): s = self.s @@ -94,6 +98,18 @@ def test_with_interval(self): result = s[Interval(0, 1)] assert result == expected + expected = s.iloc[3:5] + result = s.loc[Interval(3, 6)] + tm.assert_series_equal(expected, result) + + expected = s.iloc[3:5] + result = s.loc[[Interval(3, 6)]] + tm.assert_series_equal(expected, result) + + expected = s.iloc[3:5] + result = s.loc[[Interval(3, 5)]] + tm.assert_series_equal(expected, result) + # missing with pytest.raises(KeyError): s.loc[Interval(-2, 0)] @@ -107,88 +123,69 @@ def test_with_interval(self): with pytest.raises(KeyError): s[Interval(5, 6)] - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_with_slices_updated_behavior(self): + # To be removed (see #16316, #16386) + def test_with_slices(self): s = self.s # slice of interval - expected = s.iloc[4:] - result = s.loc[Interval(3, 4):] - tm.assert_series_equal(expected, result) - - expected = s.iloc[4:] - result = s[Interval(3, 4):] - tm.assert_series_equal(expected, result) - - with pytest.raises(KeyError): - s.loc[Interval(3, 6):] - - with pytest.raises(KeyError): - s[Interval(3, 6):] - - with pytest.raises(KeyError): - s.loc[Interval(3, 4, closed='left'):] - - with pytest.raises(KeyError): - s[Interval(3, 4, closed='left'):] - - with pytest.raises(KeyError): - s.loc[Interval(3, 4, closed='both'):] - with pytest.raises(NotImplementedError): s.loc[Interval(3, 6):] with pytest.raises(NotImplementedError): s[Interval(3, 6):] - with pytest.raises(KeyError): - s[Interval(3, 4, closed='both'):] - - # slice of scalar - expected = s.iloc[:4] # maybe [:5] ? - result = s[0:4] + expected = s.iloc[3:5] + result = s[[Interval(3, 6)]] tm.assert_series_equal(expected, result) # slice of scalar with step != 1 - with pytest.raises(NotImplementedError): + with pytest.raises(ValueError): s[0:4:2] - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_with_overlaps_updated_behavior(self): + # To be removed (see #16316, #16386) + def test_with_overlaps(self): + + s = self.s + expected = s.iloc[[3, 4, 3, 4]] + result = s.loc[[Interval(3, 6), Interval(3, 6)]] + tm.assert_series_equal(expected, result) idx = IntervalIndex.from_tuples([(1, 5), (3, 7)]) s = Series(range(len(idx)), index=idx) - # scalar - expected = s result = s[4] + expected = s tm.assert_series_equal(expected, result) - expected = s result = s[[4]] + expected = s tm.assert_series_equal(expected, result) - expected = s result = s.loc[[4]] + expected = s tm.assert_series_equal(expected, result) - # interval - with pytest.raises(KeyError): - s[Interval(3, 5)] + result = s[Interval(3, 5)] + expected = s + tm.assert_series_equal(expected, result) - with pytest.raises(KeyError): - s[[Interval(3, 5)]] + result = s.loc[Interval(3, 5)] + expected = s + tm.assert_series_equal(expected, result) + # doesn't intersect unique set of intervals with pytest.raises(KeyError): - s.loc[Interval(3, 5)] + s[[Interval(3, 5)]] with pytest.raises(KeyError): s.loc[[Interval(3, 5)]] + # To be removed (see #16316, #16386) def test_non_unique(self): idx = IntervalIndex.from_tuples([(1, 3), (3, 7)]) + s = pd.Series(range(len(idx)), index=idx) result = s.loc[Interval(1, 3)] @@ -198,33 +195,33 @@ def test_non_unique(self): expected = s.iloc[0:1] tm.assert_series_equal(expected, result) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_non_unique_moar_updated_behavior(self): + # To be removed (see #16316, #16386) + def test_non_unique_moar(self): idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) s = Series(range(len(idx)), index=idx) - expected = s.iloc[[0, 1]] result = s.loc[Interval(1, 3)] + expected = s.iloc[[0, 1]] tm.assert_series_equal(expected, result) - expected = s - result = s.loc[Interval(1, 3):] - tm.assert_series_equal(expected, result) + # non-unique index and slices not allowed + with pytest.raises(ValueError): + s.loc[Interval(1, 3):] - expected = s - result = s[Interval(1, 3):] - tm.assert_series_equal(expected, result) + with pytest.raises(ValueError): + s[Interval(1, 3):] - expected = s.iloc[[0, 1]] - result = s[[Interval(1, 3)]] - tm.assert_series_equal(expected, result) + # non-unique + with pytest.raises(ValueError): + s[[Interval(1, 3)]] + # TODO: check this behavior is consistent def test_non_matching(self): - s = self.s - # this is a departure from our current indexing scheme, but simpler + # this is a departure from our current + # indexin scheme, but simpler with pytest.raises(KeyError): s.loc[[-1, 3, 4, 5]] diff --git a/pandas/tests/indexing/test_interval_new.py b/pandas/tests/indexing/test_interval_new.py new file mode 100644 index 0000000000000..75f9a767587ed --- /dev/null +++ b/pandas/tests/indexing/test_interval_new.py @@ -0,0 +1,123 @@ +import pytest +import numpy as np +import pandas as pd + +from pandas import Series, DataFrame, IntervalIndex, Interval +from pandas.compat import product +import pandas.util.testing as tm + + +class TestIntervalIndex_new(object): + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_with_slices_updated_behavior(self): + + s = self.s + + # slice of interval + expected = s.iloc[4:] + result = s.loc[Interval(3, 4):] + tm.assert_series_equal(expected, result) + + expected = s.iloc[4:] + result = s[Interval(3, 4):] + tm.assert_series_equal(expected, result) + + with pytest.raises(KeyError): + s.loc[Interval(3, 6):] + + with pytest.raises(KeyError): + s[Interval(3, 6):] + + with pytest.raises(KeyError): + s.loc[Interval(3, 4, closed='left'):] + + with pytest.raises(KeyError): + s[Interval(3, 4, closed='left'):] + + with pytest.raises(KeyError): + s.loc[Interval(3, 4, closed='both'):] + + with pytest.raises(NotImplementedError): + s.loc[Interval(3, 6):] + + with pytest.raises(NotImplementedError): + s[Interval(3, 6):] + + with pytest.raises(KeyError): + s[Interval(3, 4, closed='both'):] + + # slice of scalar + expected = s.iloc[:4] # maybe [:5] ? + result = s[0:4] + tm.assert_series_equal(expected, result) + + # slice of scalar with step != 1 + with pytest.raises(NotImplementedError): + s[0:4:2] + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_with_overlaps_updated_behavior(self): + + idx = IntervalIndex.from_tuples([(1, 5), (3, 7)]) + s = Series(range(len(idx)), index=idx) + + # scalar + expected = s + result = s[4] + tm.assert_series_equal(expected, result) + + expected = s + result = s[[4]] + tm.assert_series_equal(expected, result) + + expected = s + result = s.loc[[4]] + tm.assert_series_equal(expected, result) + + # interval + with pytest.raises(KeyError): + s[Interval(3, 5)] + + with pytest.raises(KeyError): + s[[Interval(3, 5)]] + + with pytest.raises(KeyError): + s.loc[Interval(3, 5)] + + with pytest.raises(KeyError): + s.loc[[Interval(3, 5)]] + + def test_non_unique(self): + + idx = IntervalIndex.from_tuples([(1, 3), (3, 7)]) + s = pd.Series(range(len(idx)), index=idx) + + result = s.loc[Interval(1, 3)] + assert result == 0 + + result = s.loc[[Interval(1, 3)]] + expected = s.iloc[0:1] + tm.assert_series_equal(expected, result) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_non_unique_moar_updated_behavior(self): + + idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) + s = Series(range(len(idx)), index=idx) + + expected = s.iloc[[0, 1]] + result = s.loc[Interval(1, 3)] + tm.assert_series_equal(expected, result) + + expected = s + result = s.loc[Interval(1, 3):] + tm.assert_series_equal(expected, result) + + expected = s + result = s[Interval(1, 3):] + tm.assert_series_equal(expected, result) + + expected = s.iloc[[0, 1]] + result = s[[Interval(1, 3)]] + tm.assert_series_equal(expected, result) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 0900d21b250ed..f40fc151676da 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -469,10 +469,11 @@ def test_set_change_dtype_slice(self): # GH8850 df = DataFrame([[1.0, 2, 3], [4.0, 5, 6]], columns=cols) df['2nd'] = df['2nd'] * 2.0 - assert sorted(df.blocks.keys()) == ['float64', 'int64'] - assert_frame_equal(df.blocks['float64'], DataFrame( + blocks = df._to_dict_of_blocks() + assert sorted(blocks.keys()) == ['float64', 'int64'] + assert_frame_equal(blocks['float64'], DataFrame( [[1.0, 4.0], [4.0, 10.0]], columns=cols[:2])) - assert_frame_equal(df.blocks['int64'], DataFrame( + assert_frame_equal(blocks['int64'], DataFrame( [[3], [6]], columns=cols[2:])) def test_copy(self, mgr): diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 1e174c34221d5..194b5ba3e0276 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -1868,12 +1868,16 @@ def test_to_html_no_index_max_rows(self): def test_to_html_notebook_has_style(self): df = pd.DataFrame({"A": [1, 2, 3]}) result = df.to_html(notebook=True) - assert "thead tr:only-child" in result + assert "tbody tr th:only-of-type" in result + assert "vertical-align: middle;" in result + assert "thead th" in result def test_to_html_notebook_has_no_style(self): df = pd.DataFrame({"A": [1, 2, 3]}) result = df.to_html() - assert "thead tr:only-child" not in result + assert "tbody tr th:only-of-type" not in result + assert "vertical-align: middle;" not in result + assert "thead th" not in result def test_to_html_with_index_names_false(self): # gh-16493 diff --git a/pandas/tests/io/json/test_json_table_schema.py b/pandas/tests/io/json/test_json_table_schema.py index e097194674cf6..dab56e264b955 100644 --- a/pandas/tests/io/json/test_json_table_schema.py +++ b/pandas/tests/io/json/test_json_table_schema.py @@ -164,7 +164,10 @@ def test_as_json_table_type_string_dtypes(self): assert as_json_table_type(t) == 'string' def test_as_json_table_type_categorical_dtypes(self): - assert as_json_table_type(pd.Categorical) == 'any' + # TODO: I think before is_categorical_dtype(Categorical) + # returned True, but now it's False. Figure out why or + # if it matters + assert as_json_table_type(pd.Categorical(['a'])) == 'any' assert as_json_table_type(CategoricalDtype()) == 'any' diff --git a/pandas/tests/io/parser/common.py b/pandas/tests/io/parser/common.py index cfc4a1d7c55eb..e85d3ad294655 100644 --- a/pandas/tests/io/parser/common.py +++ b/pandas/tests/io/parser/common.py @@ -1357,20 +1357,6 @@ def test_euro_decimal_format(self): assert df2['Number2'].dtype == float assert df2['Number3'].dtype == float - def test_read_duplicate_names(self): - # See gh-7160 - data = "a,b,a\n0,1,2\n3,4,5" - df = self.read_csv(StringIO(data)) - expected = DataFrame([[0, 1, 2], [3, 4, 5]], - columns=['a', 'b', 'a.1']) - tm.assert_frame_equal(df, expected) - - data = "0,1,2\n3,4,5" - df = self.read_csv(StringIO(data), names=["a", "b", "a"]) - expected = DataFrame([[0, 1, 2], [3, 4, 5]], - columns=['a', 'b', 'a.1']) - tm.assert_frame_equal(df, expected) - def test_inf_parsing(self): data = """\ ,A diff --git a/pandas/tests/io/parser/dtypes.py b/pandas/tests/io/parser/dtypes.py index 7311c9200f269..402fa0817595c 100644 --- a/pandas/tests/io/parser/dtypes.py +++ b/pandas/tests/io/parser/dtypes.py @@ -204,10 +204,11 @@ def test_empty_with_dup_column_pass_dtype_by_indexes(self): result = self.read_csv(StringIO(data), dtype={0: 'u1', 1: 'f'}) tm.assert_frame_equal(result, expected, check_index_type=False) - data = '' - result = self.read_csv(StringIO(data), names=['one', 'one'], - dtype={0: 'u1', 1: 'f'}) - tm.assert_frame_equal(result, expected, check_index_type=False) + with tm.assert_produces_warning(UserWarning, check_stacklevel=False): + data = '' + result = self.read_csv(StringIO(data), names=['one', 'one'], + dtype={0: 'u1', 1: 'f'}) + tm.assert_frame_equal(result, expected, check_index_type=False) def test_raise_on_passed_int_dtype_with_nas(self): # see gh-2631 diff --git a/pandas/tests/io/parser/mangle_dupes.py b/pandas/tests/io/parser/mangle_dupes.py index e2efb1377f8b0..6df69eb475bf7 100644 --- a/pandas/tests/io/parser/mangle_dupes.py +++ b/pandas/tests/io/parser/mangle_dupes.py @@ -7,6 +7,9 @@ """ from pandas.compat import StringIO +from pandas import DataFrame + +import pandas.util.testing as tm class DupeColumnTests(object): @@ -25,6 +28,21 @@ def test_basic(self): mangle_dupe_cols=True) assert list(df.columns) == expected + def test_basic_names(self): + # See gh-7160 + data = "a,b,a\n0,1,2\n3,4,5" + expected = DataFrame([[0, 1, 2], [3, 4, 5]], + columns=["a", "b", "a.1"]) + + df = self.read_csv(StringIO(data)) + tm.assert_frame_equal(df, expected) + + with tm.assert_produces_warning(UserWarning, check_stacklevel=False): + data = "0,1,2\n3,4,5" + df = self.read_csv(StringIO(data), + names=["a", "b", "a"]) + tm.assert_frame_equal(df, expected) + def test_thorough_mangle_columns(self): # see gh-17060 data = "a,a,a.1\n1,2,3" @@ -45,20 +63,26 @@ def test_thorough_mangle_names(self): # see gh-17095 data = "a,b,b\n1,2,3" names = ["a.1", "a.1", "a.1.1"] - df = self.read_csv(StringIO(data), sep=",", names=names, - mangle_dupe_cols=True) - assert list(df.columns) == ["a.1", "a.1.1", "a.1.1.1"] + + with tm.assert_produces_warning(UserWarning, check_stacklevel=False): + df = self.read_csv(StringIO(data), sep=",", names=names, + mangle_dupe_cols=True) + assert list(df.columns) == ["a.1", "a.1.1", "a.1.1.1"] data = "a,b,c,d,e,f\n1,2,3,4,5,6" names = ["a", "a", "a.1", "a.1.1", "a.1.1.1", "a.1.1.1.1"] - df = self.read_csv(StringIO(data), sep=",", names=names, - mangle_dupe_cols=True) - assert list(df.columns) == ["a", "a.1", "a.1.1", "a.1.1.1", - "a.1.1.1.1", "a.1.1.1.1.1"] + + with tm.assert_produces_warning(UserWarning, check_stacklevel=False): + df = self.read_csv(StringIO(data), sep=",", names=names, + mangle_dupe_cols=True) + assert list(df.columns) == ["a", "a.1", "a.1.1", "a.1.1.1", + "a.1.1.1.1", "a.1.1.1.1.1"] data = "a,b,c,d,e,f,g\n1,2,3,4,5,6,7" names = ["a", "a", "a.3", "a.1", "a.2", "a", "a"] - df = self.read_csv(StringIO(data), sep=",", names=names, - mangle_dupe_cols=True) - assert list(df.columns) == ["a", "a.1", "a.3", "a.1.1", - "a.2", "a.2.1", "a.3.1"] + + with tm.assert_produces_warning(UserWarning, check_stacklevel=False): + df = self.read_csv(StringIO(data), sep=",", names=names, + mangle_dupe_cols=True) + assert list(df.columns) == ["a", "a.1", "a.3", "a.1.1", + "a.2", "a.2.1", "a.3.1"] diff --git a/pandas/tests/io/test_parquet.py b/pandas/tests/io/test_parquet.py index 78c72e2a05566..ecd4e8f719014 100644 --- a/pandas/tests/io/test_parquet.py +++ b/pandas/tests/io/test_parquet.py @@ -2,11 +2,12 @@ import pytest import datetime +from distutils.version import LooseVersion from warnings import catch_warnings import numpy as np import pandas as pd -from pandas.compat import PY3, is_platform_windows +from pandas.compat import PY3 from pandas.io.parquet import (to_parquet, read_parquet, get_engine, PyArrowImpl, FastParquetImpl) from pandas.util import testing as tm @@ -42,8 +43,24 @@ def engine(request): def pa(): if not _HAVE_PYARROW: pytest.skip("pyarrow is not installed") - if is_platform_windows(): - pytest.skip("pyarrow-parquet not building on windows") + return 'pyarrow' + + +@pytest.fixture +def pa_lt_070(): + if not _HAVE_PYARROW: + pytest.skip("pyarrow is not installed") + if LooseVersion(pyarrow.__version__) >= '0.7.0': + pytest.skip("pyarrow is >= 0.7.0") + return 'pyarrow' + + +@pytest.fixture +def pa_ge_070(): + if not _HAVE_PYARROW: + pytest.skip("pyarrow is not installed") + if LooseVersion(pyarrow.__version__) < '0.7.0': + pytest.skip("pyarrow is < 0.7.0") return 'pyarrow' @@ -302,10 +319,6 @@ def test_unsupported(self, pa): df = pd.DataFrame({'a': pd.period_range('2013', freq='M', periods=3)}) self.check_error_on_write(df, pa, ValueError) - # categorical - df = pd.DataFrame({'a': pd.Categorical(list('abc'))}) - self.check_error_on_write(df, pa, NotImplementedError) - # timedelta df = pd.DataFrame({'a': pd.timedelta_range('1 day', periods=3)}) @@ -315,6 +328,23 @@ def test_unsupported(self, pa): df = pd.DataFrame({'a': ['a', 1, 2.0]}) self.check_error_on_write(df, pa, ValueError) + def test_categorical(self, pa_ge_070): + pa = pa_ge_070 + + # supported in >= 0.7.0 + df = pd.DataFrame({'a': pd.Categorical(list('abc'))}) + + # de-serialized as object + expected = df.assign(a=df.a.astype(object)) + self.check_round_trip(df, pa, expected) + + def test_categorical_unsupported(self, pa_lt_070): + pa = pa_lt_070 + + # supported in >= 0.7.0 + df = pd.DataFrame({'a': pd.Categorical(list('abc'))}) + self.check_error_on_write(df, pa, NotImplementedError) + class TestParquetFastParquet(Base): @@ -364,6 +394,8 @@ def test_unsupported(self, fp): self.check_error_on_write(df, fp, ValueError) def test_categorical(self, fp): + if LooseVersion(fastparquet.__version__) < LooseVersion("0.1.3"): + pytest.skip("CategoricalDtype not supported for older fp") df = pd.DataFrame({'a': pd.Categorical(list('abc'))}) self.check_round_trip(df, fp, compression=None) diff --git a/pandas/tests/io/test_pytables.py b/pandas/tests/io/test_pytables.py index f331378b654be..ff21afc11d220 100644 --- a/pandas/tests/io/test_pytables.py +++ b/pandas/tests/io/test_pytables.py @@ -18,6 +18,7 @@ from pandas.compat import is_platform_windows, PY3, PY35, BytesIO, text_type from pandas.io.formats.printing import pprint_thing +from pandas.core.dtypes.common import is_categorical_dtype tables = pytest.importorskip('tables') from pandas.io.pytables import TableIterator @@ -1090,7 +1091,12 @@ def roundtrip(s, key='data', encoding='latin-1', nan_rep=''): nan_rep=nan_rep) retr = read_hdf(store, key) s_nan = s.replace(nan_rep, np.nan) - assert_series_equal(s_nan, retr, check_categorical=False) + if is_categorical_dtype(s_nan): + assert is_categorical_dtype(retr) + assert_series_equal(s_nan, retr, check_dtype=False, + check_categorical=False) + else: + assert_series_equal(s_nan, retr) for s in examples: roundtrip(s) @@ -4845,7 +4851,7 @@ def test_categorical(self): # Make sure the metadata is OK info = store.info() assert '/df2 ' in info - assert '/df2/meta/values_block_0/meta' in info + # assert '/df2/meta/values_block_0/meta' in info assert '/df2/meta/values_block_1/meta' in info # unordered @@ -5421,7 +5427,7 @@ def test_append_with_timezones_dateutil(self): # use maybe_get_tz instead of dateutil.tz.gettz to handle the windows # filename issues. - from pandas._libs.tslib import maybe_get_tz + from pandas._libs.tslibs.timezones import maybe_get_tz gettz = lambda x: maybe_get_tz('dateutil/' + x) # as columns diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index 93eb0ff0ac1f2..2df43158b5370 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -2131,7 +2131,7 @@ def test_write_row_by_row(self): result = sql.read_sql("select * from test", con=self.conn) result.index = frame.index - tm.assert_frame_equal(result, frame) + tm.assert_frame_equal(result, frame, check_less_precise=True) def test_execute(self): frame = tm.makeTimeDataFrame() @@ -2416,7 +2416,7 @@ def test_write_row_by_row(self): result = sql.read_sql("select * from test", con=self.conn) result.index = frame.index - tm.assert_frame_equal(result, frame) + tm.assert_frame_equal(result, frame, check_less_precise=True) def test_chunksize_read_type(self): _skip_if_no_pymysql() diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py index a414928d318c4..055a490bc6b5d 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -23,6 +23,19 @@ PossiblePrecisionLoss, StataMissingValue) +@pytest.fixture +def dirpath(): + return tm.get_data_path() + + +@pytest.fixture +def parsed_114(dirpath): + dta14_114 = os.path.join(dirpath, 'stata5_114.dta') + parsed_114 = read_stata(dta14_114, convert_dates=True) + parsed_114.index.name = 'index' + return parsed_114 + + class TestStata(object): def setup_method(self, method): @@ -108,10 +121,12 @@ def test_data_method(self): parsed_114_read = rdr.read() tm.assert_frame_equal(parsed_114_data, parsed_114_read) - def test_read_dta1(self): + @pytest.mark.parametrize( + 'file', ['dta1_114', 'dta1_117']) + def test_read_dta1(self, file): - parsed_114 = self.read_dta(self.dta1_114) - parsed_117 = self.read_dta(self.dta1_117) + file = getattr(self, file) + parsed = self.read_dta(file) # Pandas uses np.nan as missing value. # Thus, all columns will be of type float, regardless of their name. @@ -123,8 +138,7 @@ def test_read_dta1(self): # the casting doesn't fail so need to match stata here expected['float_miss'] = expected['float_miss'].astype(np.float32) - tm.assert_frame_equal(parsed_114, expected) - tm.assert_frame_equal(parsed_117, expected) + tm.assert_frame_equal(parsed, expected) def test_read_dta2(self): if LooseVersion(sys.version) < '2.7': @@ -193,11 +207,12 @@ def test_read_dta2(self): tm.assert_frame_equal(parsed_117, expected, check_datetimelike_compat=True) - def test_read_dta3(self): - parsed_113 = self.read_dta(self.dta3_113) - parsed_114 = self.read_dta(self.dta3_114) - parsed_115 = self.read_dta(self.dta3_115) - parsed_117 = self.read_dta(self.dta3_117) + @pytest.mark.parametrize( + 'file', ['dta3_113', 'dta3_114', 'dta3_115', 'dta3_117']) + def test_read_dta3(self, file): + + file = getattr(self, file) + parsed = self.read_dta(file) # match stata here expected = self.read_csv(self.csv3) @@ -205,16 +220,14 @@ def test_read_dta3(self): expected['year'] = expected['year'].astype(np.int16) expected['quarter'] = expected['quarter'].astype(np.int8) - tm.assert_frame_equal(parsed_113, expected) - tm.assert_frame_equal(parsed_114, expected) - tm.assert_frame_equal(parsed_115, expected) - tm.assert_frame_equal(parsed_117, expected) + tm.assert_frame_equal(parsed, expected) + + @pytest.mark.parametrize( + 'file', ['dta4_113', 'dta4_114', 'dta4_115', 'dta4_117']) + def test_read_dta4(self, file): - def test_read_dta4(self): - parsed_113 = self.read_dta(self.dta4_113) - parsed_114 = self.read_dta(self.dta4_114) - parsed_115 = self.read_dta(self.dta4_115) - parsed_117 = self.read_dta(self.dta4_117) + file = getattr(self, file) + parsed = self.read_dta(file) expected = DataFrame.from_records( [ @@ -237,10 +250,7 @@ def test_read_dta4(self): for col in expected], axis=1) # stata doesn't save .category metadata - tm.assert_frame_equal(parsed_113, expected, check_categorical=False) - tm.assert_frame_equal(parsed_114, expected, check_categorical=False) - tm.assert_frame_equal(parsed_115, expected, check_categorical=False) - tm.assert_frame_equal(parsed_117, expected, check_categorical=False) + tm.assert_frame_equal(parsed, expected, check_categorical=False) # File containing strls def test_read_dta12(self): @@ -427,7 +437,13 @@ def test_read_write_dta13(self): tm.assert_frame_equal(written_and_read_again.set_index('index'), formatted) - def test_read_write_reread_dta14(self): + @pytest.mark.parametrize( + 'file', ['dta14_113', 'dta14_114', 'dta14_115', 'dta14_117']) + def test_read_write_reread_dta14(self, file, parsed_114): + file = getattr(self, file) + parsed = self.read_dta(file) + parsed.index.name = 'index' + expected = self.read_csv(self.csv14) cols = ['byte_', 'int_', 'long_', 'float_', 'double_'] for col in cols: @@ -436,18 +452,7 @@ def test_read_write_reread_dta14(self): expected['date_td'] = pd.to_datetime( expected['date_td'], errors='coerce') - parsed_113 = self.read_dta(self.dta14_113) - parsed_113.index.name = 'index' - parsed_114 = self.read_dta(self.dta14_114) - parsed_114.index.name = 'index' - parsed_115 = self.read_dta(self.dta14_115) - parsed_115.index.name = 'index' - parsed_117 = self.read_dta(self.dta14_117) - parsed_117.index.name = 'index' - - tm.assert_frame_equal(parsed_114, parsed_113) - tm.assert_frame_equal(parsed_114, parsed_115) - tm.assert_frame_equal(parsed_114, parsed_117) + tm.assert_frame_equal(parsed_114, parsed) with tm.ensure_clean() as path: parsed_114.to_stata(path, {'date_td': 'td'}) @@ -455,7 +460,10 @@ def test_read_write_reread_dta14(self): tm.assert_frame_equal( written_and_read_again.set_index('index'), parsed_114) - def test_read_write_reread_dta15(self): + @pytest.mark.parametrize( + 'file', ['dta15_113', 'dta15_114', 'dta15_115', 'dta15_117']) + def test_read_write_reread_dta15(self, file): + expected = self.read_csv(self.csv15) expected['byte_'] = expected['byte_'].astype(np.int8) expected['int_'] = expected['int_'].astype(np.int16) @@ -465,18 +473,13 @@ def test_read_write_reread_dta15(self): expected['date_td'] = expected['date_td'].apply( datetime.strptime, args=('%Y-%m-%d',)) - parsed_113 = self.read_dta(self.dta15_113) - parsed_114 = self.read_dta(self.dta15_114) - parsed_115 = self.read_dta(self.dta15_115) - parsed_117 = self.read_dta(self.dta15_117) + file = getattr(self, file) + parsed = self.read_dta(file) - tm.assert_frame_equal(expected, parsed_114) - tm.assert_frame_equal(parsed_113, parsed_114) - tm.assert_frame_equal(parsed_114, parsed_115) - tm.assert_frame_equal(parsed_114, parsed_117) + tm.assert_frame_equal(expected, parsed) def test_timestamp_and_label(self): - original = DataFrame([(1,)], columns=['var']) + original = DataFrame([(1,)], columns=['variable']) time_stamp = datetime(2000, 2, 29, 14, 21) data_label = 'This is a data file.' with tm.ensure_clean() as path: @@ -710,7 +713,9 @@ def test_missing_value_generator(self): '= pidx[-1].ordinal @pytest.mark.slow def test_mixed_freq_irregular_first(self): @@ -696,8 +730,8 @@ def test_mixed_freq_regular_first_df(self): assert idx2.equals(s2.index.to_period('B')) left, right = ax2.get_xlim() pidx = s1.index.to_period() - assert left == pidx[0].ordinal - assert right == pidx[-1].ordinal + assert left <= pidx[0].ordinal + assert right >= pidx[-1].ordinal @pytest.mark.slow def test_mixed_freq_irregular_first_df(self): @@ -1211,8 +1245,8 @@ def test_irregular_ts_shared_ax_xlim(self): # check that axis limits are correct left, right = ax.get_xlim() - assert left == ts_irregular.index.min().toordinal() - assert right == ts_irregular.index.max().toordinal() + assert left <= ts_irregular.index.min().toordinal() + assert right >= ts_irregular.index.max().toordinal() @pytest.mark.slow def test_secondary_y_non_ts_xlim(self): @@ -1228,7 +1262,7 @@ def test_secondary_y_non_ts_xlim(self): s2.plot(secondary_y=True, ax=ax) left_after, right_after = ax.get_xlim() - assert left_before == left_after + assert left_before >= left_after assert right_before < right_after @pytest.mark.slow @@ -1245,7 +1279,7 @@ def test_secondary_y_regular_ts_xlim(self): s2.plot(secondary_y=True, ax=ax) left_after, right_after = ax.get_xlim() - assert left_before == left_after + assert left_before >= left_after assert right_before < right_after @pytest.mark.slow @@ -1278,8 +1312,8 @@ def test_secondary_y_irregular_ts_xlim(self): ts_irregular[:5].plot(ax=ax) left, right = ax.get_xlim() - assert left == ts_irregular.index.min().toordinal() - assert right == ts_irregular.index.max().toordinal() + assert left <= ts_irregular.index.min().toordinal() + assert right >= ts_irregular.index.max().toordinal() def test_plot_outofbounds_datetime(self): # 2579 - checking this does not raise @@ -1294,9 +1328,14 @@ def test_format_timedelta_ticks_narrow(self): if is_platform_mac(): pytest.skip("skip on mac for precision display issue on older mpl") - expected_labels = [ - '00:00:00.00000000{:d}'.format(i) - for i in range(10)] + if self.mpl_ge_2_0_0: + expected_labels = [''] + [ + '00:00:00.00000000{:d}'.format(2 * i) + for i in range(5)] + [''] + else: + expected_labels = [ + '00:00:00.00000000{:d}'.format(i) + for i in range(10)] rng = timedelta_range('0', periods=10, freq='ns') df = DataFrame(np.random.randn(len(rng), 3), rng) @@ -1312,17 +1351,32 @@ def test_format_timedelta_ticks_wide(self): if is_platform_mac(): pytest.skip("skip on mac for precision display issue on older mpl") - expected_labels = [ - '00:00:00', - '1 days 03:46:40', - '2 days 07:33:20', - '3 days 11:20:00', - '4 days 15:06:40', - '5 days 18:53:20', - '6 days 22:40:00', - '8 days 02:26:40', - '' - ] + if self.mpl_ge_2_0_0: + expected_labels = [ + '', + '00:00:00', + '1 days 03:46:40', + '2 days 07:33:20', + '3 days 11:20:00', + '4 days 15:06:40', + '5 days 18:53:20', + '6 days 22:40:00', + '8 days 02:26:40', + '9 days 06:13:20', + '' + ] + else: + expected_labels = [ + '00:00:00', + '1 days 03:46:40', + '2 days 07:33:20', + '3 days 11:20:00', + '4 days 15:06:40', + '5 days 18:53:20', + '6 days 22:40:00', + '8 days 02:26:40', + '' + ] rng = timedelta_range('0', periods=10, freq='1 d') df = DataFrame(np.random.randn(len(rng), 3), rng) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index f3b287a8889c3..a428d73fce1e3 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -304,6 +304,29 @@ def test_unsorted_index(self): rs = Series(rs[:, 1], rs[:, 0], dtype=np.int64, name='y') tm.assert_series_equal(rs, df.y) + def test_unsorted_index_lims(self): + df = DataFrame({'y': [0., 1., 2., 3.]}, index=[1., 0., 3., 2.]) + ax = df.plot() + xmin, xmax = ax.get_xlim() + lines = ax.get_lines() + assert xmin <= np.nanmin(lines[0].get_data()[0]) + assert xmax >= np.nanmax(lines[0].get_data()[0]) + + df = DataFrame({'y': [0., 1., np.nan, 3., 4., 5., 6.]}, + index=[1., 0., 3., 2., np.nan, 3., 2.]) + ax = df.plot() + xmin, xmax = ax.get_xlim() + lines = ax.get_lines() + assert xmin <= np.nanmin(lines[0].get_data()[0]) + assert xmax >= np.nanmax(lines[0].get_data()[0]) + + df = DataFrame({'y': [0., 1., 2., 3.], 'z': [91., 90., 93., 92.]}) + ax = df.plot(x='z', y='y') + xmin, xmax = ax.get_xlim() + lines = ax.get_lines() + assert xmin <= np.nanmin(lines[0].get_data()[0]) + assert xmax >= np.nanmax(lines[0].get_data()[0]) + @pytest.mark.slow def test_subplots(self): df = DataFrame(np.random.rand(10, 3), @@ -735,14 +758,14 @@ def test_line_lim(self): ax = df.plot() xmin, xmax = ax.get_xlim() lines = ax.get_lines() - assert xmin == lines[0].get_data()[0][0] - assert xmax == lines[0].get_data()[0][-1] + assert xmin <= lines[0].get_data()[0][0] + assert xmax >= lines[0].get_data()[0][-1] ax = df.plot(secondary_y=True) xmin, xmax = ax.get_xlim() lines = ax.get_lines() - assert xmin == lines[0].get_data()[0][0] - assert xmax == lines[0].get_data()[0][-1] + assert xmin <= lines[0].get_data()[0][0] + assert xmax >= lines[0].get_data()[0][-1] axes = df.plot(secondary_y=True, subplots=True) self._check_axes_shape(axes, axes_num=3, layout=(3, 1)) @@ -751,8 +774,8 @@ def test_line_lim(self): assert not hasattr(ax, 'right_ax') xmin, xmax = ax.get_xlim() lines = ax.get_lines() - assert xmin == lines[0].get_data()[0][0] - assert xmax == lines[0].get_data()[0][-1] + assert xmin <= lines[0].get_data()[0][0] + assert xmax >= lines[0].get_data()[0][-1] def test_area_lim(self): df = DataFrame(rand(6, 4), columns=['x', 'y', 'z', 'four']) @@ -763,8 +786,8 @@ def test_area_lim(self): xmin, xmax = ax.get_xlim() ymin, ymax = ax.get_ylim() lines = ax.get_lines() - assert xmin == lines[0].get_data()[0][0] - assert xmax == lines[0].get_data()[0][-1] + assert xmin <= lines[0].get_data()[0][0] + assert xmax >= lines[0].get_data()[0][-1] assert ymin == 0 ax = _check_plot_works(neg_df.plot.area, stacked=stacked) @@ -807,6 +830,20 @@ def test_bar_colors(self): self._check_colors(ax.patches[::5], facecolors=['green'] * 5) tm.close() + def test_bar_user_colors(self): + df = pd.DataFrame({"A": range(4), + "B": range(1, 5), + "color": ['red', 'blue', 'blue', 'red']}) + # This should *only* work when `y` is specified, else + # we use one color per column + ax = df.plot.bar(y='A', color=df['color']) + result = [p.get_facecolor() for p in ax.patches] + expected = [(1., 0., 0., 1.), + (0., 0., 1., 1.), + (0., 0., 1., 1.), + (1., 0., 0., 1.)] + assert result == expected + @pytest.mark.slow def test_bar_linewidth(self): df = DataFrame(randn(5, 5)) diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index 8164ad74a190a..d04065ee34339 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -102,23 +102,23 @@ def test_ts_line_lim(self): ax = self.ts.plot(ax=ax) xmin, xmax = ax.get_xlim() lines = ax.get_lines() - assert xmin == lines[0].get_data(orig=False)[0][0] - assert xmax == lines[0].get_data(orig=False)[0][-1] + assert xmin <= lines[0].get_data(orig=False)[0][0] + assert xmax >= lines[0].get_data(orig=False)[0][-1] tm.close() ax = self.ts.plot(secondary_y=True, ax=ax) xmin, xmax = ax.get_xlim() lines = ax.get_lines() - assert xmin == lines[0].get_data(orig=False)[0][0] - assert xmax == lines[0].get_data(orig=False)[0][-1] + assert xmin <= lines[0].get_data(orig=False)[0][0] + assert xmax >= lines[0].get_data(orig=False)[0][-1] def test_ts_area_lim(self): _, ax = self.plt.subplots() ax = self.ts.plot.area(stacked=False, ax=ax) xmin, xmax = ax.get_xlim() line = ax.get_lines()[0].get_data(orig=False)[0] - assert xmin == line[0] - assert xmax == line[-1] + assert xmin <= line[0] + assert xmax >= line[-1] tm.close() # GH 7471 @@ -126,8 +126,8 @@ def test_ts_area_lim(self): ax = self.ts.plot.area(stacked=False, x_compat=True, ax=ax) xmin, xmax = ax.get_xlim() line = ax.get_lines()[0].get_data(orig=False)[0] - assert xmin == line[0] - assert xmax == line[-1] + assert xmin <= line[0] + assert xmax >= line[-1] tm.close() tz_ts = self.ts.copy() @@ -136,16 +136,16 @@ def test_ts_area_lim(self): ax = tz_ts.plot.area(stacked=False, x_compat=True, ax=ax) xmin, xmax = ax.get_xlim() line = ax.get_lines()[0].get_data(orig=False)[0] - assert xmin == line[0] - assert xmax == line[-1] + assert xmin <= line[0] + assert xmax >= line[-1] tm.close() _, ax = self.plt.subplots() ax = tz_ts.plot.area(stacked=False, secondary_y=True, ax=ax) xmin, xmax = ax.get_xlim() line = ax.get_lines()[0].get_data(orig=False)[0] - assert xmin == line[0] - assert xmax == line[-1] + assert xmin <= line[0] + assert xmax >= line[-1] def test_label(self): s = Series([1, 2]) @@ -258,6 +258,16 @@ def test_bar_ignore_index(self): ax = df.plot.bar(use_index=False, ax=ax) self._check_text_labels(ax.get_xticklabels(), ['0', '1', '2', '3']) + def test_bar_user_colors(self): + s = Series([1, 2, 3, 4]) + ax = s.plot.bar(color=['red', 'blue', 'blue', 'red']) + result = [p.get_facecolor() for p in ax.patches] + expected = [(1., 0., 0., 1.), + (0., 0., 1., 1.), + (0., 0., 1., 1.), + (1., 0., 0., 1.)] + assert result == expected + def test_rotation(self): df = DataFrame(randn(5, 5)) # Default rot 0 @@ -279,6 +289,16 @@ def test_irregular_datetime(self): ax.set_xlim('1/1/1999', '1/1/2001') assert xp == ax.get_xlim()[0] + def test_unsorted_index_xlim(self): + ser = Series([0., 1., np.nan, 3., 4., 5., 6.], + index=[1., 0., 3., 2., np.nan, 3., 2.]) + _, ax = self.plt.subplots() + ax = ser.plot(ax=ax) + xmin, xmax = ax.get_xlim() + lines = ax.get_lines() + assert xmin <= np.nanmin(lines[0].get_data(orig=False)[0]) + assert xmax >= np.nanmax(lines[0].get_data(orig=False)[0]) + @pytest.mark.slow def test_pie_series(self): # if sum of values is less than 1.0, pie handle them as rate and draw diff --git a/pandas/tests/reshape/test_merge.py b/pandas/tests/reshape/test_merge.py index 338596d1523e4..df75983a29d80 100644 --- a/pandas/tests/reshape/test_merge.py +++ b/pandas/tests/reshape/test_merge.py @@ -1468,8 +1468,6 @@ def test_other_columns(self, left, right): @pytest.mark.parametrize( 'change', [lambda x: x, - lambda x: x.astype('category', - categories=['bar', 'foo']), lambda x: x.astype('category', categories=['foo', 'bar', 'bah']), lambda x: x.astype('category', ordered=True)]) @@ -1481,7 +1479,7 @@ def test_dtype_on_merged_different(self, change, how, left, right): X = change(right.X.astype('object')) right = right.assign(X=X) assert is_categorical_dtype(left.X.values) - assert not left.X.values.is_dtype_equal(right.X.values) + # assert not left.X.values.is_dtype_equal(right.X.values) merged = pd.merge(left, right, on='X', how=how) diff --git a/pandas/tests/scalar/test_period.py b/pandas/tests/scalar/test_period.py index a167c9c738b0b..c17a216df44cb 100644 --- a/pandas/tests/scalar/test_period.py +++ b/pandas/tests/scalar/test_period.py @@ -245,29 +245,29 @@ def test_timestamp_tz_arg(self): assert p.tz == exp.tz def test_timestamp_tz_arg_dateutil(self): - from pandas._libs.tslib import _dateutil_gettz as gettz - from pandas._libs.tslib import maybe_get_tz + from pandas._libs.tslibs.timezones import dateutil_gettz + from pandas._libs.tslibs.timezones import maybe_get_tz for case in ['dateutil/Europe/Brussels', 'dateutil/Asia/Tokyo', 'dateutil/US/Pacific']: p = Period('1/1/2005', freq='M').to_timestamp( tz=maybe_get_tz(case)) exp = Timestamp('1/1/2005', tz='UTC').tz_convert(case) assert p == exp - assert p.tz == gettz(case.split('/', 1)[1]) + assert p.tz == dateutil_gettz(case.split('/', 1)[1]) assert p.tz == exp.tz p = Period('1/1/2005', freq='M').to_timestamp(freq='3H', tz=maybe_get_tz(case)) exp = Timestamp('1/1/2005', tz='UTC').tz_convert(case) assert p == exp - assert p.tz == gettz(case.split('/', 1)[1]) + assert p.tz == dateutil_gettz(case.split('/', 1)[1]) assert p.tz == exp.tz def test_timestamp_tz_arg_dateutil_from_string(self): - from pandas._libs.tslib import _dateutil_gettz as gettz + from pandas._libs.tslibs.timezones import dateutil_gettz p = Period('1/1/2005', freq='M').to_timestamp(tz='dateutil/Europe/Brussels') - assert p.tz == gettz('Europe/Brussels') + assert p.tz == dateutil_gettz('Europe/Brussels') def test_timestamp_mult(self): p = pd.Period('2011-01', freq='M') diff --git a/pandas/tests/scalar/test_timedelta.py b/pandas/tests/scalar/test_timedelta.py index bc9a0388df9d9..b5a8ce24fa4f8 100644 --- a/pandas/tests/scalar/test_timedelta.py +++ b/pandas/tests/scalar/test_timedelta.py @@ -9,7 +9,7 @@ from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type as ct from pandas import (Timedelta, TimedeltaIndex, timedelta_range, Series, to_timedelta, compat) -from pandas._libs.tslib import iNaT, NaTType +from pandas._libs.tslib import iNaT, NaT class TestTimedeltas(object): @@ -166,6 +166,13 @@ def test_overflow_on_construction(self): value = pd.Timedelta('1day').value * 20169940 pytest.raises(OverflowError, pd.Timedelta, value) + # xref gh-17637 + with pytest.raises(OverflowError): + pd.Timedelta(7 * 19999, unit='D') + + with pytest.raises(OverflowError): + pd.Timedelta(timedelta(days=13 * 19999)) + def test_total_seconds_scalar(self): # see gh-10939 rng = Timedelta('1 days, 10:11:12.100123456') @@ -572,7 +579,7 @@ def test_implementation_limits(self): assert max_td.value == np.iinfo(np.int64).max # Beyond lower limit, a NAT before the Overflow - assert isinstance(min_td - Timedelta(1, 'ns'), NaTType) + assert (min_td - Timedelta(1, 'ns')) is NaT with pytest.raises(OverflowError): min_td - Timedelta(2, 'ns') @@ -582,7 +589,7 @@ def test_implementation_limits(self): # Same tests using the internal nanosecond values td = Timedelta(min_td.value - 1, 'ns') - assert isinstance(td, NaTType) + assert td is NaT with pytest.raises(OverflowError): Timedelta(min_td.value - 2, 'ns') @@ -612,6 +619,14 @@ def test_timedelta_arithmetic(self): tm.assert_series_equal(result_operator, expected) tm.assert_series_equal(result_method, expected) + def test_arithmetic_overflow(self): + + with pytest.raises(OverflowError): + pd.Timestamp('1700-01-01') + pd.Timedelta(13 * 19999, unit='D') + + with pytest.raises(OverflowError): + pd.Timestamp('1700-01-01') + timedelta(days=13 * 19999) + def test_apply_to_timedelta(self): timedelta_NaT = pd.to_timedelta('NaT') diff --git a/pandas/tests/scalar/test_timestamp.py b/pandas/tests/scalar/test_timestamp.py index 8d47ce4802ac6..c1b9f858a08de 100644 --- a/pandas/tests/scalar/test_timestamp.py +++ b/pandas/tests/scalar/test_timestamp.py @@ -17,7 +17,7 @@ import pandas.util.testing as tm from pandas.tseries import offsets, frequencies from pandas._libs import tslib, period -from pandas._libs.tslib import get_timezone +from pandas._libs.tslibs.timezones import get_timezone from pandas.compat import lrange, long from pandas.util.testing import assert_series_equal @@ -1295,7 +1295,7 @@ def test_timestamp_to_datetime_explicit_pytz(self): def test_timestamp_to_datetime_explicit_dateutil(self): tm._skip_if_windows_python_3() - from pandas._libs.tslib import _dateutil_gettz as gettz + from pandas._libs.tslibs.timezones import dateutil_gettz as gettz rng = date_range('20090415', '20090519', tz=gettz('US/Eastern')) stamp = rng[0] diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index f1d044f7a1132..914181dc94154 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -1756,7 +1756,6 @@ class TestNLargestNSmallest(object): # not supported on some archs # Series([3., 2, 1, 2, 5], dtype='complex256'), Series([3., 2, 1, 2, 5], dtype='complex128'), - Series(list('abcde'), dtype='category'), Series(list('abcde'))]) def test_error(self, r): dt = r.dtype @@ -1768,6 +1767,16 @@ def test_error(self, r): with tm.assert_raises_regex(TypeError, msg): method(arg) + def test_error_categorical_dtype(self): + # same as test_error, but regex hard to escape properly + msg = ("Cannot use method 'n(larg|small)est' with dtype " + "CategoricalDtype.+") + with tm.assert_raises_regex(TypeError, msg): + Series(list('ab'), dtype='category').nlargest(2) + + with tm.assert_raises_regex(TypeError, msg): + Series(list('ab'), dtype='category').nsmallest(2) + @pytest.mark.parametrize( "s", [v for k, v in s_main_dtypes().iteritems()]) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 3b95c2803dd9e..df7d7a946e881 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -10,6 +10,7 @@ import numpy.ma as ma import pandas as pd +from pandas.api.types import CategoricalDtype from pandas.core.dtypes.common import ( is_categorical_dtype, is_datetime64tz_dtype) @@ -157,6 +158,26 @@ def test_constructor_categorical(self): assert is_categorical_dtype(s) assert is_categorical_dtype(s.dtype) + def test_constructor_categorical_dtype(self): + result = pd.Series(['a', 'b'], + dtype=CategoricalDtype(['a', 'b', 'c'], + ordered=True)) + assert is_categorical_dtype(result) is True + tm.assert_index_equal(result.cat.categories, pd.Index(['a', 'b', 'c'])) + assert result.cat.ordered + + result = pd.Series(['a', 'b'], dtype=CategoricalDtype(['b', 'a'])) + assert is_categorical_dtype(result) + tm.assert_index_equal(result.cat.categories, pd.Index(['b', 'a'])) + assert result.cat.ordered is False + + def test_unordered_compare_equal(self): + left = pd.Series(['a', 'b', 'c'], + dtype=CategoricalDtype(['a', 'b'])) + right = pd.Series(pd.Categorical(['a', 'b', np.nan], + categories=['a', 'b'])) + tm.assert_series_equal(left, right) + def test_constructor_maskedarray(self): data = ma.masked_all((3, ), dtype=float) result = Series(data) diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index fa9feb016726e..3099c02e4aabd 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -12,7 +12,11 @@ from numpy import nan import numpy as np -from pandas import Series, Timestamp, Timedelta, DataFrame, date_range +from pandas import ( + Series, Timestamp, Timedelta, DataFrame, date_range, + Categorical, Index +) +from pandas.api.types import CategoricalDtype from pandas.compat import lrange, range, u from pandas import compat @@ -182,6 +186,34 @@ def test_astype_dict_like(self, dtype_class): with pytest.raises(KeyError): s.astype(dt5) + def test_astype_categoricaldtype(self): + s = Series(['a', 'b', 'a']) + result = s.astype(CategoricalDtype(['a', 'b'], ordered=True)) + expected = Series(Categorical(['a', 'b', 'a'], ordered=True)) + tm.assert_series_equal(result, expected) + + result = s.astype(CategoricalDtype(['a', 'b'], ordered=False)) + expected = Series(Categorical(['a', 'b', 'a'], ordered=False)) + tm.assert_series_equal(result, expected) + + result = s.astype(CategoricalDtype(['a', 'b', 'c'], ordered=False)) + expected = Series(Categorical(['a', 'b', 'a'], + categories=['a', 'b', 'c'], + ordered=False)) + tm.assert_series_equal(result, expected) + tm.assert_index_equal(result.cat.categories, Index(['a', 'b', 'c'])) + + def test_astype_categoricaldtype_with_args(self): + s = Series(['a', 'b']) + type_ = CategoricalDtype(['a', 'b']) + + with pytest.raises(TypeError): + s.astype(type_, ordered=True) + with pytest.raises(TypeError): + s.astype(type_, categories=['a', 'b']) + with pytest.raises(TypeError): + s.astype(type_, categories=['a', 'b'], ordered=False) + def test_astype_generic_timestamp_deprecated(self): # see gh-15524 data = [1] diff --git a/pandas/tests/series/test_indexing.py b/pandas/tests/series/test_indexing.py index 45a92f6d6f50b..91187b709463a 100644 --- a/pandas/tests/series/test_indexing.py +++ b/pandas/tests/series/test_indexing.py @@ -387,7 +387,7 @@ def test_getitem_setitem_datetime_tz_pytz(self): def test_getitem_setitem_datetime_tz_dateutil(self): from dateutil.tz import tzutc - from pandas._libs.tslib import _dateutil_gettz as gettz + from pandas._libs.tslibs.timezones import dateutil_gettz as gettz tz = lambda x: tzutc() if x == 'UTC' else gettz( x) # handle special case for utc in dateutil diff --git a/pandas/tests/sparse/test_frame.py b/pandas/tests/sparse/test_frame.py index 004af5066fe83..ed4a3a9e5f75f 100644 --- a/pandas/tests/sparse/test_frame.py +++ b/pandas/tests/sparse/test_frame.py @@ -1099,7 +1099,10 @@ def test_as_blocks(self): df = SparseDataFrame({'A': [1.1, 3.3], 'B': [nan, -3.9]}, dtype='float64') - df_blocks = df.blocks + # deprecated 0.21.0 + with tm.assert_produces_warning(FutureWarning, + check_stacklevel=False): + df_blocks = df.blocks assert list(df_blocks.keys()) == ['float64'] tm.assert_frame_equal(df_blocks['float64'], df) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index b26089ea7a822..3694bba594adb 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -760,55 +760,57 @@ def test_duplicated_with_nas(self): expected = np.array(trues + trues) tm.assert_numpy_array_equal(result, expected) - def test_numeric_object_likes(self): - cases = [np.array([1, 2, 1, 5, 3, - 2, 4, 1, 5, 6]), - np.array([1.1, 2.2, 1.1, np.nan, 3.3, - 2.2, 4.4, 1.1, np.nan, 6.6]), - np.array([1 + 1j, 2 + 2j, 1 + 1j, 5 + 5j, 3 + 3j, - 2 + 2j, 4 + 4j, 1 + 1j, 5 + 5j, 6 + 6j]), - np.array(['a', 'b', 'a', 'e', 'c', - 'b', 'd', 'a', 'e', 'f'], dtype=object), - np.array([1, 2**63, 1, 3**5, 10, - 2**63, 39, 1, 3**5, 7], dtype=np.uint64)] - + @pytest.mark.parametrize('case', [ + np.array([1, 2, 1, 5, 3, + 2, 4, 1, 5, 6]), + np.array([1.1, 2.2, 1.1, np.nan, 3.3, + 2.2, 4.4, 1.1, np.nan, 6.6]), + pytest.mark.xfail(resaon="Complex bug. GH 16399")( + np.array([1 + 1j, 2 + 2j, 1 + 1j, 5 + 5j, 3 + 3j, + 2 + 2j, 4 + 4j, 1 + 1j, 5 + 5j, 6 + 6j]) + ), + np.array(['a', 'b', 'a', 'e', 'c', + 'b', 'd', 'a', 'e', 'f'], dtype=object), + np.array([1, 2**63, 1, 3**5, 10, 2**63, 39, 1, 3**5, 7], + dtype=np.uint64), + ]) + def test_numeric_object_likes(self, case): exp_first = np.array([False, False, True, False, False, True, False, True, True, False]) exp_last = np.array([True, True, True, True, False, False, False, False, False, False]) exp_false = exp_first | exp_last - for case in cases: - res_first = algos.duplicated(case, keep='first') - tm.assert_numpy_array_equal(res_first, exp_first) + res_first = algos.duplicated(case, keep='first') + tm.assert_numpy_array_equal(res_first, exp_first) - res_last = algos.duplicated(case, keep='last') - tm.assert_numpy_array_equal(res_last, exp_last) + res_last = algos.duplicated(case, keep='last') + tm.assert_numpy_array_equal(res_last, exp_last) - res_false = algos.duplicated(case, keep=False) - tm.assert_numpy_array_equal(res_false, exp_false) + res_false = algos.duplicated(case, keep=False) + tm.assert_numpy_array_equal(res_false, exp_false) - # index - for idx in [pd.Index(case), pd.Index(case, dtype='category')]: - res_first = idx.duplicated(keep='first') - tm.assert_numpy_array_equal(res_first, exp_first) + # index + for idx in [pd.Index(case), pd.Index(case, dtype='category')]: + res_first = idx.duplicated(keep='first') + tm.assert_numpy_array_equal(res_first, exp_first) - res_last = idx.duplicated(keep='last') - tm.assert_numpy_array_equal(res_last, exp_last) + res_last = idx.duplicated(keep='last') + tm.assert_numpy_array_equal(res_last, exp_last) - res_false = idx.duplicated(keep=False) - tm.assert_numpy_array_equal(res_false, exp_false) + res_false = idx.duplicated(keep=False) + tm.assert_numpy_array_equal(res_false, exp_false) - # series - for s in [Series(case), Series(case, dtype='category')]: - res_first = s.duplicated(keep='first') - tm.assert_series_equal(res_first, Series(exp_first)) + # series + for s in [Series(case), Series(case, dtype='category')]: + res_first = s.duplicated(keep='first') + tm.assert_series_equal(res_first, Series(exp_first)) - res_last = s.duplicated(keep='last') - tm.assert_series_equal(res_last, Series(exp_last)) + res_last = s.duplicated(keep='last') + tm.assert_series_equal(res_last, Series(exp_last)) - res_false = s.duplicated(keep=False) - tm.assert_series_equal(res_false, Series(exp_false)) + res_false = s.duplicated(keep=False) + tm.assert_series_equal(res_false, Series(exp_false)) def test_datetime_likes(self): diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index 38d78b12b31aa..5bfd8eb7eae24 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -18,7 +18,8 @@ CategoricalIndex, Timestamp) from pandas.compat import StringIO, PYPY, long from pandas.compat.numpy import np_array_datetime64_compat -from pandas.core.base import PandasDelegate, NoNewAttributesMixin +from pandas.core.accessor import PandasDelegate +from pandas.core.base import PandasObject, NoNewAttributesMixin from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin from pandas._libs.tslib import iNaT @@ -105,7 +106,7 @@ def bar(self, *args, **kwargs): """ a test bar method """ pass - class Delegate(PandasDelegate): + class Delegate(PandasDelegate, PandasObject): def __init__(self, obj): self.obj = obj diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index 1fa3c84dc0260..d43901ea091b7 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -57,6 +57,25 @@ def test_getitem_listlike(self): expected = c[np.array([100000]).astype(np.int64)].codes tm.assert_numpy_array_equal(result, expected) + @pytest.mark.parametrize( + "method", + [ + lambda x: x.cat.set_categories([1, 2, 3]), + lambda x: x.cat.reorder_categories([2, 3, 1], ordered=True), + lambda x: x.cat.rename_categories([1, 2, 3]), + lambda x: x.cat.remove_unused_categories(), + lambda x: x.cat.remove_categories([2]), + lambda x: x.cat.add_categories([4]), + lambda x: x.cat.as_ordered(), + lambda x: x.cat.as_unordered(), + ]) + def test_getname_categorical_accessor(self, method): + # GH 17509 + s = pd.Series([1, 2, 3], name='A').astype('category') + expected = 'A' + result = method(s).name + assert result == expected + def test_getitem_category_type(self): # GH 14580 # test iloc() on Series with Categorical data @@ -123,6 +142,26 @@ def test_constructor_empty(self): expected = pd.Int64Index([1, 2, 3]) tm.assert_index_equal(c.categories, expected) + def test_constructor_tuples(self): + values = np.array([(1,), (1, 2), (1,), (1, 2)], dtype=object) + result = Categorical(values) + expected = Index([(1,), (1, 2)], tupleize_cols=False) + tm.assert_index_equal(result.categories, expected) + assert result.ordered is False + + def test_constructor_tuples_datetimes(self): + # numpy will auto reshape when all of the tuples are the + # same len, so add an extra one with 2 items and slice it off + values = np.array([(Timestamp('2010-01-01'),), + (Timestamp('2010-01-02'),), + (Timestamp('2010-01-01'),), + (Timestamp('2010-01-02'),), + ('a', 'b')], dtype=object)[:-1] + result = Categorical(values) + expected = Index([(Timestamp('2010-01-01'),), + (Timestamp('2010-01-02'),)], tupleize_cols=False) + tm.assert_index_equal(result.categories, expected) + def test_constructor_unsortable(self): # it works! @@ -154,12 +193,12 @@ def test_is_equal_dtype(self): assert c1.is_dtype_equal(c1) assert c2.is_dtype_equal(c2) assert c3.is_dtype_equal(c3) - assert not c1.is_dtype_equal(c2) + assert c1.is_dtype_equal(c2) assert not c1.is_dtype_equal(c3) assert not c1.is_dtype_equal(Index(list('aabca'))) assert not c1.is_dtype_equal(c1.astype(object)) assert c1.is_dtype_equal(CategoricalIndex(c1)) - assert not (c1.is_dtype_equal( + assert (c1.is_dtype_equal( CategoricalIndex(c1, categories=list('cab')))) assert not c1.is_dtype_equal(CategoricalIndex(c1, ordered=True)) @@ -413,6 +452,73 @@ def test_constructor_invariant(self): c2 = Categorical(c) tm.assert_categorical_equal(c, c2) + @pytest.mark.parametrize('ordered', [True, False]) + def test_constructor_with_dtype(self, ordered): + categories = ['b', 'a', 'c'] + dtype = CategoricalDtype(categories, ordered=ordered) + result = pd.Categorical(['a', 'b', 'a', 'c'], dtype=dtype) + expected = pd.Categorical(['a', 'b', 'a', 'c'], categories=categories, + ordered=ordered) + tm.assert_categorical_equal(result, expected) + assert result.ordered is ordered + + def test_constructor_dtype_and_others_raises(self): + dtype = CategoricalDtype(['a', 'b'], ordered=True) + with tm.assert_raises_regex(ValueError, "Cannot"): + Categorical(['a', 'b'], categories=['a', 'b'], dtype=dtype) + + with tm.assert_raises_regex(ValueError, "Cannot"): + Categorical(['a', 'b'], ordered=True, dtype=dtype) + + with tm.assert_raises_regex(ValueError, "Cannot"): + Categorical(['a', 'b'], ordered=False, dtype=dtype) + + @pytest.mark.parametrize('categories', [ + None, ['a', 'b'], ['a', 'c'], + ]) + @pytest.mark.parametrize('ordered', [True, False]) + def test_constructor_str_category(self, categories, ordered): + result = Categorical(['a', 'b'], categories=categories, + ordered=ordered, dtype='category') + expected = Categorical(['a', 'b'], categories=categories, + ordered=ordered) + tm.assert_categorical_equal(result, expected) + + def test_constructor_str_unknown(self): + with tm.assert_raises_regex(ValueError, "Unknown `dtype`"): + Categorical([1, 2], dtype="foo") + + def test_constructor_from_categorical_with_dtype(self): + dtype = CategoricalDtype(['a', 'b', 'c'], ordered=True) + values = Categorical(['a', 'b', 'd']) + result = Categorical(values, dtype=dtype) + # We use dtype.categories, not values.categories + expected = Categorical(['a', 'b', 'd'], categories=['a', 'b', 'c'], + ordered=True) + tm.assert_categorical_equal(result, expected) + + def test_constructor_from_categorical_with_unknown_dtype(self): + dtype = CategoricalDtype(None, ordered=True) + values = Categorical(['a', 'b', 'd']) + result = Categorical(values, dtype=dtype) + # We use values.categories, not dtype.categories + expected = Categorical(['a', 'b', 'd'], categories=['a', 'b', 'd'], + ordered=True) + tm.assert_categorical_equal(result, expected) + + def test_contructor_from_categorical_string(self): + values = Categorical(['a', 'b', 'd']) + # use categories, ordered + result = Categorical(values, categories=['a', 'b', 'c'], ordered=True, + dtype='category') + expected = Categorical(['a', 'b', 'd'], categories=['a', 'b', 'c'], + ordered=True) + tm.assert_categorical_equal(result, expected) + + # No string + result = Categorical(values, categories=['a', 'b', 'c'], ordered=True) + tm.assert_categorical_equal(result, expected) + def test_from_codes(self): # too few categories @@ -624,6 +730,11 @@ def test_categories_none(self): 'a', 'c', 'c', 'c'], ordered=True) tm.assert_categorical_equal(factor, self.factor) + def test_set_categories_inplace(self): + cat = self.factor.copy() + cat.set_categories(['a', 'b', 'c', 'd'], inplace=True) + tm.assert_index_equal(cat.categories, pd.Index(['a', 'b', 'c', 'd'])) + def test_describe(self): # string type desc = self.factor.describe() @@ -834,6 +945,72 @@ def test_ordered_api(self): tm.assert_index_equal(cat4.categories, Index(['b', 'c', 'a'])) assert cat4.ordered + def test_set_dtype_same(self): + c = Categorical(['a', 'b', 'c']) + result = c._set_dtype(CategoricalDtype(['a', 'b', 'c'])) + tm.assert_categorical_equal(result, c) + + def test_set_dtype_new_categories(self): + c = Categorical(['a', 'b', 'c']) + result = c._set_dtype(CategoricalDtype(['a', 'b', 'c', 'd'])) + tm.assert_numpy_array_equal(result.codes, c.codes) + tm.assert_index_equal(result.dtype.categories, + pd.Index(['a', 'b', 'c', 'd'])) + + def test_set_dtype_nans(self): + c = Categorical(['a', 'b', np.nan]) + result = c._set_dtype(CategoricalDtype(['a', 'c'])) + tm.assert_numpy_array_equal(result.codes, np.array([0, -1, -1], + dtype='int8')) + + def test_set_categories_private(self): + cat = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c', 'd']) + cat._set_categories(['a', 'c', 'd', 'e']) + expected = Categorical(['a', 'c', 'd'], categories=list('acde')) + tm.assert_categorical_equal(cat, expected) + + # fastpath + cat = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c', 'd']) + cat._set_categories(['a', 'c', 'd', 'e'], fastpath=True) + expected = Categorical(['a', 'c', 'd'], categories=list('acde')) + tm.assert_categorical_equal(cat, expected) + + @pytest.mark.parametrize('values, categories, new_categories', [ + # No NaNs, same cats, same order + (['a', 'b', 'a'], ['a', 'b'], ['a', 'b'],), + # No NaNs, same cats, different order + (['a', 'b', 'a'], ['a', 'b'], ['b', 'a'],), + # Same, unsorted + (['b', 'a', 'a'], ['a', 'b'], ['a', 'b'],), + # No NaNs, same cats, different order + (['b', 'a', 'a'], ['a', 'b'], ['b', 'a'],), + # NaNs + (['a', 'b', 'c'], ['a', 'b'], ['a', 'b']), + (['a', 'b', 'c'], ['a', 'b'], ['b', 'a']), + (['b', 'a', 'c'], ['a', 'b'], ['a', 'b']), + (['b', 'a', 'c'], ['a', 'b'], ['a', 'b']), + # Introduce NaNs + (['a', 'b', 'c'], ['a', 'b'], ['a']), + (['a', 'b', 'c'], ['a', 'b'], ['b']), + (['b', 'a', 'c'], ['a', 'b'], ['a']), + (['b', 'a', 'c'], ['a', 'b'], ['a']), + # No overlap + (['a', 'b', 'c'], ['a', 'b'], ['d', 'e']), + ]) + @pytest.mark.parametrize('ordered', [True, False]) + def test_set_dtype_many(self, values, categories, new_categories, + ordered): + c = Categorical(values, categories) + expected = Categorical(values, new_categories, ordered) + result = c._set_dtype(expected.dtype) + tm.assert_categorical_equal(result, expected) + + def test_set_dtype_no_overlap(self): + c = Categorical(['a', 'b', 'c'], ['d', 'e']) + result = c._set_dtype(CategoricalDtype(['a', 'b'])) + expected = Categorical([None, None, None], categories=['a', 'b']) + tm.assert_categorical_equal(result, expected) + def test_set_ordered(self): cat = Categorical(["a", "b", "c", "a"], ordered=True) @@ -964,6 +1141,40 @@ def test_rename_categories(self): with pytest.raises(ValueError): cat.rename_categories([1, 2]) + def test_rename_categories_dict(self): + # GH 17336 + cat = pd.Categorical(['a', 'b', 'c', 'd']) + res = cat.rename_categories({'a': 4, 'b': 3, 'c': 2, 'd': 1}) + expected = Index([4, 3, 2, 1]) + tm.assert_index_equal(res.categories, expected) + + # Test for inplace + res = cat.rename_categories({'a': 4, 'b': 3, 'c': 2, 'd': 1}, + inplace=True) + assert res is None + tm.assert_index_equal(cat.categories, expected) + + # Test for dicts of smaller length + cat = pd.Categorical(['a', 'b', 'c', 'd']) + res = cat.rename_categories({'a': 1, 'c': 3}) + + expected = Index([1, 'b', 3, 'd']) + tm.assert_index_equal(res.categories, expected) + + # Test for dicts with bigger length + cat = pd.Categorical(['a', 'b', 'c', 'd']) + res = cat.rename_categories({'a': 1, 'b': 2, 'c': 3, + 'd': 4, 'e': 5, 'f': 6}) + expected = Index([1, 2, 3, 4]) + tm.assert_index_equal(res.categories, expected) + + # Test for dicts with no items from old categories + cat = pd.Categorical(['a', 'b', 'c', 'd']) + res = cat.rename_categories({'f': 1, 'g': 3}) + + expected = Index(['a', 'b', 'c', 'd']) + tm.assert_index_equal(res.categories, expected) + @pytest.mark.parametrize('codes, old, new, expected', [ ([0, 1], ['a', 'b'], ['a', 'b'], [0, 1]), ([0, 1], ['b', 'a'], ['b', 'a'], [0, 1]), @@ -1507,7 +1718,7 @@ def test_shift(self): def test_nbytes(self): cat = pd.Categorical([1, 2, 3]) - exp = cat._codes.nbytes + cat._categories.values.nbytes + exp = 3 + 3 * 8 # 3 int8s for values + 3 int64s for categories assert cat.nbytes == exp def test_memory_usage(self): @@ -1681,6 +1892,13 @@ def test_validate_inplace(self): with pytest.raises(ValueError): cat.sort_values(inplace=value) + @pytest.mark.xfail(reason="Imaginary values not supported in Categorical") + def test_imaginary(self): + values = [1, 2, 3 + 1j] + c1 = pd.Categorical(values) + tm.assert_index_equal(c1.categories, pd.Index(values)) + tm.assert_numpy_array_equal(np.array(c1), np.array(values)) + class TestCategoricalAsBlock(object): @@ -2113,15 +2331,18 @@ def test_assignment_to_dataframe(self): result = df.dtypes expected = Series( - [np.dtype('int32'), CategoricalDtype()], index=['value', 'D']) + [np.dtype('int32'), CategoricalDtype(categories=labels, + ordered=False)], + index=['value', 'D']) tm.assert_series_equal(result, expected) df['E'] = s str(df) result = df.dtypes - expected = Series([np.dtype('int32'), CategoricalDtype(), - CategoricalDtype()], + expected = Series([np.dtype('int32'), + CategoricalDtype(categories=labels, ordered=False), + CategoricalDtype(categories=labels, ordered=False)], index=['value', 'D', 'E']) tm.assert_series_equal(result, expected) @@ -4031,7 +4252,7 @@ def test_categorical_index_preserver(self): # wrong catgories df3 = DataFrame({'A': a, - 'B': pd.Categorical(b, categories=list('abc')) + 'B': pd.Categorical(b, categories=list('abe')) }).set_index('B') pytest.raises(TypeError, lambda: pd.concat([df2, df3])) diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py index d42e37048d87f..28a68a0a6e36d 100644 --- a/pandas/tests/test_resample.py +++ b/pandas/tests/test_resample.py @@ -1329,6 +1329,14 @@ def test_upsample_with_limit(self): expected = ts.reindex(result.index, method='ffill', limit=2) assert_series_equal(result, expected) + def test_nearest_upsample_with_limit(self): + rng = date_range('1/1/2000', periods=3, freq='5t') + ts = Series(np.random.randn(len(rng)), rng) + + result = ts.resample('t').nearest(limit=2) + expected = ts.reindex(result.index, method='nearest', limit=2) + assert_series_equal(result, expected) + def test_resample_ohlc(self): s = self.series @@ -2934,6 +2942,24 @@ def test_getitem_multiple(self): result = r['buyer'].count() assert_series_equal(result, expected) + def test_nearest(self): + + # GH 17496 + # Resample nearest + index = pd.date_range('1/1/2000', periods=3, freq='T') + result = pd.Series(range(3), index=index).resample('20s').nearest() + + expected = pd.Series( + np.array([0, 0, 1, 1, 1, 2, 2]), + index=pd.DatetimeIndex( + ['2000-01-01 00:00:00', '2000-01-01 00:00:20', + '2000-01-01 00:00:40', '2000-01-01 00:01:00', + '2000-01-01 00:01:20', '2000-01-01 00:01:40', + '2000-01-01 00:02:00'], + dtype='datetime64[ns]', + freq='20S')) + assert_series_equal(result, expected) + def test_methods(self): g = self.frame.groupby('A') r = g.resample('2s') @@ -2960,7 +2986,7 @@ def test_methods(self): expected = g.B.apply(lambda x: getattr(x.resample('2s'), f)()) assert_series_equal(result, expected) - for f in ['backfill', 'ffill', 'asfreq']: + for f in ['nearest', 'backfill', 'ffill', 'asfreq']: result = getattr(r, f)() expected = g.apply(lambda x: getattr(x.resample('2s'), f)()) assert_frame_equal(result, expected) diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index 1cc0ad8bb4041..0fe51121abef6 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -1175,7 +1175,7 @@ def test_rolling_quantile_np_percentile(self): # is analogus to Numpy's percentile row = 10 col = 5 - idx = pd.date_range(20100101, periods=row, freq='B') + idx = pd.date_range('20100101', periods=row, freq='B') df = pd.DataFrame(np.random.rand(row * col).reshape((row, -1)), index=idx) diff --git a/pandas/tests/tseries/test_offsets.py b/pandas/tests/tseries/test_offsets.py index cd2c29ffe3ac6..543d21e162f04 100644 --- a/pandas/tests/tseries/test_offsets.py +++ b/pandas/tests/tseries/test_offsets.py @@ -33,6 +33,7 @@ to_datetime, DateParseError) import pandas.tseries.offsets as offsets from pandas.io.pickle import read_pickle +from pandas._libs.tslibs import timezones from pandas._libs.tslib import normalize_date, NaT, Timestamp, Timedelta import pandas._libs.tslib as tslib import pandas.util.testing as tm @@ -288,7 +289,7 @@ def _check_offsetfunc_works(self, offset, funcname, dt, expected, for tz in self.timezones: expected_localize = expected.tz_localize(tz) - tz_obj = tslib.maybe_get_tz(tz) + tz_obj = timezones.maybe_get_tz(tz) dt_tz = tslib._localize_pydatetime(dt, tz_obj) result = func(dt_tz) diff --git a/pandas/tests/tseries/test_timezones.py b/pandas/tests/tseries/test_timezones.py index a9ecfd797a32b..e7b470e01e2af 100644 --- a/pandas/tests/tseries/test_timezones.py +++ b/pandas/tests/tseries/test_timezones.py @@ -18,6 +18,7 @@ from pandas.core.indexes.datetimes import bdate_range, date_range from pandas.core.dtypes.dtypes import DatetimeTZDtype from pandas._libs import tslib +from pandas._libs.tslibs import timezones from pandas import (Index, Series, DataFrame, isna, Timestamp, NaT, DatetimeIndex, to_datetime) from pandas.util.testing import (assert_frame_equal, assert_series_equal, @@ -943,7 +944,7 @@ def tz(self, tz): Use tslib.maybe_get_tz so that we get the filename on the tz right on windows. See #7337. """ - return tslib.maybe_get_tz('dateutil/' + tz) + return timezones.maybe_get_tz('dateutil/' + tz) def tzstr(self, tz): """ Construct a timezone string from a string. Overridden in subclass @@ -962,7 +963,7 @@ def test_utc_with_system_utc(self): # Skipped on win32 due to dateutil bug tm._skip_if_windows() - from pandas._libs.tslib import maybe_get_tz + from pandas._libs.tslibs.timezones import maybe_get_tz # from system utc to real utc ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC')) @@ -1133,7 +1134,7 @@ def test_tzlocal(self): assert ts.tz == dateutil.tz.tzlocal() assert "tz='tzlocal()')" in repr(ts) - tz = tslib.maybe_get_tz('tzlocal()') + tz = timezones.maybe_get_tz('tzlocal()') assert tz == dateutil.tz.tzlocal() # get offset using normal datetime for test @@ -1176,12 +1177,13 @@ def test_cache_keys_are_distinct_for_pytz_vs_dateutil(self): if tz_name == 'UTC': # skip utc as it's a special case in dateutil continue - tz_p = tslib.maybe_get_tz(tz_name) - tz_d = tslib.maybe_get_tz('dateutil/' + tz_name) + tz_p = timezones.maybe_get_tz(tz_name) + tz_d = timezones.maybe_get_tz('dateutil/' + tz_name) if tz_d is None: # skip timezones that dateutil doesn't know about. continue - assert tslib._p_tz_cache_key(tz_p) != tslib._p_tz_cache_key(tz_d) + assert (timezones._p_tz_cache_key(tz_p) != + timezones._p_tz_cache_key(tz_d)) class TestTimeZones(object): @@ -1269,6 +1271,27 @@ def test_ambiguous_compat(self): assert (result_pytz.to_pydatetime().tzname() == result_dateutil.to_pydatetime().tzname()) + def test_replace_tzinfo(self): + # GH 15683 + dt = datetime(2016, 3, 27, 1) + tzinfo = pytz.timezone('CET').localize(dt, is_dst=False).tzinfo + + result_dt = dt.replace(tzinfo=tzinfo) + result_pd = Timestamp(dt).replace(tzinfo=tzinfo) + + if hasattr(result_dt, 'timestamp'): # New method in Py 3.3 + assert result_dt.timestamp() == result_pd.timestamp() + assert result_dt == result_pd + assert result_dt == result_pd.to_pydatetime() + + result_dt = dt.replace(tzinfo=tzinfo).replace(tzinfo=None) + result_pd = Timestamp(dt).replace(tzinfo=tzinfo).replace(tzinfo=None) + + if hasattr(result_dt, 'timestamp'): # New method in Py 3.3 + assert result_dt.timestamp() == result_pd.timestamp() + assert result_dt == result_pd + assert result_dt == result_pd.to_pydatetime() + def test_index_equals_with_tz(self): left = date_range('1/1/2011', periods=100, freq='H', tz='utc') right = date_range('1/1/2011', periods=100, freq='H', tz='US/Eastern') @@ -1743,13 +1766,13 @@ def compare_local_to_utc(tz_didx, utc_didx): # Check empty array result = tslib.tz_convert(np.array([], dtype=np.int64), - tslib.maybe_get_tz('US/Eastern'), - tslib.maybe_get_tz('Asia/Tokyo')) + timezones.maybe_get_tz('US/Eastern'), + timezones.maybe_get_tz('Asia/Tokyo')) tm.assert_numpy_array_equal(result, np.array([], dtype=np.int64)) # Check all-NaT array result = tslib.tz_convert(np.array([tslib.iNaT], dtype=np.int64), - tslib.maybe_get_tz('US/Eastern'), - tslib.maybe_get_tz('Asia/Tokyo')) + timezones.maybe_get_tz('US/Eastern'), + timezones.maybe_get_tz('Asia/Tokyo')) tm.assert_numpy_array_equal(result, np.array( [tslib.iNaT], dtype=np.int64)) diff --git a/pandas/tests/util/test_util.py b/pandas/tests/util/test_util.py index abd82cfa89f94..ffc9703abff41 100644 --- a/pandas/tests/util/test_util.py +++ b/pandas/tests/util/test_util.py @@ -9,7 +9,7 @@ import pytest from pandas.compat import intern from pandas.util._move import move_into_mutable_buffer, BadMove, stolenbuf -from pandas.util._decorators import deprecate_kwarg +from pandas.util._decorators import deprecate_kwarg, make_signature from pandas.util._validators import (validate_args, validate_kwargs, validate_args_and_kwargs, validate_bool_kwarg) @@ -467,3 +467,17 @@ def test_set_locale(self): current_locale = locale.getlocale() assert current_locale == self.current_locale + + +def test_make_signature(): + # See GH 17608 + # Case where the func does not have default kwargs + sig = make_signature(validate_kwargs) + assert sig == (['fname', 'kwargs', 'compat_args'], + ['fname', 'kwargs', 'compat_args']) + + # Case where the func does have default kwargs + sig = make_signature(deprecate_kwarg) + assert sig == (['old_arg_name', 'new_arg_name', + 'mapping=None', 'stacklevel=2'], + ['old_arg_name', 'new_arg_name', 'mapping', 'stacklevel']) From 10c91778b7a01bb20107d240b98a9a9a8598facb Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Tue, 26 Sep 2017 07:55:58 -0400 Subject: [PATCH 43/56] fix merge conflicts --- pandas/tests/indexes/test_interval.py | 68 --------------------------- 1 file changed, 68 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index dfc919ad62f5c..4006d75bd8fc5 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -299,7 +299,6 @@ def test_unique(self): def test_monotonic(self): # increasing non-overlapping idx = IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)]) -<<<<<<< HEAD assert idx.is_monotonic assert idx._is_strictly_monotonic_increasing assert not idx.is_monotonic_decreasing @@ -365,73 +364,6 @@ def test_monotonic(self): idx = IntervalIndex([]) assert idx.is_monotonic assert idx._is_strictly_monotonic_increasing -======= - assert idx.is_monotonic - assert idx._is_strictly_monotonic_increasing - assert not idx.is_monotonic_decreasing - assert not idx._is_strictly_monotonic_decreasing - - # decreasing non-overlapping - idx = IntervalIndex.from_tuples([(4, 5), (2, 3), (1, 2)]) - assert not idx.is_monotonic - assert not idx._is_strictly_monotonic_increasing - assert idx.is_monotonic_decreasing - assert idx._is_strictly_monotonic_decreasing - - # unordered non-overlapping - idx = IntervalIndex.from_tuples([(0, 1), (4, 5), (2, 3)]) - assert not idx.is_monotonic - assert not idx._is_strictly_monotonic_increasing - assert not idx.is_monotonic_decreasing - assert not idx._is_strictly_monotonic_decreasing - - # increasing overlapping - idx = IntervalIndex.from_tuples([(0, 2), (0.5, 2.5), (1, 3)]) - assert idx.is_monotonic - assert idx._is_strictly_monotonic_increasing - assert not idx.is_monotonic_decreasing - assert not idx._is_strictly_monotonic_decreasing - - # decreasing overlapping - idx = IntervalIndex.from_tuples([(1, 3), (0.5, 2.5), (0, 2)]) - assert not idx.is_monotonic - assert not idx._is_strictly_monotonic_increasing - assert idx.is_monotonic_decreasing - assert idx._is_strictly_monotonic_decreasing - - # unordered overlapping - idx = IntervalIndex.from_tuples([(0.5, 2.5), (0, 2), (1, 3)]) - assert not idx.is_monotonic - assert not idx._is_strictly_monotonic_increasing - assert not idx.is_monotonic_decreasing - assert not idx._is_strictly_monotonic_decreasing - - # increasing overlapping shared endpoints - idx = pd.IntervalIndex.from_tuples([(1, 2), (1, 3), (2, 3)]) - assert idx.is_monotonic - assert idx._is_strictly_monotonic_increasing - assert not idx.is_monotonic_decreasing - assert not idx._is_strictly_monotonic_decreasing - - # decreasing overlapping shared endpoints - idx = pd.IntervalIndex.from_tuples([(2, 3), (1, 3), (1, 2)]) - assert not idx.is_monotonic - assert not idx._is_strictly_monotonic_increasing - assert idx.is_monotonic_decreasing - assert idx._is_strictly_monotonic_decreasing - - # stationary - idx = IntervalIndex.from_tuples([(0, 1), (0, 1)]) - assert idx.is_monotonic - assert not idx._is_strictly_monotonic_increasing - assert idx.is_monotonic_decreasing - assert not idx._is_strictly_monotonic_decreasing - - # empty - idx = IntervalIndex([]) - assert idx.is_monotonic - assert idx._is_strictly_monotonic_increasing ->>>>>>> pandas-dev/master assert idx.is_monotonic_decreasing assert idx._is_strictly_monotonic_decreasing From c8be3e6e45df294a44c61f8c9cb88bea15a4b098 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 29 Sep 2017 18:36:02 +0200 Subject: [PATCH 44/56] parametrize a little bit more --- pandas/tests/indexes/test_interval_new.py | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/test_interval_new.py index 274a16e807076..0895e0059672f 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/test_interval_new.py @@ -25,26 +25,26 @@ def _compare_tuple_of_numpy_array(self, result, expected): @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither']) - def test_get_loc_interval_updated_behavior(self, idx_side): + @pytest.mark.parametrize("side", ['right', 'left', 'both', 'neither']) + def test_get_loc_interval_updated_behavior(self, idx_side, side): idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) for bound in [[0, 1], [1, 2], [2, 3], [3, 4], [0, 2], [2.5, 3], [-1, 4]]: - for side in ['right', 'left', 'both', 'neither']: - # if get_loc is supplied an interval, it should only search - # for exact matches, not overlaps or covers, else KeyError. - if idx_side == side: - if bound == [0, 1]: - assert idx.get_loc(Interval(0, 1, closed=side)) == 0 - elif bound == [2, 3]: - assert idx.get_loc(Interval(2, 3, closed=side)) == 1 - else: - pytest.raises(KeyError, idx.get_loc, - Interval(*bound, closed=side)) + # if get_loc is supplied an interval, it should only search + # for exact matches, not overlaps or covers, else KeyError. + if idx_side == side: + if bound == [0, 1]: + assert idx.get_loc(Interval(0, 1, closed=side)) == 0 + elif bound == [2, 3]: + assert idx.get_loc(Interval(2, 3, closed=side)) == 1 else: - pytest.raises(KeyError, idx.get_loc, - Interval(*bound, closed=side)) + with pytest.raises(KeyError): + idx.get_loc(Interval(*bound, closed=side)) + else: + with pytest.raises(KeyError): + idx.get_loc(Interval(*bound, closed=side)) @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither']) From 93f1742b40351c3bb6f8faed9f7389149c58330a Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 29 Sep 2017 18:42:01 +0200 Subject: [PATCH 45/56] remove _updated_behaviour from test name + PEP8 (unused imports) --- pandas/tests/indexes/test_interval_new.py | 29 +++++++++------------- pandas/tests/indexing/test_interval_new.py | 14 ++++++----- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/test_interval_new.py index 0895e0059672f..89509f6af2feb 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/test_interval_new.py @@ -3,18 +3,13 @@ import pytest import numpy as np -from datetime import timedelta -from pandas import (Interval, IntervalIndex, Index, Int64Index, isna, - interval_range, Timestamp, Timedelta, - compat, date_range, timedelta_range, DateOffset) -from pandas.tseries.offsets import Day -from pandas._libs.interval import IntervalTree +from pandas import (Interval, IntervalIndex, Int64Index, + Timestamp, Timedelta) from pandas.tests.indexes.common import Base import pandas.util.testing as tm -import pandas as pd -class TestIntervalIndex_new(Base): +class TestIntervalIndex(Base): def _compare_tuple_of_numpy_array(self, result, expected): lidx, ridx = result @@ -26,7 +21,7 @@ def _compare_tuple_of_numpy_array(self, result, expected): @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither']) @pytest.mark.parametrize("side", ['right', 'left', 'both', 'neither']) - def test_get_loc_interval_updated_behavior(self, idx_side, side): + def test_get_loc_interval(self, idx_side, side): idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) @@ -48,7 +43,7 @@ def test_get_loc_interval_updated_behavior(self, idx_side, side): @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither']) - def test_get_loc_scalar_updated_behavior(self, idx_side): + def test_get_loc_scalar(self, idx_side): scalars = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5] correct = {'right': {0.5: 0, 1: 0, 2.5: 1, 3: 1}, @@ -67,7 +62,7 @@ def test_get_loc_scalar_updated_behavior(self, idx_side): pytest.raises(KeyError, idx.get_loc, scalar) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_slice_locs_with_interval_updated_behavior(self): + def test_slice_locs_with_interval(self): # increasing monotonically index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) @@ -125,7 +120,7 @@ def test_slice_locs_with_interval_updated_behavior(self): start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_slice_locs_with_ints_and_floats_updated_behavior(self): + def test_slice_locs_with_ints_and_floats(self): queries = [[0, 1], [0, 2], [0, 3], [3, 1], [3, 4], [0, 4]] @@ -174,7 +169,7 @@ def test_slice_locs_with_ints_and_floats_updated_behavior(self): pytest.raises(KeyError, index.slice_locs, query) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_indexer_for_interval_updated_behavior(self): + def test_get_indexer_with_interval(self): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='right') @@ -208,7 +203,7 @@ def test_get_indexer_for_interval_updated_behavior(self): tm.assert_numpy_array_equal(result, expect) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_indexer_for_ints_and_floats_updated_behavior(self): + def test_get_indexer_with_ints_and_floats(self): index = IntervalIndex.from_tuples( [(0, 1), (1, 2), (3, 4)], closed='right') @@ -235,7 +230,7 @@ def test_get_indexer_for_ints_and_floats_updated_behavior(self): # TODO: @shoyer believes this should raise, master branch doesn't @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): + def test_get_indexer_non_unique_with_ints_and_floats(self): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='left') @@ -276,7 +271,7 @@ def test_get_indexer_non_unique_for_ints_and_floats_updated_behavior(self): # the intervals are duplicated, decreasing, non-monotonic, etc.. @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_contains_updated_behavior(self): + def test_contains(self): index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right') @@ -294,7 +289,7 @@ def test_contains_updated_behavior(self): assert Interval(0, 1, closed='both') not in index @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_contains_method_updated_behavior(self): + def test_contains_method(self): index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right') diff --git a/pandas/tests/indexing/test_interval_new.py b/pandas/tests/indexing/test_interval_new.py index 75f9a767587ed..5df3bf1d3519b 100644 --- a/pandas/tests/indexing/test_interval_new.py +++ b/pandas/tests/indexing/test_interval_new.py @@ -2,15 +2,17 @@ import numpy as np import pandas as pd -from pandas import Series, DataFrame, IntervalIndex, Interval -from pandas.compat import product +from pandas import Series, IntervalIndex, Interval import pandas.util.testing as tm -class TestIntervalIndex_new(object): +class TestIntervalIndex(object): + + def setup_method(self, method): + self.s = Series(np.arange(5), IntervalIndex.from_breaks(np.arange(6))) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_with_slices_updated_behavior(self): + def test_with_slices(self): s = self.s @@ -57,7 +59,7 @@ def test_with_slices_updated_behavior(self): s[0:4:2] @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_with_overlaps_updated_behavior(self): + def test_with_overlaps(self): idx = IntervalIndex.from_tuples([(1, 5), (3, 7)]) s = Series(range(len(idx)), index=idx) @@ -101,7 +103,7 @@ def test_non_unique(self): tm.assert_series_equal(expected, result) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_non_unique_moar_updated_behavior(self): + def test_non_unique_moar(self): idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) s = Series(range(len(idx)), index=idx) From 3464883106a0fa248229cb0c14af0a8dd6784aab Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sat, 30 Sep 2017 23:46:48 +0200 Subject: [PATCH 46/56] update loc tests --- pandas/tests/indexing/test_interval.py | 4 +- pandas/tests/indexing/test_interval_new.py | 167 ++++++++++++++++++--- 2 files changed, 148 insertions(+), 23 deletions(-) diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index b114e7fcd05f9..8fb4e76052283 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -12,7 +12,7 @@ class TestIntervalIndex(object): def setup_method(self, method): self.s = Series(np.arange(5), IntervalIndex.from_breaks(np.arange(6))) - # TODO: check this behavior is consistent + # To be removed (see #16316, #16386) def test_loc_with_scalar(self): s = self.s @@ -86,7 +86,7 @@ def test_nonoverlapping_monotonic(self, direction, closed): assert s[key] == expected assert s.loc[key] == expected - # TODO: check this behavior is consistent + # To be removed (see #16316, #16386) def test_with_interval(self): s = self.s diff --git a/pandas/tests/indexing/test_interval_new.py b/pandas/tests/indexing/test_interval_new.py index 5df3bf1d3519b..6b94adf3e5423 100644 --- a/pandas/tests/indexing/test_interval_new.py +++ b/pandas/tests/indexing/test_interval_new.py @@ -12,16 +12,106 @@ def setup_method(self, method): self.s = Series(np.arange(5), IntervalIndex.from_breaks(np.arange(6))) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_with_slices(self): + def test_loc_with_interval(self): + + # loc with single label / list of labels: + # - Intervals: only exact matches + # - scalars: those that contain it + + s = self.s + + expected = 0 + result = s.loc[Interval(0, 1)] + assert result == expected + result = s[Interval(0, 1)] + assert result == expected + + expected = s.iloc[3:5] + result = s.loc[[Interval(3, 4), Interval(4, 5)]] + tm.assert_series_equal(expected, result) + result = s[[Interval(3, 4), Interval(4, 5)]] + tm.assert_series_equal(expected, result) + + # missing or not exact + with pytest.raises(KeyError): + s.loc[Interval(3, 5, closed='left')] + + with pytest.raises(KeyError): + s[Interval(3, 5, closed='left')] + + with pytest.raises(KeyError): + s[Interval(3, 5)] + + with pytest.raises(KeyError): + s.loc[Interval(3, 5)] + + with pytest.raises(KeyError): + s[Interval(3, 5)] + + with pytest.raises(KeyError): + s.loc[Interval(-2, 0)] + + with pytest.raises(KeyError): + s[Interval(-2, 0)] + + with pytest.raises(KeyError): + s.loc[Interval(5, 6)] + + with pytest.raises(KeyError): + s[Interval(5, 6)] + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_loc_with_scalar(self): + + # loc with single label / list of labels: + # - Intervals: only exact matches + # - scalars: those that contain it + + s = self.s + + assert s.loc[1] == 0 + assert s.loc[1.5] == 1 + assert s.loc[2] == 1 + + # TODO with __getitem__ same rules as loc, or positional ? + # assert s[1] == 0 + # assert s[1.5] == 1 + # assert s[2] == 1 + + expected = s.iloc[1:4] + tm.assert_series_equal(expected, s.loc[[1.5, 2.5, 3.5]]) + tm.assert_series_equal(expected, s.loc[[2, 3, 4]]) + tm.assert_series_equal(expected, s.loc[[1.5, 3, 4]]) + + expected = s.iloc[[1, 1, 2, 1]] + tm.assert_series_equal(expected, s.loc[[1.5, 2, 2.5, 1.5]]) + + expected = s.iloc[2:5] + tm.assert_series_equal(expected, s.loc[s >= 2]) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + def test_loc_with_slices(self): + + # loc with slices: + # - Interval objects: only works with exact matches + # - scalars: only works for non-overlapping, monotonic intervals, + # and start/stop select location based on the interval that + # contains them: + # (slice_loc(start, stop) == (idx.get_loc(start), idx.get_loc(stop)) s = self.s # slice of interval - expected = s.iloc[4:] - result = s.loc[Interval(3, 4):] + + expected = s.iloc[:3] + result = s.loc[Interval(0, 1):Interval(2, 3)] + tm.assert_series_equal(expected, result) + result = s[Interval(0, 1):Interval(2, 3)] tm.assert_series_equal(expected, result) expected = s.iloc[4:] + result = s.loc[Interval(3, 4):] + tm.assert_series_equal(expected, result) result = s[Interval(3, 4):] tm.assert_series_equal(expected, result) @@ -37,58 +127,93 @@ def test_with_slices(self): with pytest.raises(KeyError): s[Interval(3, 4, closed='left'):] - with pytest.raises(KeyError): - s.loc[Interval(3, 4, closed='both'):] + # TODO with non-existing intervals ? + # s.loc[Interval(-1, 0):Interval(2, 3)] - with pytest.raises(NotImplementedError): - s.loc[Interval(3, 6):] + # slice of scalar - with pytest.raises(NotImplementedError): - s[Interval(3, 6):] + expected = s.iloc[:3] + tm.assert_series_equal(expected, s.loc[:3]) + tm.assert_series_equal(expected, s.loc[:2.5]) + tm.assert_series_equal(expected, s.loc[0.1:2.5]) - with pytest.raises(KeyError): - s[Interval(3, 4, closed='both'):] + # TODO should this work? (-1 is not contained in any of the Intervals) + # tm.assert_series_equal(expected, s.loc[-1:3]) - # slice of scalar - expected = s.iloc[:4] # maybe [:5] ? - result = s[0:4] - tm.assert_series_equal(expected, result) + # TODO with __getitem__ same rules as loc, or positional ? + # tm.assert_series_equal(expected, s[:3]) + # tm.assert_series_equal(expected, s[:2.5]) + # tm.assert_series_equal(expected, s[0.1:2.5]) # slice of scalar with step != 1 with pytest.raises(NotImplementedError): s[0:4:2] @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_with_overlaps(self): + def test_loc_with_overlap(self): idx = IntervalIndex.from_tuples([(1, 5), (3, 7)]) s = Series(range(len(idx)), index=idx) # scalar expected = s + result = s.loc[4] + tm.assert_series_equal(expected, result) + result = s[4] tm.assert_series_equal(expected, result) - expected = s + result = s.loc[[4]] + tm.assert_series_equal(expected, result) + result = s[[4]] tm.assert_series_equal(expected, result) + # interval + expected = 0 + result = s.loc[pd.interval(1, 5)] + tm.assert_series_equal(expected, result) + + result = s[pd.interval(1, 5)] + tm.assert_series_equal(expected, result) + expected = s - result = s.loc[[4]] + result = s.loc[[pd.interval(1, 5), pd.Interval(3, 7)]] + tm.assert_series_equal(expected, result) + + result = s[[pd.interval(1, 5), pd.Interval(3, 7)]] tm.assert_series_equal(expected, result) - # interval + with pytest.raises(KeyError): + s.loc[Interval(3, 5)] + + with pytest.raises(KeyError): + s.loc[[Interval(3, 5)]] + with pytest.raises(KeyError): s[Interval(3, 5)] with pytest.raises(KeyError): s[[Interval(3, 5)]] + # slices with interval (only exact matches) + expected = s + result = s.loc[pd.interval(1, 5):pd.Interval(3, 7)] + tm.assert_series_equal(expected, result) + + result = s[pd.interval(1, 5):pd.Interval(3, 7)] + tm.assert_series_equal(expected, result) + with pytest.raises(KeyError): - s.loc[Interval(3, 5)] + s.loc[pd.interval(1, 6):pd.Interval(3, 8)] with pytest.raises(KeyError): - s.loc[[Interval(3, 5)]] + s[pd.interval(1, 6):pd.Interval(3, 8)] + + # slices with scalar raise for overlapping intervals + # TODO KeyError is the appropriate error? + with pytest.raises(KeyError): + s.loc[1:4] def test_non_unique(self): From e82ae3342ae85313243f90ffe3a71502613e9cf1 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Wed, 20 Dec 2017 10:34:38 -0800 Subject: [PATCH 47/56] update comments --- pandas/tests/indexes/test_interval.py | 30 +++++++++++++------------- pandas/tests/indexing/test_interval.py | 18 ++++++++-------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 4006d75bd8fc5..18ea89991cf13 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -395,7 +395,7 @@ def test_repr_max_seq_item_setting(self): def test_repr_roundtrip(self): super(TestIntervalIndex, self).test_repr_roundtrip() - # TODO: check this behavior is consistent + # TODO: check this behavior is consistent with test_interval_new.py def test_get_item(self): i = IntervalIndex.from_arrays((0, 1, np.nan), (1, 2, np.nan), closed='right') @@ -416,7 +416,7 @@ def test_get_item(self): closed='right') tm.assert_index_equal(result, expected) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_get_loc_value(self): pytest.raises(KeyError, self.index.get_loc, 0) assert self.index.get_loc(0.5) == 0 @@ -439,7 +439,7 @@ def test_get_loc_value(self): idx = IntervalIndex.from_arrays([0, 2], [1, 3]) pytest.raises(KeyError, idx.get_loc, 1.5) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def slice_locs_cases(self, breaks): # TODO: same tests for more index types index = IntervalIndex.from_breaks([0, 1, 2], closed='right') @@ -468,15 +468,15 @@ def slice_locs_cases(self, breaks): assert index.slice_locs(1, 1) == (0, 1) assert index.slice_locs(1, 2) == (0, 2) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_slice_locs_int64(self): self.slice_locs_cases([0, 1, 2]) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_slice_locs_float64(self): self.slice_locs_cases([0.0, 1.0, 2.0]) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def slice_locs_decreasing_cases(self, tuples): index = IntervalIndex.from_tuples(tuples) assert index.slice_locs(1.5, 0.5) == (1, 3) @@ -490,21 +490,21 @@ def slice_locs_decreasing_cases(self, tuples): slice_locs = index.slice_locs(-1, -1) assert slice_locs[0] == slice_locs[1] - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_slice_locs_decreasing_int64(self): self.slice_locs_cases([(2, 4), (1, 3), (0, 2)]) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_slice_locs_decreasing_float64(self): self.slice_locs_cases([(2., 4.), (1., 3.), (0., 2.)]) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_slice_locs_fails(self): index = IntervalIndex.from_tuples([(1, 2), (0, 1), (2, 3)]) with pytest.raises(KeyError): index.slice_locs(1, 2) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_get_loc_interval(self): assert self.index.get_loc(Interval(0, 1)) == 0 assert self.index.get_loc(Interval(0, 0.5)) == 0 @@ -513,7 +513,7 @@ def test_get_loc_interval(self): pytest.raises(KeyError, self.index.get_loc, Interval(-1, 0, 'left')) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_get_indexer(self): actual = self.index.get_indexer([-1, 0, 0.5, 1, 1.5, 2, 3]) expected = np.array([-1, -1, 0, 0, 1, 1, -1], dtype='intp') @@ -536,7 +536,7 @@ def test_get_indexer(self): expected = np.array([-1, 1], dtype='intp') tm.assert_numpy_array_equal(actual, expected) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_get_indexer_subintervals(self): # TODO: is this right? @@ -560,7 +560,7 @@ def test_get_indexer_subintervals(self): expected = np.array([0, 0, 0], dtype='intp') tm.assert_numpy_array_equal(actual, expected) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_contains(self): # Only endpoints are valid. i = IntervalIndex.from_arrays([0, 1], [1, 2]) @@ -577,7 +577,7 @@ def test_contains(self): assert Interval(3, 5) not in i assert Interval(-1, 0, closed='left') not in i - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def testcontains(self): # can select values that are IN the range of a value i = IntervalIndex.from_arrays([0, 1], [1, 2]) @@ -607,7 +607,7 @@ def test_dropna(self): result = ii.dropna() tm.assert_index_equal(result, expected) - # TODO: check this behavior is consistent + # TODO: check this behavior is consistent with test_interval_new.py def test_non_contiguous(self): index = IntervalIndex.from_tuples([(0, 1), (2, 3)]) target = [0.5, 1.5, 2.5] diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/test_interval.py index 8fb4e76052283..a5a0c19e2ba31 100644 --- a/pandas/tests/indexing/test_interval.py +++ b/pandas/tests/indexing/test_interval.py @@ -12,7 +12,7 @@ class TestIntervalIndex(object): def setup_method(self, method): self.s = Series(np.arange(5), IntervalIndex.from_breaks(np.arange(6))) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_loc_with_scalar(self): s = self.s @@ -31,7 +31,7 @@ def test_loc_with_scalar(self): expected = s.iloc[2:5] tm.assert_series_equal(expected, s.loc[s >= 2]) - # TODO: check this behavior is consistent + # TODO: check this behavior is consistent with test_interval_new.py def test_getitem_with_scalar(self): s = self.s @@ -50,7 +50,7 @@ def test_getitem_with_scalar(self): expected = s.iloc[2:5] tm.assert_series_equal(expected, s[s >= 2]) - # TODO: check this behavior is consistent + # TODO: check this behavior is consistent with test_interval_new.py @pytest.mark.parametrize('direction, closed', product(('increasing', 'decreasing'), ('left', 'right', 'neither', 'both'))) @@ -86,7 +86,7 @@ def test_nonoverlapping_monotonic(self, direction, closed): assert s[key] == expected assert s.loc[key] == expected - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_with_interval(self): s = self.s @@ -123,7 +123,7 @@ def test_with_interval(self): with pytest.raises(KeyError): s[Interval(5, 6)] - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_with_slices(self): s = self.s @@ -143,7 +143,7 @@ def test_with_slices(self): with pytest.raises(ValueError): s[0:4:2] - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_with_overlaps(self): s = self.s @@ -181,7 +181,7 @@ def test_with_overlaps(self): with pytest.raises(KeyError): s.loc[[Interval(3, 5)]] - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_non_unique(self): idx = IntervalIndex.from_tuples([(1, 3), (3, 7)]) @@ -195,7 +195,7 @@ def test_non_unique(self): expected = s.iloc[0:1] tm.assert_series_equal(expected, result) - # To be removed (see #16316, #16386) + # To be removed, replaced by test_interval_new.py (see #16316, #16386) def test_non_unique_moar(self): idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) @@ -216,7 +216,7 @@ def test_non_unique_moar(self): with pytest.raises(ValueError): s[[Interval(1, 3)]] - # TODO: check this behavior is consistent + # TODO: check this behavior is consistent with test_interval_new.py def test_non_matching(self): s = self.s From 33ebe1bdcc4bb5c2982f5064b0ecedf15227796c Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Wed, 20 Dec 2017 11:02:16 -0800 Subject: [PATCH 48/56] worked on parameterization --- pandas/tests/indexes/test_interval_new.py | 169 +++++++++++----------- 1 file changed, 85 insertions(+), 84 deletions(-) diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/test_interval_new.py index 89509f6af2feb..fd328614270b2 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/test_interval_new.py @@ -43,9 +43,11 @@ def test_get_loc_interval(self, idx_side, side): @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither']) - def test_get_loc_scalar(self, idx_side): + @pytest.mark.parametrize("scalar", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]) + def test_get_loc_scalar(self, idx_side, scalar): - scalars = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5] + # correct = {side: {query: answer}}. + # If query is not in the dict, that query should raise a KeyError correct = {'right': {0.5: 0, 1: 0, 2.5: 1, 3: 1}, 'left': {0: 0, 0.5: 0, 2: 1, 2.5: 1}, 'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1}, @@ -53,13 +55,12 @@ def test_get_loc_scalar(self, idx_side): idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side) - for scalar in scalars: - # if get_loc is supplied a scalar, it should return the index of - # the interval which contains the scalar, or KeyError. - if scalar in correct[idx_side].keys(): - assert idx.get_loc(scalar) == correct[idx_side][scalar] - else: - pytest.raises(KeyError, idx.get_loc, scalar) + # if get_loc is supplied a scalar, it should return the index of + # the interval which contains the scalar, or KeyError. + if scalar in correct[idx_side].keys(): + assert idx.get_loc(scalar) == correct[idx_side][scalar] + else: + pytest.raises(KeyError, idx.get_loc, scalar) @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_slice_locs_with_interval(self): @@ -120,9 +121,7 @@ def test_slice_locs_with_interval(self): start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_slice_locs_with_ints_and_floats(self): - - queries = [[0, 1], [0, 2], [0, 3], [3, 1], [3, 4], [0, 4]] + def test_slice_locs_with_ints_and_floats_succeeds(self): # increasing non-overlapping index = IntervalIndex.from_tuples([(0, 1), (1, 2), (3, 4)]) @@ -143,101 +142,96 @@ def test_slice_locs_with_ints_and_floats(self): assert index.slice_locs(3, 4) == (1, 0) assert index.slice_locs(0, 4) == (3, 0) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + @pytest.mark.parametrize("query", [[0, 1], [0, 2], [0, 3], + [3, 1], [3, 4], [0, 4]]) + def test_slice_locs_with_ints_and_floats_fails(self, query): + # increasing overlapping index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) - for query in queries: - pytest.raises(KeyError, index.slice_locs, query) + pytest.raises(KeyError, index.slice_locs, query) # decreasing overlapping index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)]) - for query in queries: - pytest.raises(KeyError, index.slice_locs, query) + pytest.raises(KeyError, index.slice_locs, query) # sorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)]) - for query in queries: - pytest.raises(KeyError, index.slice_locs, query) + pytest.raises(KeyError, index.slice_locs, query) # unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)]) - for query in queries: - pytest.raises(KeyError, index.slice_locs, query) + pytest.raises(KeyError, index.slice_locs, query) # another unsorted duplicates index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) - for query in queries: - pytest.raises(KeyError, index.slice_locs, query) + pytest.raises(KeyError, index.slice_locs, query) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_indexer_with_interval(self): + @pytest.mark.parametrize("query_and_expected_result", [ + (Interval(1, 3, closed='right'), 1), + (Interval(1, 3, closed='left'), -1), + (Interval(1, 3, closed='both'), -1), + (Interval(1, 3, closed='neither'), -1), + (Interval(1, 4, closed='right'), -1), + (Interval(0, 4, closed='right'), -1), + (Interval(1, 2, closed='right'), -1)]) + def test_get_indexer_with_interval_single_queries(self, query_and_expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='right') - # single queries - queries = [Interval(1, 3, closed='right'), - Interval(1, 3, closed='left'), - Interval(1, 3, closed='both'), - Interval(1, 3, closed='neither'), - Interval(1, 4, closed='right'), - Interval(0, 4, closed='right'), - Interval(1, 2, closed='right')] - expected = [1, -1, -1, -1, -1, -1, -1] - - for query, expected_result in zip(queries, expected): - result = index.get_indexer([query]) - expect = np.array([expected_result], dtype='intp') - tm.assert_numpy_array_equal(result, expect) - - # multiple queries - queries = [ - [Interval(2, 4, closed='right'), Interval(1, 3, closed='right')], - [Interval(1, 3, closed='right'), Interval(0, 2, closed='right')], - [Interval(1, 3, closed='right'), Interval(1, 3, closed='left')], - index] - expected = [[2, 1], [1, -1], [1, -1], [0, 1, 2]] - - for query, expected_result in zip(queries, expected): - result = index.get_indexer(query) - expect = np.array(expected_result, dtype='intp') - tm.assert_numpy_array_equal(result, expect) + query, expected_result = query_and_expected_result + result = index.get_indexer([query]) + expect = np.array([expected_result], dtype='intp') + tm.assert_numpy_array_equal(result, expect) @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_indexer_with_ints_and_floats(self): + @pytest.mark.parametrize("query_and_expected_result", [ + ([Interval(2, 4, closed='right'), Interval(1, 3, closed='right')], [2, 1]), + ([Interval(1, 3, closed='right'), Interval(0, 2, closed='right')], [1, -1]), + ([Interval(1, 3, closed='right'), Interval(1, 3, closed='left')], [1, -1])]) + def test_get_indexer_with_interval_multiple_queries(self, query_and_expected_result): + + index = IntervalIndex.from_tuples( + [(0, 2.5), (1, 3), (2, 4)], closed='right') + + query, expected_result = query_and_expected_result + result = index.get_indexer(query) + expect = np.array(expected_result, dtype='intp') + tm.assert_numpy_array_equal(result, expect) + + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) + @pytest.mark.parametrize("expected_result", [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1]) + def test_get_indexer_with_ints_and_floats_single_queries(self, query, expected_result): index = IntervalIndex.from_tuples( [(0, 1), (1, 2), (3, 4)], closed='right') - # single queries - queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] - expected = [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1] + result = index.get_indexer([query]) + expect = np.array([expected_result], dtype='intp') + tm.assert_numpy_array_equal(result, expect) - for query, expected_result in zip(queries, expected): - result = index.get_indexer([query]) - expect = np.array([expected_result], dtype='intp') - tm.assert_numpy_array_equal(result, expect) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + @pytest.mark.parametrize("query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) + @pytest.mark.parametrize("expected_result", [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]]) + def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, expected_result): - # multiple queries - queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] - expected = [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]] + index = IntervalIndex.from_tuples( + [(0, 1), (1, 2), (3, 4)], closed='right') - for query, expected_result in zip(queries, expected): - result = index.get_indexer(query) - expect = np.array(expected_result, dtype='intp') - tm.assert_numpy_array_equal(result, expect) + result = index.get_indexer(query) + expect = np.array(expected_result, dtype='intp') + tm.assert_numpy_array_equal(result, expect) index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) # TODO: @shoyer believes this should raise, master branch doesn't @pytest.mark.xfail(reason="new indexing tests for issue 16316") - def test_get_indexer_non_unique_with_ints_and_floats(self): - - index = IntervalIndex.from_tuples( - [(0, 2.5), (1, 3), (2, 4)], closed='left') - - # single queries - queries = [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5] - expected = [(Int64Index([], dtype='int64'), np.array([0])) + @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) + @pytest.mark.parametrize("expected_result", [ + (Int64Index([], dtype='int64'), np.array([0])) (Int64Index([0], dtype='int64'), np.array([])) (Int64Index([0], dtype='int64'), np.array([])) (Int64Index([0, 1], dtype='int64'), np.array([])) @@ -247,25 +241,32 @@ def test_get_indexer_non_unique_with_ints_and_floats(self): (Int64Index([2], dtype='int64'), np.array([])) (Int64Index([2], dtype='int64'), np.array([])) (Int64Index([], dtype='int64'), np.array([0])) - (Int64Index([], dtype='int64'), np.array([0]))] + (Int64Index([], dtype='int64'), np.array([0]))]) + def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, query, expected_result): + + index = IntervalIndex.from_tuples( + [(0, 2.5), (1, 3), (2, 4)], closed='left') - for query, expected_result in zip(queries, expected): - result = index.get_indexer_non_unique([query]) - tm.assert_numpy_array_equal(result, expected_result) + result = index.get_indexer_non_unique([query]) + tm.assert_numpy_array_equal(result, expected_result) - # multiple queries - queries = [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]] - expected = [(Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])) + @pytest.mark.xfail(reason="new indexing tests for issue 16316") + @pytest.mark.parametrize("query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) + @pytest.mark.parametrize("expected_result", [ + (Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])) (Int64Index([0, 1, 0, 1, 2, 2], dtype='int64'), np.array([])) (Int64Index([0, 1, 0, 1, 2, 2, -1], dtype='int64'), np.array([3])) (Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], - dtype='int64'), np.array([3]))] + dtype='int64'), np.array([3]))]) + def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries(self, query, expected_result): + + index = IntervalIndex.from_tuples( + [(0, 2.5), (1, 3), (2, 4)], closed='left') - for query, expected_result in zip(queries, expected): - result = index.get_indexer_non_unique(query) - tm.assert_numpy_array_equal(result, expected_result) + result = index.get_indexer_non_unique(query) + tm.assert_numpy_array_equal(result, expected_result) # TODO we may also want to test get_indexer for the case when # the intervals are duplicated, decreasing, non-monotonic, etc.. From be050d6548aced9495eff57a1ce16d8bde5daf79 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 21 Dec 2017 11:51:35 -0800 Subject: [PATCH 49/56] move xfails to top of module --- pandas/tests/indexes/test_interval_new.py | 15 +-------------- pandas/tests/indexing/test_interval_new.py | 7 +------ 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/test_interval_new.py index fd328614270b2..55a2943db80e2 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/test_interval_new.py @@ -8,7 +8,7 @@ from pandas.tests.indexes.common import Base import pandas.util.testing as tm - +@pytest.mark.xfail(reason="new indexing tests for issue 16316") class TestIntervalIndex(Base): def _compare_tuple_of_numpy_array(self, result, expected): @@ -18,7 +18,6 @@ def _compare_tuple_of_numpy_array(self, result, expected): tm.assert_numpy_array_equal(lidx, lidx_expected) tm.assert_numpy_array_equal(ridx, ridx_expected) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither']) @pytest.mark.parametrize("side", ['right', 'left', 'both', 'neither']) def test_get_loc_interval(self, idx_side, side): @@ -41,7 +40,6 @@ def test_get_loc_interval(self, idx_side, side): with pytest.raises(KeyError): idx.get_loc(Interval(*bound, closed=side)) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither']) @pytest.mark.parametrize("scalar", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5]) def test_get_loc_scalar(self, idx_side, scalar): @@ -62,7 +60,6 @@ def test_get_loc_scalar(self, idx_side, scalar): else: pytest.raises(KeyError, idx.get_loc, scalar) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_slice_locs_with_interval(self): # increasing monotonically @@ -120,7 +117,6 @@ def test_slice_locs_with_interval(self): assert index.slice_locs( start=Interval(2, 4), end=Interval(0, 2)) == (2, 2) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_slice_locs_with_ints_and_floats_succeeds(self): # increasing non-overlapping @@ -142,7 +138,6 @@ def test_slice_locs_with_ints_and_floats_succeeds(self): assert index.slice_locs(3, 4) == (1, 0) assert index.slice_locs(0, 4) == (3, 0) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("query", [[0, 1], [0, 2], [0, 3], [3, 1], [3, 4], [0, 4]]) def test_slice_locs_with_ints_and_floats_fails(self, query): @@ -167,7 +162,6 @@ def test_slice_locs_with_ints_and_floats_fails(self, query): index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) pytest.raises(KeyError, index.slice_locs, query) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("query_and_expected_result", [ (Interval(1, 3, closed='right'), 1), (Interval(1, 3, closed='left'), -1), @@ -186,7 +180,6 @@ def test_get_indexer_with_interval_single_queries(self, query_and_expected_resul expect = np.array([expected_result], dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("query_and_expected_result", [ ([Interval(2, 4, closed='right'), Interval(1, 3, closed='right')], [2, 1]), ([Interval(1, 3, closed='right'), Interval(0, 2, closed='right')], [1, -1]), @@ -201,7 +194,6 @@ def test_get_indexer_with_interval_multiple_queries(self, query_and_expected_res expect = np.array(expected_result, dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) @pytest.mark.parametrize("expected_result", [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1]) def test_get_indexer_with_ints_and_floats_single_queries(self, query, expected_result): @@ -213,7 +205,6 @@ def test_get_indexer_with_ints_and_floats_single_queries(self, query, expected_r expect = np.array([expected_result], dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) @pytest.mark.parametrize("expected_result", [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]]) def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, expected_result): @@ -228,7 +219,6 @@ def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, expected index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) # TODO: @shoyer believes this should raise, master branch doesn't - @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) @pytest.mark.parametrize("expected_result", [ (Int64Index([], dtype='int64'), np.array([0])) @@ -250,7 +240,6 @@ def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, query, result = index.get_indexer_non_unique([query]) tm.assert_numpy_array_equal(result, expected_result) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") @pytest.mark.parametrize("query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) @pytest.mark.parametrize("expected_result", [ (Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])) @@ -271,7 +260,6 @@ def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries(self, quer # TODO we may also want to test get_indexer for the case when # the intervals are duplicated, decreasing, non-monotonic, etc.. - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_contains(self): index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right') @@ -289,7 +277,6 @@ def test_contains(self): assert Interval(0, 1, closed='left') not in index assert Interval(0, 1, closed='both') not in index - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_contains_method(self): index = IntervalIndex.from_arrays([0, 1], [1, 2], closed='right') diff --git a/pandas/tests/indexing/test_interval_new.py b/pandas/tests/indexing/test_interval_new.py index 6b94adf3e5423..e102973bbe5a9 100644 --- a/pandas/tests/indexing/test_interval_new.py +++ b/pandas/tests/indexing/test_interval_new.py @@ -5,13 +5,12 @@ from pandas import Series, IntervalIndex, Interval import pandas.util.testing as tm - +@pytest.mark.xfail(reason="new indexing tests for issue 16316") class TestIntervalIndex(object): def setup_method(self, method): self.s = Series(np.arange(5), IntervalIndex.from_breaks(np.arange(6))) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_loc_with_interval(self): # loc with single label / list of labels: @@ -60,7 +59,6 @@ def test_loc_with_interval(self): with pytest.raises(KeyError): s[Interval(5, 6)] - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_loc_with_scalar(self): # loc with single label / list of labels: @@ -89,7 +87,6 @@ def test_loc_with_scalar(self): expected = s.iloc[2:5] tm.assert_series_equal(expected, s.loc[s >= 2]) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_loc_with_slices(self): # loc with slices: @@ -149,7 +146,6 @@ def test_loc_with_slices(self): with pytest.raises(NotImplementedError): s[0:4:2] - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_loc_with_overlap(self): idx = IntervalIndex.from_tuples([(1, 5), (3, 7)]) @@ -227,7 +223,6 @@ def test_non_unique(self): expected = s.iloc[0:1] tm.assert_series_equal(expected, result) - @pytest.mark.xfail(reason="new indexing tests for issue 16316") def test_non_unique_moar(self): idx = IntervalIndex.from_tuples([(1, 3), (1, 3), (3, 7)]) From 1cc8004d39cb851c1d42845ab376d33df0d13c01 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 21 Dec 2017 12:09:43 -0800 Subject: [PATCH 50/56] minor update for pep --- pandas/tests/indexes/test_interval_new.py | 61 +++++++++++++--------- pandas/tests/indexing/test_interval_new.py | 1 + 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/test_interval_new.py index 55a2943db80e2..66350145c4dcb 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/test_interval_new.py @@ -162,41 +162,46 @@ def test_slice_locs_with_ints_and_floats_fails(self, query): index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)]) pytest.raises(KeyError, index.slice_locs, query) - @pytest.mark.parametrize("query_and_expected_result", [ - (Interval(1, 3, closed='right'), 1), - (Interval(1, 3, closed='left'), -1), - (Interval(1, 3, closed='both'), -1), - (Interval(1, 3, closed='neither'), -1), - (Interval(1, 4, closed='right'), -1), - (Interval(0, 4, closed='right'), -1), - (Interval(1, 2, closed='right'), -1)]) - def test_get_indexer_with_interval_single_queries(self, query_and_expected_result): + @pytest.mark.parametrize("query", [ + Interval(1, 3, closed='right'), + Interval(1, 3, closed='left'), + Interval(1, 3, closed='both'), + Interval(1, 3, closed='neither'), + Interval(1, 4, closed='right'), + Interval(0, 4, closed='right'), + Interval(1, 2, closed='right')]) + @pytest.mark.parametrize("expected_result", [1, -1, -1, -1, -1, -1, -1]) + def test_get_indexer_with_interval_single_queries(self, query, + expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='right') - query, expected_result = query_and_expected_result result = index.get_indexer([query]) expect = np.array([expected_result], dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.parametrize("query_and_expected_result", [ - ([Interval(2, 4, closed='right'), Interval(1, 3, closed='right')], [2, 1]), - ([Interval(1, 3, closed='right'), Interval(0, 2, closed='right')], [1, -1]), - ([Interval(1, 3, closed='right'), Interval(1, 3, closed='left')], [1, -1])]) - def test_get_indexer_with_interval_multiple_queries(self, query_and_expected_result): + @pytest.mark.parametrize("query", [ + [Interval(2, 4, closed='right'), Interval(1, 3, closed='right')], + [Interval(1, 3, closed='right'), Interval(0, 2, closed='right')], + [Interval(1, 3, closed='right'), Interval(1, 3, closed='left')]]) + @pytest.mark.parametrize("expected_result", [[2, 1], [1, -1], [1, -1]]) + def test_get_indexer_with_interval_multiple_queries(self, query, + expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='right') - query, expected_result = query_and_expected_result result = index.get_indexer(query) expect = np.array(expected_result, dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) - @pytest.mark.parametrize("expected_result", [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1]) - def test_get_indexer_with_ints_and_floats_single_queries(self, query, expected_result): + @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, + 2, 2.5, 3, 3.5, 4, 4.5]) + @pytest.mark.parametrize("expected_result", [-1, -1, 0, 0, 1, + 1, -1, -1, 2, 2, -1]) + def test_get_indexer_with_ints_and_floats_single_queries(self, query, + expected_result): index = IntervalIndex.from_tuples( [(0, 1), (1, 2), (3, 4)], closed='right') @@ -205,9 +210,12 @@ def test_get_indexer_with_ints_and_floats_single_queries(self, query, expected_r expect = np.array([expected_result], dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.parametrize("query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) - @pytest.mark.parametrize("expected_result", [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]]) - def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, expected_result): + @pytest.mark.parametrize("query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], + [1, 2, 3, 4, 2]]) + @pytest.mark.parametrize("expected_result", [[0, 1], [0, 1, -1], + [0, 1, -1, 2], [0, 1, -1, 2, 1]]) + def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, + expected_result): index = IntervalIndex.from_tuples( [(0, 1), (1, 2), (3, 4)], closed='right') @@ -219,7 +227,8 @@ def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, expected index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) # TODO: @shoyer believes this should raise, master branch doesn't - @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) + @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, + 2.5, 3, 3.5, 4, 4.5]) @pytest.mark.parametrize("expected_result", [ (Int64Index([], dtype='int64'), np.array([0])) (Int64Index([0], dtype='int64'), np.array([])) @@ -232,7 +241,8 @@ def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, expected (Int64Index([2], dtype='int64'), np.array([])) (Int64Index([], dtype='int64'), np.array([0])) (Int64Index([], dtype='int64'), np.array([0]))]) - def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, query, expected_result): + def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, + query, expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='left') @@ -249,7 +259,8 @@ def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, query, dtype='int64'), np.array([3])) (Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='int64'), np.array([3]))]) - def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries(self, query, expected_result): + def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries(self, + query, expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='left') diff --git a/pandas/tests/indexing/test_interval_new.py b/pandas/tests/indexing/test_interval_new.py index e102973bbe5a9..2d8810ef980d9 100644 --- a/pandas/tests/indexing/test_interval_new.py +++ b/pandas/tests/indexing/test_interval_new.py @@ -5,6 +5,7 @@ from pandas import Series, IntervalIndex, Interval import pandas.util.testing as tm + @pytest.mark.xfail(reason="new indexing tests for issue 16316") class TestIntervalIndex(object): From 930da2b2c7e515b0a2edc821d6582648b7c5f073 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 21 Dec 2017 12:12:24 -0800 Subject: [PATCH 51/56] more for pep --- pandas/tests/indexes/test_interval_new.py | 31 ++++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/test_interval_new.py index 66350145c4dcb..60045008a3aed 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/test_interval_new.py @@ -172,7 +172,7 @@ def test_slice_locs_with_ints_and_floats_fails(self, query): Interval(1, 2, closed='right')]) @pytest.mark.parametrize("expected_result", [1, -1, -1, -1, -1, -1, -1]) def test_get_indexer_with_interval_single_queries(self, query, - expected_result): + expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='right') @@ -187,7 +187,7 @@ def test_get_indexer_with_interval_single_queries(self, query, [Interval(1, 3, closed='right'), Interval(1, 3, closed='left')]]) @pytest.mark.parametrize("expected_result", [[2, 1], [1, -1], [1, -1]]) def test_get_indexer_with_interval_multiple_queries(self, query, - expected_result): + expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='right') @@ -196,10 +196,10 @@ def test_get_indexer_with_interval_multiple_queries(self, query, expect = np.array(expected_result, dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, - 2, 2.5, 3, 3.5, 4, 4.5]) - @pytest.mark.parametrize("expected_result", [-1, -1, 0, 0, 1, - 1, -1, -1, 2, 2, -1]) + @pytest.mark.parametrize("query", + [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) + @pytest.mark.parametrize("expected_result", + [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1]) def test_get_indexer_with_ints_and_floats_single_queries(self, query, expected_result): @@ -210,12 +210,12 @@ def test_get_indexer_with_ints_and_floats_single_queries(self, query, expect = np.array([expected_result], dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.parametrize("query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], - [1, 2, 3, 4, 2]]) - @pytest.mark.parametrize("expected_result", [[0, 1], [0, 1, -1], - [0, 1, -1, 2], [0, 1, -1, 2, 1]]) + @pytest.mark.parametrize("query", + [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) + @pytest.mark.parametrize("expected_result", + [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]]) def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, - expected_result): + expected_result): index = IntervalIndex.from_tuples( [(0, 1), (1, 2), (3, 4)], closed='right') @@ -227,8 +227,8 @@ def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) # TODO: @shoyer believes this should raise, master branch doesn't - @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, - 2.5, 3, 3.5, 4, 4.5]) + @pytest.mark.parametrize("query", + [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) @pytest.mark.parametrize("expected_result", [ (Int64Index([], dtype='int64'), np.array([0])) (Int64Index([0], dtype='int64'), np.array([])) @@ -242,7 +242,7 @@ def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, (Int64Index([], dtype='int64'), np.array([0])) (Int64Index([], dtype='int64'), np.array([0]))]) def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, - query, expected_result): + query, expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='left') @@ -250,7 +250,8 @@ def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, result = index.get_indexer_non_unique([query]) tm.assert_numpy_array_equal(result, expected_result) - @pytest.mark.parametrize("query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) + @pytest.mark.parametrize("query", + [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) @pytest.mark.parametrize("expected_result", [ (Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])) (Int64Index([0, 1, 0, 1, 2, 2], From 2e01d28d3490f3b51c80d09f28ddb20e5f80808e Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Thu, 21 Dec 2017 15:58:50 -0800 Subject: [PATCH 52/56] fix mistake --- pandas/tests/indexes/test_interval_new.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/test_interval_new.py index 60045008a3aed..f56f03d22ca2d 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/test_interval_new.py @@ -8,6 +8,7 @@ from pandas.tests.indexes.common import Base import pandas.util.testing as tm + @pytest.mark.xfail(reason="new indexing tests for issue 16316") class TestIntervalIndex(Base): @@ -230,16 +231,16 @@ def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) @pytest.mark.parametrize("expected_result", [ - (Int64Index([], dtype='int64'), np.array([0])) - (Int64Index([0], dtype='int64'), np.array([])) - (Int64Index([0], dtype='int64'), np.array([])) - (Int64Index([0, 1], dtype='int64'), np.array([])) - (Int64Index([0, 1], dtype='int64'), np.array([])) - (Int64Index([0, 1, 2], dtype='int64'), np.array([])) - (Int64Index([1, 2], dtype='int64'), np.array([])) - (Int64Index([2], dtype='int64'), np.array([])) - (Int64Index([2], dtype='int64'), np.array([])) - (Int64Index([], dtype='int64'), np.array([0])) + (Int64Index([], dtype='int64'), np.array([0])), + (Int64Index([0], dtype='int64'), np.array([])), + (Int64Index([0], dtype='int64'), np.array([])), + (Int64Index([0, 1], dtype='int64'), np.array([])), + (Int64Index([0, 1], dtype='int64'), np.array([])), + (Int64Index([0, 1, 2], dtype='int64'), np.array([])), + (Int64Index([1, 2], dtype='int64'), np.array([])), + (Int64Index([2], dtype='int64'), np.array([])), + (Int64Index([2], dtype='int64'), np.array([])), + (Int64Index([], dtype='int64'), np.array([0])), (Int64Index([], dtype='int64'), np.array([0]))]) def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, query, expected_result): From 5c000a010652a81adde9a787ad824f9d2bbd8692 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 22 Dec 2017 10:14:52 -0800 Subject: [PATCH 53/56] might have messed up a function signature during a previous merge. See if this passes tests --- pandas/tests/indexes/test_interval.py | 2 +- pandas/tests/indexes/test_interval_new.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/test_interval.py index 7eb778fdc1501..510c1c9fce5b0 100644 --- a/pandas/tests/indexes/test_interval.py +++ b/pandas/tests/indexes/test_interval.py @@ -810,7 +810,7 @@ def test_dropna(self, closed): tm.assert_index_equal(result, expected) # TODO: check this behavior is consistent with test_interval_new.py - def test_non_contiguous(self): + def test_non_contiguous(self, closed): index = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=closed) target = [0.5, 1.5, 2.5] actual = index.get_indexer(target) diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/test_interval_new.py index f56f03d22ca2d..6ba86ec04f2b9 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/test_interval_new.py @@ -216,7 +216,7 @@ def test_get_indexer_with_ints_and_floats_single_queries(self, query, @pytest.mark.parametrize("expected_result", [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]]) def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, - expected_result): + expected_result): index = IntervalIndex.from_tuples( [(0, 1), (1, 2), (3, 4)], closed='right') From 96c978acc81fa81a4bccbd67094c985b7902b8c5 Mon Sep 17 00:00:00 2001 From: Alexander Lenail Date: Fri, 22 Dec 2017 10:42:30 -0800 Subject: [PATCH 54/56] formatting, potentially last bug --- pandas/tests/indexes/test_interval_new.py | 34 +++++++++++------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/test_interval_new.py index 6ba86ec04f2b9..cf3871b04459d 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/test_interval_new.py @@ -231,17 +231,17 @@ def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, @pytest.mark.parametrize("query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) @pytest.mark.parametrize("expected_result", [ - (Int64Index([], dtype='int64'), np.array([0])), - (Int64Index([0], dtype='int64'), np.array([])), - (Int64Index([0], dtype='int64'), np.array([])), - (Int64Index([0, 1], dtype='int64'), np.array([])), - (Int64Index([0, 1], dtype='int64'), np.array([])), - (Int64Index([0, 1, 2], dtype='int64'), np.array([])), - (Int64Index([1, 2], dtype='int64'), np.array([])), - (Int64Index([2], dtype='int64'), np.array([])), - (Int64Index([2], dtype='int64'), np.array([])), - (Int64Index([], dtype='int64'), np.array([0])), - (Int64Index([], dtype='int64'), np.array([0]))]) + (Int64Index([], dtype='int64'), np.array([0])), + (Int64Index([0], dtype='int64'), np.array([])), + (Int64Index([0], dtype='int64'), np.array([])), + (Int64Index([0, 1], dtype='int64'), np.array([])), + (Int64Index([0, 1], dtype='int64'), np.array([])), + (Int64Index([0, 1, 2], dtype='int64'), np.array([])), + (Int64Index([1, 2], dtype='int64'), np.array([])), + (Int64Index([2], dtype='int64'), np.array([])), + (Int64Index([2], dtype='int64'), np.array([])), + (Int64Index([], dtype='int64'), np.array([0])), + (Int64Index([], dtype='int64'), np.array([0]))]) def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, query, expected_result): @@ -254,13 +254,11 @@ def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, @pytest.mark.parametrize("query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) @pytest.mark.parametrize("expected_result", [ - (Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])) - (Int64Index([0, 1, 0, 1, 2, 2], - dtype='int64'), np.array([])) - (Int64Index([0, 1, 0, 1, 2, 2, -1], - dtype='int64'), np.array([3])) - (Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], - dtype='int64'), np.array([3]))]) + (Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])), + (Int64Index([0, 1, 0, 1, 2, 2], dtype='int64'), np.array([])), + (Int64Index([0, 1, 0, 1, 2, 2, -1], dtype='int64'), np.array([3])), + (Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='int64'), + np.array([3]))]) def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries(self, query, expected_result): From 518d16e99890821ed96a5080600acffce06f06f1 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Sat, 23 Dec 2017 15:23:12 -0500 Subject: [PATCH 55/56] skip interval_new tests move to separate sub-dirs --- pandas/tests/indexes/interval/__init__.py | 0 pandas/tests/indexes/{ => interval}/test_interval.py | 0 pandas/tests/indexes/{ => interval}/test_interval_new.py | 4 +++- pandas/tests/indexing/interval/__init__.py | 0 pandas/tests/indexing/{ => interval}/test_interval.py | 0 pandas/tests/indexing/{ => interval}/test_interval_new.py | 4 +++- 6 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 pandas/tests/indexes/interval/__init__.py rename pandas/tests/indexes/{ => interval}/test_interval.py (100%) rename pandas/tests/indexes/{ => interval}/test_interval_new.py (99%) create mode 100644 pandas/tests/indexing/interval/__init__.py rename pandas/tests/indexing/{ => interval}/test_interval.py (100%) rename pandas/tests/indexing/{ => interval}/test_interval_new.py (98%) diff --git a/pandas/tests/indexes/interval/__init__.py b/pandas/tests/indexes/interval/__init__.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/pandas/tests/indexes/test_interval.py b/pandas/tests/indexes/interval/test_interval.py similarity index 100% rename from pandas/tests/indexes/test_interval.py rename to pandas/tests/indexes/interval/test_interval.py diff --git a/pandas/tests/indexes/test_interval_new.py b/pandas/tests/indexes/interval/test_interval_new.py similarity index 99% rename from pandas/tests/indexes/test_interval_new.py rename to pandas/tests/indexes/interval/test_interval_new.py index cf3871b04459d..d3f5564ee5071 100644 --- a/pandas/tests/indexes/test_interval_new.py +++ b/pandas/tests/indexes/interval/test_interval_new.py @@ -9,7 +9,9 @@ import pandas.util.testing as tm -@pytest.mark.xfail(reason="new indexing tests for issue 16316") +pytestmark = pytest.mark.skip(reason="new indexing tests for issue 16316") + + class TestIntervalIndex(Base): def _compare_tuple_of_numpy_array(self, result, expected): diff --git a/pandas/tests/indexing/interval/__init__.py b/pandas/tests/indexing/interval/__init__.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/pandas/tests/indexing/test_interval.py b/pandas/tests/indexing/interval/test_interval.py similarity index 100% rename from pandas/tests/indexing/test_interval.py rename to pandas/tests/indexing/interval/test_interval.py diff --git a/pandas/tests/indexing/test_interval_new.py b/pandas/tests/indexing/interval/test_interval_new.py similarity index 98% rename from pandas/tests/indexing/test_interval_new.py rename to pandas/tests/indexing/interval/test_interval_new.py index 2d8810ef980d9..16326845de1d5 100644 --- a/pandas/tests/indexing/test_interval_new.py +++ b/pandas/tests/indexing/interval/test_interval_new.py @@ -6,7 +6,9 @@ import pandas.util.testing as tm -@pytest.mark.xfail(reason="new indexing tests for issue 16316") +pytestmark = pytest.mark.skip(reason="new indexing tests for issue 16316") + + class TestIntervalIndex(object): def setup_method(self, method): From f530637f8f5d3d6f183c67c5ea9b2f34da5e2e62 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Sat, 23 Dec 2017 15:28:22 -0500 Subject: [PATCH 56/56] lint --- .../tests/indexes/interval/test_interval.py | 1 + .../indexes/interval/test_interval_new.py | 45 ++++++++++--------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/pandas/tests/indexes/interval/test_interval.py b/pandas/tests/indexes/interval/test_interval.py index 510c1c9fce5b0..1393eb70dd1ad 100644 --- a/pandas/tests/indexes/interval/test_interval.py +++ b/pandas/tests/indexes/interval/test_interval.py @@ -1407,6 +1407,7 @@ def test_errors(self): with tm.assert_raises_regex(TypeError, msg): interval_range(start=start, end=end) + class TestIntervalTree(object): def setup_method(self, method): gentree = lambda dtype: IntervalTree(np.arange(5, dtype=dtype), diff --git a/pandas/tests/indexes/interval/test_interval_new.py b/pandas/tests/indexes/interval/test_interval_new.py index d3f5564ee5071..c8b30e19daa02 100644 --- a/pandas/tests/indexes/interval/test_interval_new.py +++ b/pandas/tests/indexes/interval/test_interval_new.py @@ -3,8 +3,7 @@ import pytest import numpy as np -from pandas import (Interval, IntervalIndex, Int64Index, - Timestamp, Timedelta) +from pandas import Interval, IntervalIndex, Int64Index from pandas.tests.indexes.common import Base import pandas.util.testing as tm @@ -174,8 +173,8 @@ def test_slice_locs_with_ints_and_floats_fails(self, query): Interval(0, 4, closed='right'), Interval(1, 2, closed='right')]) @pytest.mark.parametrize("expected_result", [1, -1, -1, -1, -1, -1, -1]) - def test_get_indexer_with_interval_single_queries(self, query, - expected_result): + def test_get_indexer_with_interval_single_queries( + self, query, expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='right') @@ -189,8 +188,8 @@ def test_get_indexer_with_interval_single_queries(self, query, [Interval(1, 3, closed='right'), Interval(0, 2, closed='right')], [Interval(1, 3, closed='right'), Interval(1, 3, closed='left')]]) @pytest.mark.parametrize("expected_result", [[2, 1], [1, -1], [1, -1]]) - def test_get_indexer_with_interval_multiple_queries(self, query, - expected_result): + def test_get_indexer_with_interval_multiple_queries( + self, query, expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='right') @@ -199,12 +198,14 @@ def test_get_indexer_with_interval_multiple_queries(self, query, expect = np.array(expected_result, dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.parametrize("query", + @pytest.mark.parametrize( + "query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) - @pytest.mark.parametrize("expected_result", + @pytest.mark.parametrize( + "expected_result", [-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1]) - def test_get_indexer_with_ints_and_floats_single_queries(self, query, - expected_result): + def test_get_indexer_with_ints_and_floats_single_queries( + self, query, expected_result): index = IntervalIndex.from_tuples( [(0, 1), (1, 2), (3, 4)], closed='right') @@ -213,12 +214,14 @@ def test_get_indexer_with_ints_and_floats_single_queries(self, query, expect = np.array([expected_result], dtype='intp') tm.assert_numpy_array_equal(result, expect) - @pytest.mark.parametrize("query", + @pytest.mark.parametrize( + "query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) - @pytest.mark.parametrize("expected_result", + @pytest.mark.parametrize( + "expected_result", [[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]]) - def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, - expected_result): + def test_get_indexer_with_ints_and_floats_multiple_queries( + self, query, expected_result): index = IntervalIndex.from_tuples( [(0, 1), (1, 2), (3, 4)], closed='right') @@ -230,7 +233,8 @@ def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)]) # TODO: @shoyer believes this should raise, master branch doesn't - @pytest.mark.parametrize("query", + @pytest.mark.parametrize( + "query", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]) @pytest.mark.parametrize("expected_result", [ (Int64Index([], dtype='int64'), np.array([0])), @@ -244,8 +248,8 @@ def test_get_indexer_with_ints_and_floats_multiple_queries(self, query, (Int64Index([2], dtype='int64'), np.array([])), (Int64Index([], dtype='int64'), np.array([0])), (Int64Index([], dtype='int64'), np.array([0]))]) - def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, - query, expected_result): + def test_get_indexer_non_unique_with_ints_and_floats_single_queries( + self, query, expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='left') @@ -253,7 +257,8 @@ def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, result = index.get_indexer_non_unique([query]) tm.assert_numpy_array_equal(result, expected_result) - @pytest.mark.parametrize("query", + @pytest.mark.parametrize( + "query", [[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]]) @pytest.mark.parametrize("expected_result", [ (Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])), @@ -261,8 +266,8 @@ def test_get_indexer_non_unique_with_ints_and_floats_single_queries(self, (Int64Index([0, 1, 0, 1, 2, 2, -1], dtype='int64'), np.array([3])), (Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='int64'), np.array([3]))]) - def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries(self, - query, expected_result): + def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries( + self, query, expected_result): index = IntervalIndex.from_tuples( [(0, 2.5), (1, 3), (2, 4)], closed='left')