Jack Wright 8b9f02246f
Allow polars first to be used with polars group-by (#15855)
# Description
Provides functionality similar to
https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.dataframe.group_by.GroupBy.first.html
by allowing polars first to be used with a group by

```
> ❯ : [[a b c d]; [1 0.5 true Apple] [2 0.5 true Orange] [2 4 true Apple] [3 10 false Apple] [4 13 false Banana] [5 14 true Banana]] | polars into-df -s {a: u8, b: f32, c: bool, d: str} | polars group-by d | polars first | polars collect
╭───┬────────┬───┬───────┬───────╮
│ # │   d    │ a │   b   │   c   │
├───┼────────┼───┼───────┼───────┤
│ 0 │ Apple  │ 1 │  0.50 │ true  │
│ 1 │ Banana │ 4 │ 13.00 │ false │
│ 2 │ Orange │ 2 │  0.50 │ true  │
╰───┴────────┴───┴───────┴───────╯
```

Additionally, I am setting the POLARS_ALLOW_EXTENSION to true to avoid
panicking with operations using the dtype object. The conversion will
fallback to object when the type cannot be determining, so this could be
a common case.

# User-Facing Changes
- `polars first` can now be used with `polars group-by`

---------

Co-authored-by: Jack Wright <jack.wright@nike.com>
2025-05-30 10:56:44 -07:00

22 lines
707 B
Rust

use nu_plugin::{MsgPackSerializer, serve_plugin};
use nu_plugin_polars::PolarsPlugin;
fn main() {
env_logger::init();
// Set config options via environment variable
unsafe {
// Extensions are required for certain things like aggregates with object dtypes to work
// correctly. It is disabled by default because of unsafe code.
// See https://docs.rs/polars/latest/polars/#user-guide for details
std::env::set_var("POLARS_ALLOW_EXTENSION", "true");
}
match PolarsPlugin::new() {
Ok(ref plugin) => serve_plugin(plugin, MsgPackSerializer {}),
Err(e) => {
eprintln!("{}", e);
std::process::exit(1);
}
}
}