-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Divide each element in a list by an int #14711
Labels
enhancement
New feature or an improvement of an existing feature
Comments
Liyixin95
added
the
enhancement
New feature or an improvement of an existing feature
label
Feb 27, 2024
To date, I have found: It's allowed on structs, but you lose the list: df.with_columns(
pl.col("a").list.to_struct("max_width") / pl.struct("b")
)
# shape: (3, 2)
# ┌─────────────────────────┬─────┐
# │ a ┆ b │
# │ --- ┆ --- │
# │ struct[3] ┆ i64 │
# ╞═════════════════════════╪═════╡
# │ {0.25,0.5,0.75} ┆ 4 │
# │ {0.2,0.6,null} ┆ 5 │
# │ {0.333333,0.666667,1.0} ┆ 6 │
# └─────────────────────────┴─────┘ "Manual" broadcasting: df.with_columns(
(pl.col("a").flatten() / pl.col("b")).implode().over(pl.int_range(pl.len()))
)
# shape: (3, 2)
# ┌───────────────────────────┬─────┐
# │ a ┆ b │
# │ --- ┆ --- │
# │ list[f64] ┆ i64 │
# ╞═══════════════════════════╪═════╡
# │ [0.25, 0.5, 0.75] ┆ 4 │
# │ [0.2, 0.6] ┆ 5 │
# │ [0.333333, 0.666667, 1.0] ┆ 6 │
# └───────────────────────────┴─────┘ Does anybody know if this is planned for list or maybe array types? |
Related is #14541. I definitely think simple arithmetic should be allowed on lists/arrays, and inter-series arithmetic on arrays of the same dtype. |
I will start on this once #17823 is merged. |
This is a narrow case of #17496 |
This was referenced Sep 24, 2024
This now works #19162 df = pl.DataFrame({"a": [[1, 2, 3], [1, 3], [2, 4, 6]], "b": [4, 5, 6]})
df.with_columns(c=(pl.col.a / pl.col.b))
# shape: (3, 3)
# ┌───────────┬─────┬───────────────────────────┐
# │ a ┆ b ┆ c │
# │ --- ┆ --- ┆ --- │
# │ list[i64] ┆ i64 ┆ list[f64] │
# ╞═══════════╪═════╪═══════════════════════════╡
# │ [1, 2, 3] ┆ 4 ┆ [0.25, 0.5, 0.75] │
# │ [1, 3] ┆ 5 ┆ [0.2, 0.6] │
# │ [2, 4, 6] ┆ 6 ┆ [0.333333, 0.666667, 1.0] │
# └───────────┴─────┴───────────────────────────┘ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
I have a dataframe with a list column and an int column, then I want to divide these two columns like this:
then I get an error:
The text was updated successfully, but these errors were encountered: