Polars:“ValueError:无法将值‘未知’转换为文字”

Pau*_*ing 5 python python-polars

我在 Polars 中有一行代码,在我最近将 Polars 包更新为“0.19.0”之前,该代码行有效。这个例子之前运行过:

import polars as pl

df = pl.DataFrame(
    {
        "a": [5, 6, 7, 8, 9],
        "b": [5, 6, 7, 8, 9],
        "c": [5, 6, 7, 8, None],})

cols_1 = ["a", "b"]
cols_2 = ["c"]

df = df.filter(pl.all(pl.col(cols_1 + cols_2).is_not_null()))

Run Code Online (Sandbox Code Playgroud)

但现在引发错误:

ValueError: could not convert value 'Unknown' as a Literal
Run Code Online (Sandbox Code Playgroud)

Her*_*cks 3

免责声明。我只是总结@jqurious 和@keraion 的评论。

从 Polars 版本 0.19.0 开始,您需要使用专用的水平聚合函数,例如all_horizontalany_horizontal

import polars as pl

df = pl.DataFrame({
    "a": [5, 6, 7, 8, 9],
    "b": [5, 6, 7, 8, 9],
    "c": [5, 6, 7, 8, None]
})

df.filter(pl.all_horizontal(pl.col(["a", "b", "c"]).is_not_null()))
Run Code Online (Sandbox Code Playgroud)

水平聚合在0.18.7版本中引入,并在 0.18.8 版本中弃用。