pandas 即使在熊猫中使用 .loc 后,也会收到 SettingWithCopyWarning 警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40033471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting SettingWithCopyWarning warning even after using .loc in pandas
提问by user308827
df_masked.loc[:, col] = df_masked.groupby([df_masked.index.month, df_masked.index.day])[col].\
transform(lambda y: y.fillna(y.median()))
Even after using a .loc, I get the foll. error, how do I fix it?
即使在使用 .loc 之后,我也明白了。错误,我该如何解决?
Anaconda\lib\site-packages\pandas\core\indexing.py:476: 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
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item] = s
回答by unutbu
You could get this UserWarning if df_masked
is a sub-DataFrame of some other DataFrame.
In particular, if data had been copiedfrom the original DataFrame to df_masked
then, Pandas emits the UserWarning to alert you that modifying df_masked
will not affect the original DataFrame.
如果df_masked
是某个其他 DataFrame 的子 DataFrame,您可能会收到此UserWarning。特别是,如果数据已从原始 DataFrame复制到df_masked
那时,Pandas 会发出 UserWarning 以提醒您修改df_masked
不会影响原始 DataFrame。
If you do not intend to modify the original DataFrame, then you are free to ignore the UserWarning.
如果您不打算修改原始 DataFrame,那么您可以随意忽略 UserWarning。
There are waysto shut off the UserWarning on a per-statement basis. In particular, you could use df_masked.is_copy = False
.
有多种方法可以在每个语句的基础上关闭 UserWarning。特别是,您可以使用df_masked.is_copy = False
.
If you run into this UserWarning a lot, then instead of silencing the UserWarnings one-by-one, I think it is better to leave them be as you are developing your code. Be aware of what the UserWarning means, and if the modifying-the-child-does-not-affect-the-parent issue does not affect you, then ignore it. When your code is ready for production, or if you are experienced enough to not need the warnings, shut them off entirely with
如果您经常遇到这个 UserWarning,那么与其一个一个地关闭 UserWarning,我认为最好在开发代码时让它们保持原样。请注意 UserWarning 的含义,如果修改子级不影响父级的问题对您没有影响,则忽略它。当您的代码准备好用于生产时,或者如果您有足够的经验不需要警告,请使用以下命令完全关闭它们
pd.options.mode.chained_assignment = None
near the top of your code.
靠近代码顶部。
Here is a simple example which demonstrate the problem and (a) solution:
这是一个简单的示例,它演示了问题和 (a) 解决方案:
import pandas as pd
df = pd.DataFrame({'swallow':['African','European'], 'cheese':['gouda', 'cheddar']})
df_masked = df.iloc[1:]
df_masked.is_copy = False # comment-out this line to see the UserWarning
df_masked.loc[:, 'swallow'] = 'forest'
The reason why the UserWarning exists is to help alert new users to the fact that chained-indexingsuch as
UserWarning 存在的原因是为了帮助提醒新用户注意以下事实: 链式索引,例如
df.iloc[1:].loc[:, 'swallow'] = 'forest'
will not affect df
when the result of the first indexer (e.g. df.iloc[1:]
)
returns a copy.
不会影响df
第一个索引器(例如df.iloc[1:]
)的结果何时返回副本。