gpt4 book ai didi

python - 替换 dask map_partitions 中的现有列会给出 SettingWithCopyWarning

转载 作者:行者123 更新时间:2023-12-05 04:29:31 28 4
gpt4 key购买 nike

我正在使用 map_partitions 替换 dask 数据框中的 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:SettingWithCopyWarning: A value is trying to be set on a copy of aslice from a DataFrame. Try using .loc[row_indexer,col_indexer] =value instead

See the caveats in the documentation:https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copydf['id2'] = df['balance2'] + 1

最佳答案

快速修复是添加数据框的副本:

def func2(df):
df = df.copy() # will make a copy of the dataframe
df['id2'] = df['balance2'] + 1
return df

但是,据我所知,不需要复制数据帧,因为 dask 数据帧的延迟特性意味着更改不会传播回 dask 数据帧分区。

更新:有一个 relevant question这解释了 pandas.copy 的原因。在下面的代码片段中,应用该函数将修改原始的 pandas 数据框,这可能是不可取的:

from pandas import DataFrame

def addcol(df):
df['a'] = 1
return df

df = DataFrame()

df1 = addcol(df)
# without .copy, df is also modified, which might be undesirable

dask 的上下文中,此警告只是一个警告,因此不需要 .copy

from dask.dataframe import from_pandas
ddf = from_pandas(df, npartitions=1)
ddf1 = ddf.map_partitions(addcol)
# will show warning, but original ddf is not modified

关于python - 替换 dask map_partitions 中的现有列会给出 SettingWithCopyWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72321979/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com