我想应用一个用户定义函数,该函数需要一些输入(对应于 Polars DataFrame 中的某些列)到 Rust 中的 Polars DataFrame 的某些列。我正在使用的模式如下。我想知道这是最佳实践吗?
fn my_filter_func(col1: &Series, col2: &Series, col2: &Series) -> ReturnType {
let it = (0..n).map(|i| {
let col1 = match col.get(i) {
AnyValue::UInt64(val) => val,
_ => panic!("Wrong type of col1!"),
};
// similar for col2 and col3
// apply user-defined function to col1, col2 and col3
}
// convert it to a collection of the required type
}
Run Code Online (Sandbox Code Playgroud) 我有一个字符串格式的 UUID 时间序列,我希望 Polars 将它们转换为 u128 数字,以便更好地存储和查询。
与我们对日期所做的类似:
....str.strptime(pl.Datetime, fmt="%Y-%m-%dT%H:%M:%S.%fZ", strict=False)
Run Code Online (Sandbox Code Playgroud)
这是受支持的,还是我需要在 Python 端处理它?
另外,我没有看到u128类型,但有一个Decimal似乎是i128. 如果我自己翻译,我应该使用哪种类型?
PS 我注意到Polars 存储库中有一个关于支持 Rust crate 的GitHub 票证Uuid,但在某种程度上,这可以在没有它的情况下实现。所以,我不确定是否是这样。
我想通过根据数量列中的值重复行来扩展以下Polars数据框。
原始数据框:
| 水果 | 数量 |
|---|---|
| 苹果 | 2 |
| 香蕉 | 3 |
预期输出:
| 水果 | 数量 |
|---|---|
| 苹果 | 1 |
| 苹果 | 1 |
| 香蕉 | 1 |
| 香蕉 | 1 |
| 香蕉 | 1 |
这是一个非常相似的问题,但使用 Pandas 而不是 Polars:Repeat rows in a pandas DataFrame based on column value
Polars 重复函数似乎没有提供与 Pandas 对应函数相同的功能:https://pola-rs.github.io/polars/py-polars/html/reference/api/polars.repeat.html
我正在尝试从极坐标数据帧进行聚合。但我没有得到我所期待的。
\n这是该问题的最小复制:
\n\nimport polars as pl\n\n# Create a DataFrame\ndf = pl.DataFrame({"category": ["A", "A", "B", "B", "B"],\n"value": [1., 2., 3., 4., 5.]})\n\n# Group by 'category' and sum 'value'\nresult = df.groupby("category").agg({"value": pl.sum})\n\n# Print the result\nprint(result)\nRun Code Online (Sandbox Code Playgroud)\n我得到:
\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 category \xe2\x94\x86 value \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 str \xe2\x94\x86 list[f64] \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 A \xe2\x94\x86 [1.0, 2.0] \xe2\x94\x82\n\xe2\x94\x82 B \xe2\x94\x86 [3.0, 4.0, 5.0] \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n我想得到:
\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 category \xe2\x94\x86 value \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 str \xe2\x94\x86 list[f64] \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 A \xe2\x94\x86 …Run Code Online (Sandbox Code Playgroud)