Julia DataFrame,参数:保留在`dataFramerow'的广播上

发布于 2025-02-06 20:34:21 字数 987 浏览 3 评论 0原文

如果我想将矢量添加到数据框的每一列,我可以广播它。相反,如果我在同一数据框架的所有行上循环,然后要广播到所有列,我会得到错误

参数:保留``dataframerow''的广播

这将是一个最小的(非)工作示例:

d = DataFrame()

#create the list with column names
columnlist = [:a, :b, :c, :d]

#each column is filled with [0, 0, ...] of length 10
for col in columnlist
    d[!, col] = zeros(10)
end

#then I want to add a "1" to each entry
#this does work
d[!, columnlist] .+= ones(10)

#this does produce the error mentioned above
for row in 1:nrow(d)
    dfrow = d[row, :]
    dfrow[columnlist] .+= ones(4) #ArgumentError: broadcasting over `DataFrameRow`s is reserved
end

#this also does not work, with the same error
for row in 1:nrow(d)
    d[row, columnlist] .+= ones(4) #ArgumentError: broadcasting over `DataFrameRow`s is reserved
end

是预期的行为还是错误?还是我监督愚蠢的东西?是否有一种方法可以广播到DataFramerow的所有列? (我知道朱莉娅(Julia)是列的事实,但是由于我需要进行一些重型插值,包括每行一次I/O一次,因此这种方式更快了)

我正在使用Julia 1.7.2和DataFrames v1.3.4在Ubuntu 20.04.4上。

If I want to add a Vector to each column of a Dataframe, I can broadcast it. If instead I loop over all rows of the same DataFrame and then want to broadcast to all columns, I get the error

ArgumentError: broadcasting over `DataFrameRow`s is reserved

This would be a minimal (non-) working example:

d = DataFrame()

#create the list with column names
columnlist = [:a, :b, :c, :d]

#each column is filled with [0, 0, ...] of length 10
for col in columnlist
    d[!, col] = zeros(10)
end

#then I want to add a "1" to each entry
#this does work
d[!, columnlist] .+= ones(10)

#this does produce the error mentioned above
for row in 1:nrow(d)
    dfrow = d[row, :]
    dfrow[columnlist] .+= ones(4) #ArgumentError: broadcasting over `DataFrameRow`s is reserved
end

#this also does not work, with the same error
for row in 1:nrow(d)
    d[row, columnlist] .+= ones(4) #ArgumentError: broadcasting over `DataFrameRow`s is reserved
end

Is that the expected behaviour, or a bug? Or am I overseeing something stupid? And is there a way how to broadcast to all columns of a DataFrameRow? (I am aware of the fact that Julia is column-major, but as I need to do some heavy interpolation including I/O once for each row, it is faster this way)

I am using Julia 1.7.2 and DataFrames v1.3.4 on Ubuntu 20.04.4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑忘罢 2025-02-13 20:34:21

这是预期的。原因是朱莉娅(Julia)尚未决定如何处理名为tuple>>:

julia> nt = (a=1, b=2, c=3)
(a = 1, b = 2, c = 3)

julia> nt .+ 1
ERROR: ArgumentError: broadcasting over dictionaries and `NamedTuple`s is reserved

如果您想使用广播的方式来执行此操作(直到基础朱莉娅(Base Julia)决定如何处理nater nationtuple s)是:

for row in 1:nrow(d)
    d[row:row, columnlist] .+= ones(1, 4)
end

或只是不使用广播,而是其他一些获得相同结果的方法:

for row in 1:nrow(d)
    foreach(col -> d[row, col] += 1, columnlist)
end

注意:这是您问题的答案。我没有专注于使您的代码最有效。如果您有绩效问题,您可以问另一个问题,确切地指定您的问题是什么,我会尝试提供帮助。

This is expected. The reason is that Base Julia has not decided yet how to handle broacasting of NamedTuple:

julia> nt = (a=1, b=2, c=3)
(a = 1, b = 2, c = 3)

julia> nt .+ 1
ERROR: ArgumentError: broadcasting over dictionaries and `NamedTuple`s is reserved

If you want to use broadcasting the way to do it (till Base Julia decides what to do with NamedTuples) is:

for row in 1:nrow(d)
    d[row:row, columnlist] .+= ones(1, 4)
end

or just do not use broadcasting but some other method to get the same result:

for row in 1:nrow(d)
    foreach(col -> d[row, col] += 1, columnlist)
end

Note: this is an answer to your question. I have not focused on making your code most efficient possible. If you have a performance issue can you please ask another question specifying exactly what is your problem and I will try to help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文