用法可能类似于下面的代码
out_df = df.select([
pl.col("*"),
pl.col("md5").row_count().over("md5").alias("row_count"),
])
print(out_df)
Run Code Online (Sandbox Code Playgroud)
数据应该是这样的:
前:
MD5
A
A
乙
后:
md5 行数
一个 1
a2
乙1
我需要在 Polars_lazy 中编写自己的表达式。根据我对源代码的理解,我需要编写一个返回 Expr::Function 的函数。问题是为了构造这种类型的对象,必须提供 FunctionOptions 类型的对象。需要注意的是,这个类是公共的,但成员是 pub(crate) 的,因此在 create 之外无法构造这样的对象。有办法解决这个问题吗?
我正在使用 LazyCsvReader 读取文件,并且该文件包含日期列。LazyCsvReader 将日期读取为字符串。日期的格式为“%m-%d-%Y”。如何正确处理日期。有一个页面是针对这个的,但它是针对 python 的。我试图阅读文档但无法弄清楚。以下是我的尝试案例,但无法编译
use polars::prelude::*;
use polars_lazy::prelude::*;
use chrono::prelude::*;
use polars_core::time::*;
fn main() {
let lf = read_csv_lazy("file.csv").unwrap();
let out = lf.clone()
.with_column((col("InvoiceDate").utf8().strptime("%m-%d-%Y")))
.collect();
println!("{:?}", out3);
}
fn read_csv_lazy(file_name: &str) -> Result<LazyFrame> {
let lf: LazyFrame = LazyCsvReader::new(file_name.into())
.has_header(true)
.with_encoding(CsvEncoding::LossyUtf8)
.finish()?;
Ok(lf)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
error[E0599]: no method named `utf8` found for enum `Expr` in the current scope
--> src/main.rs:20:38
|
20 | .with_column((col("InvoiceDate").utf8().strptime("%m-%d-%Y")))
| ^^^^ method not found in `Expr`
Run Code Online (Sandbox Code Playgroud) 我有一个返回列表类型列的函数。因此,我的专栏之一是一个列表。我想将此列表列变成多列。例如:
\nuse polars::prelude::*;\nuse polars::df;\n\nfn main() {\n let s0 = Series::new("a", &[1i64, 2, 3]);\n let s1 = Series::new("b", &[1i64, 1, 1]);\n let s2 = Series::new("c", &[Some(2i64), None, None]);\n // construct a new ListChunked for a slice of Series.\n let list = Series::new("foo", &[s0, s1, s2]);\n\n // construct a few more Series.\n let s0 = Series::new("Group", ["A", "B", "A"]);\n let s1 = Series::new("Cost", [1, 1, 1]);\n let df = DataFrame::new(vec![s0, s1, list]).unwrap();\n\n dbg!(df);\n
Run Code Online (Sandbox Code Playgroud)\n在这个阶段 DF 看起来像这样:
\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\xac\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 Group \xe2\x94\x86 …
Run Code Online (Sandbox Code Playgroud) 我正在处理一些包含 Unix 纪元(以毫秒为单位)的数据帧,并且希望将整个时间戳系列显示为日期。不幸的是,这些文档没有帮助我找到一个极地本地方法来做到这一点,我正在这里伸出援手。关于如何在 Python 和 Rust 中做到这一点的解决方案会让我的头脑和一天变得明亮。
例如,对于 pandas,这样的事情是可能的:
pd.to_datetime(pd_df.timestamp, unit="ms")
# or to convert the whole col
pd_df.timestamp = pd.to_datetime(pd_df.timestamp, unit="ms")
Run Code Online (Sandbox Code Playgroud)
我可以循环整个过程,并像我在这里为每行中的单个条目所做的那样。
datetime.utcfromtimestamp(pl_df["timestamp"][0] / 1000).strftime("%Y-%m-%d")
Run Code Online (Sandbox Code Playgroud)
如果我要在 Rust 中执行此操作,我会使用 chrono 之类的东西将 ts 转换为日期。但我不认为循环每一行是一个好的解决方案。
目前,我发现帮助我的最好方法是pd_df = pl_df.to_pandas()
在 pandas 中进行转换和执行。
我很难找到这么简单的问题的答案。我一直在尝试使用“追加”、“扩展”或其他方法。最后我发现/意识到这种with_column
方法是极地的必经之路。
我认为我应该在这里为遇到同样问题的其他人提供我的解决方案。
有没有办法将 Polars DataFrame 保存到数据库中,例如 MS SQL?
\nConnectorX 库似乎没有这个选项。
\n我正在尝试将 Pandas Dataframe 转换为 Polar Dataframe。
我只是简单地使用了该功能result_polars = pl.from_pandas(result)
。转换进展顺利,但是当我检查两个数据帧的形状时,我发现 Polars 数据帧的大小是原始 Pandas 数据帧的一半。
我相信长度 4172903059 几乎是极坐标数据帧允许的最大尺寸。
有人有建议吗?
这是一个最小工作示例
import polars as pl
import pandas as pd
import numpy as np
df = pd.DataFrame(np.zeros((4292903069,1), dtype=np.uint8))
df_polars = pl.from_pandas(df)
Run Code Online (Sandbox Code Playgroud)
使用这些维度,两个数据框具有相同的大小。如果我输入以下内容:
import polars as pl
import pandas as pd
import numpy as np
df = pd.DataFrame(np.zeros((4392903069,1), dtype=np.uint8))
df_polars = pl.from_pandas(df)
Run Code Online (Sandbox Code Playgroud)
Polars 数据框的尺寸要小得多 (97935773)。
macos: monterey\nnode: v18.1.0\nnodejs-polars: 0.5.3\n
Run Code Online (Sandbox Code Playgroud)\n将Polars DataFrame中的每一列减去该列的平均值。
\n在 pandas 中,解决方案非常简洁,这要归功于DataFrame.sub(other, axis=\'columns\', level=None, fill_value=None)
. other
是scalar, sequence, Series, or DataFrame
:
df.sub(df.mean())\ndf - df.mean()\n
Run Code Online (Sandbox Code Playgroud)\n而在nodejs-polars函数中,other
似乎只是一个Series
根据sub: (other) => wrap("sub", prepareOtherArg(other).inner())
。
console.log(df)\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\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\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\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\x90\n\xe2\x94\x82 A \xe2\x94\x86 B \xe2\x94\x86 C \xe2\x94\x86 D \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x86 i64 \xe2\x94\x86 i64 \xe2\x94\x86 i64 \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\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\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\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\xa1\n\xe2\x94\x82 13520 …
Run Code Online (Sandbox Code Playgroud) 将函数应用于极坐标数据帧pl.DataFrame
或极坐标数据帧的最快方法是什么pl.internals.lazy_frame.LazyFrame
?这个问题是对 Polars-DataFrame 的所有列应用函数的附带条件
我正在尝试使用 python 标准库中的 hashlib 连接所有列并散列值。我正在使用的功能如下:
import hashlib
def hash_row(row):
os.environ['PYTHONHASHSEED'] = "0"
row = str(row).encode('utf-8')
return hashlib.sha256(row).hexdigest()
Run Code Online (Sandbox Code Playgroud)
然而,鉴于此函数需要一个字符串作为输入,这意味着此函数需要应用于 pl.Series 中的每个单元格。处理少量数据应该没问题,但是当我们的行数接近 100m 时,这就变得非常有问题。该主题的问题是我们如何才能在整个 Polars 系列中以最高性能的方式应用这样的函数?
提供了一些创建新列的选项,其中一些比其他列性能更高。
df['new_col'] = df['some_col'] * 100 # vectorized calls
Run Code Online (Sandbox Code Playgroud)
另一种选择是为行操作创建自定义函数。
def apply_func(row):
return row['some_col'] + row['another_col']
df['new_col'] = df.apply(lambda row: apply_func(row), axis=1) # using apply operations
Run Code Online (Sandbox Code Playgroud)
根据我的经验,最快的方法是创建 numpy 矢量化解决方案。
import numpy as np
def np_func(some_col, another_col):
return some_col + another_col
vec_func = np.vectorize(np_func)
df['new_col'] = vec_func(df['some_col'].values, …
Run Code Online (Sandbox Code Playgroud)