即使在 pandas 中使用.loc后,也会收到SettingWithCopyWarning警告 [英] Getting SettingWithCopyWarning warning even after using .loc in pandas

查看:92
本文介绍了即使在 pandas 中使用.loc后,也会收到SettingWithCopyWarning警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

df_masked.loc[:, col] = df_masked.groupby([df_masked.index.month, df_masked.index.day])[col].\
            transform(lambda y: y.fillna(y.median()))

即使使用.loc之后,我仍然可以理解.错误,我该如何解决?

Even after using a .loc, I get the foll. error, how do I fix it?

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

推荐答案

如果df_masked是其他某个DataFrame的子DataFrame,则可以获取此UserWarning. 特别是,如果已将数据从原始DataFrame 复制df_masked,则Pandas会发出UserWarning来提醒您修改df_masked不会影响原始DataFrame.

You could get this UserWarning if df_masked is a sub-DataFrame of some other DataFrame. In particular, if data had been copied from the original DataFrame to df_masked then, Pandas emits the UserWarning to alert you that modifying df_masked will not affect the original DataFrame.

如果您不打算修改原始DataFrame,则可以随意忽略UserWarning.

If you do not intend to modify the original DataFrame, then you are free to ignore the UserWarning.

有很多方法可以按语句关闭用户警告.特别是,您可以使用df_masked.is_copy = False.

There are ways to shut off the UserWarning on a per-statement basis. In particular, you could use df_masked.is_copy = False.

如果您经常遇到此UserWarning,那么与其一一静默地关闭UserWarnings,不如在开发代码时将它们保留为更好.请注意UserWarning的含义,并且如果修改孩子不影响父母的问题对您没有影响,请忽略它.当您的代码准备好进行生产时,或者如果您有足够的经验不需要警告,请使用

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

pd.options.mode.chained_assignment = None

代码顶部附近.

这是一个简单的示例,演示了该问题和(a)解决方案:

Here is a simple example which demonstrate the problem and (a) solution:

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'


之所以存在UserWarning,是为了提醒新用户注意以下事实: 链接索引


The reason why the UserWarning exists is to help alert new users to the fact that chained-indexing such as

df.iloc[1:].loc[:, 'swallow'] = 'forest'

当第一个索引器的结果(例如df.iloc[1:])时,

不会影响df 返回一个副本.

will not affect df when the result of the first indexer (e.g. df.iloc[1:]) returns a copy.

这篇关于即使在 pandas 中使用.loc后,也会收到SettingWithCopyWarning警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆