Example #1
0
    def test_binary_ops_align(self):

        # test aligning binary ops

        # GH 6681
        index = MultiIndex.from_product(
            [list("abc"), ["one", "two", "three"], [1, 2, 3]], names=["first", "second", "third"]
        )

        df = DataFrame(
            np.arange(27 * 3).reshape(27, 3), index=index, columns=["value1", "value2", "value3"]
        ).sortlevel()

        idx = pd.IndexSlice
        for op in ["add", "sub", "mul", "div", "truediv"]:
            opa = getattr(operator, op, None)
            if opa is None:
                continue

            x = Series([1.0, 10.0, 100.0], [1, 2, 3])
            result = getattr(df, op)(x, level="third", axis=0)

            expected = pd.concat([opa(df.loc[idx[:, :, i], :], v) for i, v in x.iteritems()]).sortlevel()
            assert_frame_equal(result, expected)

            x = Series([1.0, 10.0], ["two", "three"])
            result = getattr(df, op)(x, level="second", axis=0)

            expected = pd.concat([opa(df.loc[idx[:, i], :], v) for i, v in x.iteritems()]).reindex_like(df).sortlevel()
            assert_frame_equal(result, expected)

        # GH9463 (alignment level of dataframe with series)

        midx = MultiIndex.from_product([["A", "B"], ["a", "b"]])
        df = DataFrame(np.ones((2, 4), dtype="int64"), columns=midx)
        s = pd.Series({"a": 1, "b": 2})

        df2 = df.copy()
        df2.columns.names = ["lvl0", "lvl1"]
        s2 = s.copy()
        s2.index.name = "lvl1"

        # different cases of integer/string level names:
        res1 = df.mul(s, axis=1, level=1)
        res2 = df.mul(s2, axis=1, level=1)
        res3 = df2.mul(s, axis=1, level=1)
        res4 = df2.mul(s2, axis=1, level=1)
        res5 = df2.mul(s, axis=1, level="lvl1")
        res6 = df2.mul(s2, axis=1, level="lvl1")

        exp = DataFrame(np.array([[1, 2, 1, 2], [1, 2, 1, 2]], dtype="int64"), columns=midx)

        for res in [res1, res2]:
            assert_frame_equal(res, exp)

        exp.columns.names = ["lvl0", "lvl1"]
        for res in [res3, res4, res5, res6]:
            assert_frame_equal(res, exp)
Example #2
0
def partial_correlation(C, X, S, N):
    S = DataFrame(S)
    C = Series(C)
    X = Series(X)
    # linear fit between S and C
    linreg = linear_model.LinearRegression()
    linreg.fit(S, C)
    coef_c = linreg.coef_
    intercept_c = linreg.intercept_
    R_c = C - (S.mul(coef_c, axis=1).sum() + intercept_c)  # residuals of C and S

    # linear fit between S and X
    linreg.fit(S, X)
    coef_x = linreg.coef_
    intercept_x = linreg.intercept_
    R_x = X - (S.mul(coef_x, axis=1).sum() + intercept_x)  # residuals for X and S
    numerator = N * (R_c * R_x).sum() - R_c.sum() * R_x.sum()
    denominator = np.sqrt(N * np.square(R_c).sum() - np.square(R_c.sum())) * np.sqrt(
        N * np.square(R_x).sum() - np.square(R_x.sum())
    )
    rou = numerator * 1.0 / denominator
    return rou