意外的SettingWithCopyWarning:

时间:2019-04-06 15:51:23

标签: python pandas warnings chained-assignment

我正在数据框中运行modifies values within a certain threshold的代码。我收到警告,表面上似乎没有必要:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

我的代码已经使用了.loc[row,column]赋值,因此我不明白警告为什么建议这样做。

import pandas as pd
#pd.options.mode.chained_assignment = None #disable warning 
#pd.set_option('mode.chained_assignment','warn')#or "warn" or "raise"
u = (df 
# Group all forecasts together
     .groupby(by="forecast_id",  sort=False)
# modify only forecasts groups that have smallest value = 0 
     .filter(lambda x: x.value.min() == 0, dropna=False)
# transform values according to a function
     .value.transform( lambda x: (x+0.005).where(x == 0, x-0.005) ) 
     )
# replace the column in the dataframe with the new values except those unaffected
df.loc[pd.notnull(u), "value"] = u

我无法解释的另一种行为是,当我在使用警告选项时,将警告设置为None后,即使将其重新设置为"warn",也没有警告了。注意:我的代码用作函数。


编辑

顶部的链接中提供了代码功能的说明以及示例。但是,我在这里的重点是要理解为什么警告提示建议已经实现的实现:Pandas - Calculate New Value Based on Cross Reference with Another Column

0 个答案:

没有答案