def test_get_slice_bound_missing(label, side, kind): mylist = [2, 4, 6, 8, 10] index = GenericIndex(mylist) index_pd = pd.Index(mylist) assert index.get_slice_bound(label, side, kind) == index_pd.get_slice_bound( label, side, kind)
def test_get_slice_bound(testlist, side, kind): index = GenericIndex(testlist) index_pd = pd.Index(testlist) for label in testlist: assert index.get_slice_bound(label, side, kind) == index_pd.get_slice_bound( label, side, kind)
def test_index_comparision(): start, stop = 10, 34 rg = RangeIndex(start, stop) gi = GenericIndex(np.arange(start, stop)) assert rg.equals(gi) assert gi.equals(rg) assert not rg[:-1].equals(gi) assert rg[:-1].equals(gi[:-1])
def test_get_slice_bound_missing_str(label, side): # Slicing for monotonic string indices not yet supported # when missing values are specified (allowed in pandas) mylist = ["b", "d", "f"] index = GenericIndex(mylist) index_pd = pd.Index(mylist) assert index.get_slice_bound(label, side, "getitem") == index_pd.get_slice_bound( label, side, "getitem")
def test_index_rename_preserves_arg(): idx1 = GenericIndex([1, 2, 3], name="orig_name") # this should be an entirely new object idx2 = idx1.rename("new_name", inplace=False) assert idx2.name == "new_name" assert idx1.name == "orig_name" # a new object but referencing the same data idx3 = as_index(idx1, name="last_name") assert idx3.name == "last_name" assert idx1.name == "orig_name"
def test_index_rename_inplace(): pds = pd.Index([1, 2, 3], name="asdf") gds = as_index(pds) # inplace=False should yield a deep copy gds_renamed_deep = gds.rename("new_name", inplace=False) gds._values.data.mem = GenericIndex([2, 3, 4])._values.data.mem assert (gds_renamed_deep.values == [1, 2, 3]).all() # inplace=True returns none gds_to_rename = gds gds.rename("new_name", inplace=True) gds._values.data.mem = GenericIndex([3, 4, 5])._values.data.mem assert (gds_to_rename.values == [3, 4, 5]).all()
def test_index_immutable(): start, stop = 10, 34 rg = RangeIndex(start, stop) with pytest.raises(TypeError): rg[1] = 5 gi = GenericIndex(np.arange(start, stop)) with pytest.raises(TypeError): gi[1] = 5
def test_generic_index(testlist): index = GenericIndex(testlist) index_pd = pd.Index(testlist) assert index.is_unique == index_pd.is_unique assert index.is_monotonic == index_pd.is_monotonic assert index.is_monotonic_increasing == index_pd.is_monotonic_increasing assert index.is_monotonic_decreasing == index_pd.is_monotonic_decreasing
def test_name(): idx = GenericIndex(np.asarray([4, 5, 6, 10]), name="foo") assert idx.name == "foo"
def test_reductions(func): x = np.asarray([4, 5, 6, 10]) idx = GenericIndex(np.asarray([4, 5, 6, 10])) assert func(x) == func(idx)
def test_index_find_label_range(): # Monotonic Index idx = GenericIndex(np.asarray([4, 5, 6, 10])) assert idx.find_label_range(4, 6) == (0, 3) assert idx.find_label_range(5, 10) == (1, 4) assert idx.find_label_range(0, 6) == (0, 3) assert idx.find_label_range(4, 11) == (0, 4) # Non-monotonic Index idx_nm = GenericIndex(np.asarray([5, 4, 6, 10])) assert idx_nm.find_label_range(4, 6) == (1, 3) assert idx_nm.find_label_range(5, 10) == (0, 4) # Last value not found with pytest.raises(ValueError) as raises: idx_nm.find_label_range(0, 6) raises.match("value not found") # Last value not found with pytest.raises(ValueError) as raises: idx_nm.find_label_range(4, 11) raises.match("value not found")