Open starzar opened 1 week ago
Definitely don't do this in a Python function ;)
I think you just want this: unnest the struct column for simplicity, and then get the horizontal sum of everything except the first field (selectors[^1] make that kind of column selection pretty easy - adjust as needed):
import polars.selectors as cs
Total_df.unnest("Market_Long").with_columns(
Sum_Market_Long=pl.sum_horizontal(~cs.first())
)
# shape: (3, 5)
# ┌────────┬──────────┬──────────┬──────────┬─────────────────┐
# │ Ticker ┆ trader_a ┆ trader_b ┆ trader_c ┆ Sum_Market_Long │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ str ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
# ╞════════╪══════════╪══════════╪══════════╪═════════════════╡
# │ AAPL ┆ 1 ┆ 2 ┆ 3 ┆ 6 │
# │ GOOGL ┆ 1 ┆ 2 ┆ 3 ┆ 6 │
# │ MSFT ┆ 1 ┆ 2 ┆ 3 ┆ 6 │
# └────────┴──────────┴──────────┴──────────┴─────────────────┘
[^1]: Polars selectors https://docs.pola.rs/api/python/stable/reference/selectors.html
@alexander-beedie thanks for the reply...the selector works fine for a single nested dataclass , but for multidimensional dataclasses, there is more functionality required.Are there any other ways to access deep nested fields of mutlidimensional dataclasses?
You should probably just use the Struct field access, optionally in conjunction with selectors, optionally unnesting to simplify the top-level (as shown). Once it's in Polars it's not a dataclass anymore (obviously ;) it is a Struct composed from the top-level fields of the input dataclass, so it's optimal to interact with it as such.
I'd suggest providing a more specific example that's closer to your real data and asking in the Discord channel for some suggestions (I can pick this up there if you do so) 👍
Checks
Reproducible example
Log output
Issue description
Polars(0.20.31) Trying to alias the processed column returned by map_element(custom_function) gives 'AttributeError: 'dict' object has no attribute 'alias'' (refer code-Try_1 ).
Also,directly processing and returning a column in as custom function does not get processed correctly(refer code-Try_2 &Try_3.)How to access nested values in dataclass fields ?
Tried 3 different ways to access them , but each way has some issue.
Expected behavior
Show nested values in data class fields
Installed versions