Comments (5)
It's happening due to n_field_strategy
Because group_by
is returning random order, the length of the first list changes.
df.group_by("a", "b").agg(pl.col("value").bottom_k(3))
# shape: (4, 3)
# ┌─────┬─────┬───────────┐
# │ a ┆ b ┆ value │
# │ --- ┆ --- ┆ --- │
# │ i64 ┆ str ┆ list[i64] │
# ╞═════╪═════╪═══════════╡
# │ 2 ┆ A ┆ [99] │
# │ 2 ┆ B ┆ [3] │
# │ 1 ┆ A ┆ [1, 2] │
# │ 1 ┆ B ┆ [4, 98] │
# └─────┴─────┴───────────┘
df.group_by("a", "b").agg(pl.col("value").bottom_k(3))
# shape: (4, 3)
# ┌─────┬─────┬───────────┐
# │ a ┆ b ┆ value │
# │ --- ┆ --- ┆ --- │
# │ i64 ┆ str ┆ list[i64] │
# ╞═════╪═════╪═══════════╡
# │ 1 ┆ A ┆ [1, 2] │
# │ 2 ┆ B ┆ [3] │
# │ 1 ┆ B ┆ [4, 98] │
# │ 2 ┆ A ┆ [99] │
# └─────┴─────┴───────────┘
You would need n_field_strategy="max_width"
.list.to_struct("max_width", upper_bound=3))
from polars.
Yeah, something similar came up in #15742 recently.
When n
is known, it's effectively:
pl.struct(
field_0 = pl.col("value").list.get(0),
field_1 = pl.col("value").list.get(1),
field_2 = pl.col("value").list.get(2)
)
But not having to type all that out seems like it would be useful.
from polars.
Makes sense now! I guess this means n_field_strategy=first_non_null
is only useful when the number of elements is known to be the same.
Maybe (?) not a bug, but we might consider adding a warning to the documentation since it's a surprising consequence.
from polars.
Yeah - it is a bit of a footgun.
There was a Notes section added with an explanation - but perhaps a Warning should also be added pointing to that section.
from polars.
Agreed.
Would it make sense for .to_struct
to accept an argument exact=n
, so that the resulting final struct has exactly n
fields (filling with null
when necessary)?
It would behave similar to str.split
and str.split_exact
. (Maybe we could create a new function .to_struct_exact
.)
from polars.
Related Issues (20)
- `join_asof` breaks with certain parquet files (I think due to memory layout or something?) HOT 2
- Expressions result in different serializations across Python Polars PATCH version upgrades HOT 1
- Add inline XOR (`^`) operator for selectors HOT 3
- Inconsistency in constructing dataframes with timezone-aware datatypes HOT 8
- "Failed to determine supertype of bool and datetime[ns]" Panic
- suggest sql add `select sth` without a `from` syntax HOT 1
- Unexpected `index out of bounds` error for specific dataset and set of operations HOT 10
- `pl.DataFrame.corr()` should offer an argument "include_column_names_as_column". HOT 4
- Polars String or &str HOT 5
- Add `split` as superset of `slice` on `ChunkedArray`, `DataFrame` and `Series`.
- expose `null_on_oob` on `gather`.
- Identical query behaves differently between Expression API and SQL API regarding `streaming` HOT 1
- update object_store version 0.9 -> 0.10.1 HOT 1
- [QST]: should `pl.Expr.slice` raise if the numeric arguments are not integral HOT 1
- Large hike in memory for .unique on array fields
- polars.testing.assert_series_equal has incorrect logic for infinities
- Providing nightly builds ? HOT 2
- Increase the verbosity of Schema and Catalog/DB input arguments to the write_database function.
- polars.exceptions.SchemaError: type Int64 is incompatible with expected type Int32 HOT 4
- pl.DataFrame.corr(method = "spearman") HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from polars.