替换dask map_partitions中的现有列提供设置WithCopyWarning
我正在使用 dask
使用 map_partitions
在 dask
dataframe中替换列 ID2
。结果是替换了值,但用 pandas
警告。
该警告是什么,以及如何在下面的示例中应用 .loc
建议?
pdf = pd.DataFrame({
'dummy2': [10, 10, 10, 20, 20, 15, 10, 30, 20, 26],
'id2': [1, 1, 1, 2, 2, 1, 1, 1, 2, 2],
'balance2': [150, 140, 130, 280, 260, 150, 140, 130, 280, 260]
})
ddf = dd.from_pandas(pdf, npartitions=3)
def func2(df):
df['id2'] = df['balance2'] + 1
return df
ddf = ddf.map_partitions(func2)
ddf.compute()
c:\ users \ xxxxxx \ appdata \ local \ temp \ ipykernel_30076 \ 248155462.py:2:2:2: setterWithCopyWarning:一个值试图在一个副本上设置一个值 从数据框架切片。尝试使用.loc [row_indexer,col_indexer] = 值
请参阅文档中的注意事项: df ['id2'] = df ['Balance2'] + 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个快速的修复是添加数据框的副本:
但是,据我了解,由于dask数据框的延迟性质意味着更改不会传播回DASK DATAMFRAME分区,因此不需要数据框架的副本。
更新:有一个相关问题,它解释了
copy .copy .copy
inpandas in
pandas
。在下面的摘要中,应用该函数将修改原始的pandas dataframe,这可能是不希望的:在
dask的上下文中,此警告只是一个警告,因此
.copy 。不需要。
A quick fix is to add copy of the dataframe:
However, as I understand, copying of the dataframe is not required as the delayed nature of the dask dataframe means that the changes are not propagated back to the dask dataframe partitions.
Update: there is a relevant question which explains the reason for
.copy
inpandas
. In the snippet below applying the function will modify the original pandas dataframe, which might be undesirable:In the context of
dask
this warning is just that, a warning, so.copy
is not needed.