-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple arithmetic operations on the "list" type columns #8006
Comments
This is a much needed feature. It seems that the expected syntax would be df.with_columns(pl.col("x1").arr.eval(pl.element() / pl.col("x2")).alias("scaled_x1")) A clunky way to get to the desired result can currently be accomplished by pl.concat(
[
_df.with_columns(
pl.col("x1").arr.eval(pl.element() / x2).alias("scaled_x1")
)
for x2, _df in df.groupby("x2")
]
) |
If you are okay using numpy, this is another way to do it: import numpy as np
df.with_columns(scaled_x1 = pl.struct(["x1", "x2"]).apply(lambda x: np.array(x["x1"]) / x["x2"])) |
I have implemented a working prototype (see branch |
Remaining work:
|
Have made decent progress on making this work. |
The PR will also close #14711. |
This has been added #19162 df.with_columns(scaled_x1 = pl.col.x1 / pl.col.x2)
# shape: (2, 3)
# ┌───────────┬─────┬─────────────┐
# │ x1 ┆ x2 ┆ scaled_x1 │
# │ --- ┆ --- ┆ --- │
# │ list[i64] ┆ i64 ┆ list[f64] │
# ╞═══════════╪═════╪═════════════╡
# │ [1, 2] ┆ 10 ┆ [0.1, 0.2] │
# │ [3, 4] ┆ 20 ┆ [0.15, 0.2] │
# └───────────┴─────┴─────────────┘ |
Problem description
It'd be nice for this code to work:
The idea here is that
x1
is column of arrays, and I want to divide each element in by the value inpl.col('x2')
.I'd be nice to support the basic arithmetic operations from numpy:
+
,-
,*
,/
,%
The text was updated successfully, but these errors were encountered: