在Polars 0.13.14中,我可以创建一个DataFrame
,其中包含一个全常数列,如下所示:
import polars as pl
pl.DataFrame(dict(x=pl.repeat(1, 3)))
# shape: (3, 1)
# ┌─────┐
# │ x │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 1 │
# ├╌╌╌╌╌┤
# │ 1 │
# ├╌╌╌╌╌┤
# │ 1 │
# └─────┘
字符串
但在Polars 0.13.15中,这是一个错误
ValueError: Series constructor not called properly.
型
如何用polars中的值填充列?
3条答案
按热度按时间smdnsysy1#
您可能正在寻找
pl.lit(..)
字符串
这将为您给予以下输出
型
vc6uscn92#
从Polars 0.13.15开始,默认情况下,
repeat
became a lazy function和lazy函数不会在DataFrame
构造函数中进行计算。您可以使用eager=True
标志恢复渴望行为:字符串
或者你可以使用这样的上下文:
型
xyhw6mcr3#
pl.repeat(polars v0.20.0)的文档字符串如下:
字符串
默认情况下,它返回一个惰性表达式。
型
正如@ Drupal所提到的。
表达式也可以使用pl.select运行,并转换为系列:
型
pl.select并行运行表达式,因此您可以简化并加快该过程,
型
当你有很多表达式计算时,这可能很方便。为了演示它:
型