def test_binop(closed, operand, array_type, func): arr = sc.StairsArray([s1(closed), s2(closed)]) other = { "scalar": 2.5, "Stairs": s3(closed), "array": sc.StairsArray([s3(closed), s4(closed)]), }[operand] if operand == "array": expected = sc.StairsArray( [func(s1(closed), s3(closed)), func(s2(closed), s4(closed))]) else: expected = sc.StairsArray( [func(s1(closed), other), func(s2(closed), other)]) if array_type == "Series": arr = pd.Series(arr) if operand == "array": other = pd.Series(other) expected = pd.Series(expected) result = func(arr, other) assert type(result) == type(expected) if array_type == "Series": assert str(result.dtype) == "Stairs" assert all([a.identical(b) for a, b in zip(result, expected)]) assert all([a.closed == closed for a in result])
def test_StairsArray_relational(date_func, relational): ia1 = sc.StairsArray([s3(date_func), s4(date_func)]) ia2 = sc.StairsArray([s4(date_func), s3(date_func)]) result = getattr(ia1, relational)(ia2) expected = sc.StairsArray( [getattr(x1, relational)(x2) for x1, x2 in zip(ia1, ia2)]) assert (result == expected).bool().all()
def data(): """ Length-100 array for this type. * data[0] and data[1] should both be non missing * data[0] and data[1] should not be equal """ return sc.StairsArray([sc.Stairs().layer(0, i + 1) for i in range(100)])
def test_StairsArray_construction(date_func, klass): list_data = [s1(date_func), s2(date_func)] if klass == dict: data = dict(enumerate(list_data)) else: data = klass(list_data) result = sc.StairsArray(data) for x, y in zip(result, list_data): assert x.identical(y)
def perform_array_func(how, func_str, arr, *args, **kwargs): obj = { "toplevel": sc, "series": pd.Series(arr, dtype="Stairs"), "accessor": pd.Series(arr, dtype="Stairs").sc, "array": sc.StairsArray(arr), }[how] if how == "toplevel": args = [arr] + list(args) return getattr(obj, func_str)(*args, **kwargs)
def data_for_grouping(): """ Data for factorization, grouping, and unique tests. Expected to be like [B, B, NA, NA, A, A, B, C] Where A < B < C and NA is missing """ return sc.StairsArray([ sc.Stairs().layer(1, 3), sc.Stairs().layer(1, 3), None, None, sc.Stairs().layer(2, 4), sc.Stairs().layer(2, 4), sc.Stairs().layer(1, 3), sc.Stairs().layer(1, 4), ])
def test_StairsArray_plot_exception(IS1, IS2): ia = sc.StairsArray([IS1, IS2]) _, ax = plt.subplots() with pytest.raises(ValueError): ia.plot(ax, ["s1"])
def test_StairsArray_plot(IS1, IS2): ia = sc.StairsArray([IS1, IS2]) ia.plot()
def test_StairsArray_construction2(IS1, IS2): with pytest.raises(ValueError): sc.StairsArray(np.array([[sc.Stairs()], [sc.Stairs()]]))
def test_StairsArray_take(IS1, IS2): ia = sc.StairsArray([IS1, IS2, IS1]) ia2 = ia.take((0, 2)) assert (ia2 == sc.StairsArray([IS1, IS1])).bool().all()
def test_StairsArray_copy(IS1, IS2): ia = sc.StairsArray([IS1, IS2]) ia2 = ia.copy() assert (ia == ia2).bool().all() and (id(ia) != id(ia2))
def test_StairsArray_get_exception(IS1, IS2): ia = sc.StairsArray([IS1, IS2, IS1])[1:] with pytest.raises(TypeError): ia[1.5]
def test_StairsArray_set2(IS1, IS2): ia = sc.StairsArray([IS1, IS1, IS1]) ia[1:] = IS2 assert (ia == sc.StairsArray([IS1, IS2, IS2])).bool().all()
def test_StairsArray_slice(IS1, IS2): ia = sc.StairsArray([IS1, IS2, IS1])[1:] assert (ia == sc.StairsArray([IS2, IS1])).bool().all()
def test_StairsArray_construction3(): with pytest.raises(TypeError): sc.StairsArray("Stairs")
def test_StairsArray_isna(IS1): ia = sc.StairsArray([IS1, None, IS1]) assert (ia.isna() == np.array([False, True, False])).all()
def test_StairsArray_concat_same_type(IS1): # the _concat_same_type method is called with shift() sc.StairsArray([IS1, None, IS1]).shift()
def data_missing(): """Length-2 array with [NA, Valid]""" return sc.StairsArray([None, sc.Stairs()])
def test_StairsArray_construction1(IS1, IS2): sa1 = sc.StairsArray([IS1, IS2]) sa2 = sc.StairsArray(sa1) assert (sa1 == sa2).bool().all()