pandas模块之SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

利用python进行数据处理的时候,经常会使用到pandas这一强大的数据处理模块。将数据存储为DataFrame形式,进行一系列的操作。

之前以及最近在处理数据的时候经常出现到的一个问题,将这个问题记录一下

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy return super().fillna(

问题复现:

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'variety': ['beef', 'mutton', 'pork'],
    'count': [10, 5, np.nan]
})

df[['count']].fillna(0, inplace=True)

实际项目程序中,需要处理多列填充值的情况,复现的话我只是简单的用一列用于测试。运行程序会出现如标题的警告。

出现这个问题,python还会很贴切的告诉你请参阅文档中的注意事项(See the caveats in the documentation),并给出对应的链接https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

我们点进去一看究竟吧。

Returning a view versus a copy
返回视图与副本
When setting values in a pandas object, care must be taken to avoid what is called chained indexing. Here is an example.

在 Pandas 对象中设置值时,必须小心避免所谓的 chained indexing. 这是一个例子。

In [354]: dfmi = pd.DataFrame([list('abcd'),
   .....:                      list('efgh'),
   .....:                      list('ijkl'),
   .....:                      list('mnop')],
   .....:                     columns=pd.MultiIndex.from_product([['one', 'two'],
   .....:                                                         ['first', 'second']]))
   .....:

In [355]: dfmi
Out[355]:
    one          two
  first second first second
0     a      b     c      d
1     e      f     g      h
2     i      j     k      l
3     m      n     o      p

Compare these two access methods:
比较这两种方式

In [356]: dfmi['one']['second']
Out[356]:
0    b
1    f
2    j
3    n
Name: second, dtype: object
In [357]: dfmi.loc[:, ('one', 'second')]
Out[357]:
0    b
1    f
2    j
3    n
Name: (one, second), dtype: object

These both yield the same results, so which should you use? It is instructive to understand the order of operations on these and why method 2 (.loc) is much preferred over method 1 (chained []).

这些都产生相同的结果,那么你应该使用哪个?理解这些操作的顺序以及为什么方法 2 ( .loc) 比方法 1 (链式[]) 更受欢迎是有益的。

dfmi[‘one’] selects the first level of the columns and returns a DataFrame that is singly-indexed. Then another Python operation dfmi_with_one[‘second’] selects the series indexed by ‘second’. This is indicated by the variable dfmi_with_one because pandas sees these operations as separate events. e.g. separate calls to getitem, so it has to treat them as linear operations, they happen one after another.

dfmi[‘one’]选择列的第一级并返回一个单索引的 DataFrame。然后另一个 Python 操作dfmi_with_one[‘second’]选择由 索引的系列’second’。这是由变量指示的,dfmi_with_one因为 Pandas 将这些操作视为单独的事件。例如,单独调用__getitem__,因此它必须将它们视为线性操作,它们一个接一个地发生。

Contrast this to df.loc[😢’one’,’second’)] which passes a nested tuple of (slice(None),(‘one’,’second’)) to a single call to getitem. This allows pandas to deal with this as a single entity. Furthermore this order of operations can be significantly faster, and allows one to index both axes if so desired.

对比 thisdf.loc[😢’one’,’second’)]将嵌套元组 of(slice(None),(‘one’,’second’))传递给单个调用 getitem. 这允许熊猫将其作为单个实体来处理。此外,这种操作顺序可以显着加快,并且如果需要,允许对两个轴进行索引。

Why does assignment fail when using chained indexing?

为什么在使用链式索引时赋值会失败?

The problem in the previous section is just a performance issue. What’s up with the SettingWithCopy warning? We don’t usually throw warnings around when you do something that might cost a few extra milliseconds!

上一节中的问题只是一个性能问题。有什么用的了SettingWithCopy警告?当您执行可能需要额外花费几毫秒的时间时,我们通常不会发出警告!

But it turns out that assigning to the product of chained indexing has inherently unpredictable results. To see this, think about how the Python interpreter executes this code:
但事实证明,分配给链式索引的乘积本质上具有不可预测的结果。要看到这一点,请考虑 Python 解释器如何执行此代码:

dfmi.loc[:, ('one', 'second')] = value

dfmi.loc.__setitem__((slice(None), ('one', 'second')), value)

But this code is handled differently:
但是这段代码的处理方式不同:

dfmi['one']['second'] = value

dfmi.__getitem__('one').__setitem__('second', value)

See that getitem in there? Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees), and therefore whether the setitem will modify dfmi or a temporary object that gets thrown out immediately afterward. That’s what SettingWithCopy is warning you about!

看到__getitem__里面了吗?除了简单的情况之外,很难预测它是返回视图还是副本(这取决于数组的内存布局,pandas 对此不做任何保证),因此很难预测是否__setitem__会修改dfmi或获取临时对象之后立即扔掉。那什么SettingWithCopy是警告你!

Note
注意
You may be wondering whether we should be concerned about the loc property in the first example. But dfmi.loc is guaranteed to be dfmi itself with modified indexing behavior, so dfmi.loc. getitem / dfmi.loc. setitem operate on dfmi directly. Of course, dfmi.loc. getitem(idx) may be a view or a copy of dfmi.

您可能想知道我们是否应该关注loc 第一个示例中的属性。但是dfmi.loc保证dfmi 本身具有修改的索引行为,所以dfmi.loc. getitem/ 直接dfmi.loc.__setitem__操作dfmi。当然, dfmi.loc. getitem(idx)可能是视图或副本dfmi。

Sometimes a SettingWithCopy warning will arise at times when there’s no obvious chained indexing going on. These are the bugs that SettingWithCopy is designed to catch! pandas is probably trying to warn you that you’ve done this:
有时SettingWithCopy,当没有明显的链式索引时,会出现警告。这些SettingWithCopy是旨在捕获的错误 !pandas 可能试图警告你你已经这样做了:

def do_something(df):
    foo = df[['bar', 'baz']]

    foo['quux'] = value
    return foo

希望对大家有所帮助,有问题的地方也请大家批评指正,感谢!!

Original: https://blog.csdn.net/lhbo_bo/article/details/119830123
Author: 写代码ing
Title: pandas模块之SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/755246/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Pandas 操作数据(二)

    一、内置函数应用和映射 函数 用法 apply() 用于对DataFrame中的数据进行按行或者按列操作 map() 用于对Series中的每一个数据操作 applymap() 用…

    Python 2023年8月21日
    044
  • Matplotlib figure图形对象

    Matplotlib figure图形对象 通过前面的学习,我们知道matplotlib.pyplot模块能够快速地生成图像,但如果使用面向对象的编程思想,我们就可以更好地控制和自…

    Python 2023年9月4日
    089
  • 数据库搏击

    Mysql文档地址👉 建议在用户变量下配置 在mysql-5.7.19-winx64目录下创建my.ini文件 ; 1.2 命令行连接到Mysql 1.3 图形化软件 1.3.1 …

    Python 2023年11月8日
    049
  • SQL Archery 代码说明及优化(一)

    1.字段加密–django-mirage-field 一个django模型字段,在保存到数据库时对数据进行加密,在从数据库获取数据时进行解密。它使数据库中的数据始终加密…

    Python 2023年5月23日
    0109
  • 【Pandas】DataFrame只复制其中的某一行为多次

    import pandas as pd df = pd.DataFrame(data={ ‘id’: [‘1’, ‘2’, ‘3’], ‘col1’ : [ 5, 6, 7], ‘…

    Python 2023年8月18日
    048
  • 【面试总结】项目实践面试问题

    博客园 :当前访问的博文已被密码保护 请输入阅读密码: Original: https://www.cnblogs.com/upstudy/p/16708781.htmlAutho…

    Python 2023年6月15日
    063
  • 【数模之文本文件操作】

    趣玩目录 文本文件操作 * – 文件自行准备: 例题1: – 相关程序代码如下: + 运行结果如下: 写入的文件内容如下: 例题2: – 相关程…

    Python 2023年8月24日
    042
  • Elasticsearch搜索引擎的使用

    当用户在搜索框输入关键字后,我们要为用户提供相关的搜索结果。 这种需求依赖数据库的模糊查询like关键字可以实现,但是like关键字的效率极低,而且查询需要在多个字段中进行,使用l…

    Python 2023年6月11日
    051
  • Django 面试题

    Flask是一个轻量级、灵活和易扩展的Web开发框架。在 面试_中,会经常涉及到Flask的相关问 _题,下面是对一些常见的Flask 面试 _题_的详细解答。 1. 什么是Fla…

    Python 2023年8月4日
    052
  • 使用linux的ffmpeg进行B站直播推流

    很久之前买了个友善的开发板R2S,一直在家吃灰。最近看到网上有用ffmpeg进行直播推流的案例,想把吃灰的的开发板利用起来,于是有了这篇教程。 第一步:安装ffmpeg sudo …

    Python 2023年10月11日
    053
  • linux环境下conda更改pkg和env缓存路径

    今天在用户目录下( $ cd ~ )使用 du -h -x –max-depth=1 看了一下,miniconda内存占用高达99%…一般来说conda虚拟环境下载的…

    Python 2023年9月9日
    062
  • 在python中使用matplotlib画简单折线图

    live long and prosper 在python中安装matplotlib实现数据可视化(简单折线图) 1、安装matplotlib 在Windows平台上,试用win+…

    Python 2023年9月3日
    054
  • Pytest测试框架(一):Pytest介绍与安装,Pytest编写规则及pytest.main()的参数

    Pytest测试框架(1):Pytest介绍与安装 pytest简介: pytest是python的第三方单元测试框架,比自带的unittest更简洁和高效,同时兼容unittes…

    Python 2023年9月12日
    050
  • Python数据清洗

    实验题目 Python 数据清洗 实验目和要求 1)了解 Python 数据清洗的特点; 2)掌握 Numpy、Pandas、Matplotlib 库的使用 3)能进行与Pytho…

    Python 2023年8月21日
    055
  • 联想拯救者R720如何组建双通道内存

    联想拯救者R720如何组建双通道内存 第一步 内存条的选择 查看电脑的内存条是什么型号,频率,大小等,不过一般都是单条 8g,不放心的话,你可以自己查看一下,任意使用一个检测软件检…

    Python 2023年6月10日
    094
  • 基于Python-django餐厅点餐及推荐系统

    随着时代的发展,人们的生活压力也越来越大。在忙碌的工作和学习了之后,通常会选择到餐厅进行就餐。这样不仅能够品尝到美味的食品,而且能够更加快捷方便的进行就餐从,从而更好的投入到学习和…

    Python 2023年8月4日
    053
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球