pythonnumpypython-polars

polars arr.get(i) replacement


I have a code

df.select([
    pl.all().exclude("elapsed_time_linreg"),
    pl.col("elapsed_time_linreg").arr.get(0).suffix("_slope"),
    pl.col("elapsed_time_linreg").arr.get(1).suffix("_intercept"),
    pl.col("elapsed_time_linreg").arr.get(2).suffix("_resid_std"),
])

which unpacked the result of a function

@jit
def linear_regression(session_np: np.ndarray) -> np.ndarray:
    w = len(session_np)
    x = np.arange(w)
    sx = w ** 2 / 2
    sy = np.sum(session_np)
    sx2 = (w * (w + 1) * (2 * w + 1)) / 6
    sxy = np.sum(x * session_np)
    slope = (w * sxy - sx * sy) / (w * sx2 - sx**2)
    intercept = (sy - slope * sx) / w
    resids = session_np - (x * slope + intercept)
    return slope, intercept, resids.std()


def get_linreg_aggs(session) -> np.ndarray:
    return linear_regression(np.array(session))


df.select(
    pl.col("elapsed_time")
    .apply(utils.get_linreg_aggs)
    .cast(pl.Float32)
    .alias("elapsed_time_linreg")
)

which worked perfectly on polars 17.15, but since I upgraded it to the latest (0.18.4), it stopped working. Now I get the following error and have no idea how to fix this:

AttributeError: 'ExprArrayNameSpace' object has no attribute 'get'

Is there a way of fixing it without downgrading the version? Or even a more efficient way of creating a bunch of columns from this function?

I thought of the caching the computational result with lru-cache, but since polars uses multiprocessing, idk if it helps.


Solution

  • The .arr namespace was renamed to .list in v0.18.0

    The function is now .list.get()