admin管理员组文章数量:1431994
In Rust Polars I have a dataframe with columns a
and b
and a struct MyStruct
with fields a
and b
(with type u64
). I want to convert each row of the dataframe into a MyStruct
, returning a vector of MyStruct
s.
In Polars version 0.41.0 the following works:
let foo = df
.into_struct("bar")
.iter()
.map(|row| MyStruct {
row[0].try_extract<u64>().unwrap(),
row[1].try_extract<u64>().unwrap()
})
.collect::<Vec<MyStruct>>();
It is also possible to do this by zipping columns together, but I actually have many fields and find this more concise.
In Cargo.toml
the Polars dependency is specified as
polars = { version = "0.41.0" , default-features = false, features = ["dtype-struct", "lazy"] }
Now I want to upgrade to the latest version (0.44.2) and I get compilation errors:
error[E0608]: cannot index into a value of type `Option<()>
referring to the row
in map
. (Also, the argument to into_struct
is now PlSmallStr::from_str("bar")
)
I have been looking through the release notes since 0.41.0, but I have found no breaking changes mentioning map
/iter
.
In Rust Polars I have a dataframe with columns a
and b
and a struct MyStruct
with fields a
and b
(with type u64
). I want to convert each row of the dataframe into a MyStruct
, returning a vector of MyStruct
s.
In Polars version 0.41.0 the following works:
let foo = df
.into_struct("bar")
.iter()
.map(|row| MyStruct {
row[0].try_extract<u64>().unwrap(),
row[1].try_extract<u64>().unwrap()
})
.collect::<Vec<MyStruct>>();
It is also possible to do this by zipping columns together, but I actually have many fields and find this more concise.
In Cargo.toml
the Polars dependency is specified as
polars = { version = "0.41.0" , default-features = false, features = ["dtype-struct", "lazy"] }
Now I want to upgrade to the latest version (0.44.2) and I get compilation errors:
error[E0608]: cannot index into a value of type `Option<()>
referring to the row
in map
. (Also, the argument to into_struct
is now PlSmallStr::from_str("bar")
)
I have been looking through the release notes since 0.41.0, but I have found no breaking changes mentioning map
/iter
.
- 1 You could also check the edit of this answer – user459872 Commented Nov 19, 2024 at 12:32
1 Answer
Reset to default 2See also this answer. One way to do it is to create an iterator for each column you're interested in, and iterating them in lockstep:
let mut columns = df
.columns (["a", "b"])?
.iter()
.map (|s| s.iter()).collect::<Vec<_>>();
let foo = (0..df.height())
.map (|_| MyStruct {
columns[0].next().unwrap().try_extract<u64>().unwrap(),
columns[1].next().unwrap().try_extract<u64>().unwrap(),
})
.collect::<Vec<_>>();
本文标签: rustMap over struct in PolarsStack Overflow
版权声明:本文标题:rust - Map over struct in Polars - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745577722a2664433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论