pandas SettingWithCopyWarning,返回视图与副本

时间:2016-11-17 13:41:10

标签: python-3.x pandas dataframe

我很担心提出SettingWithCopyWarning的时候。例如:

import pandas as pd

df0 = pd.DataFrame([["Fruit", "Apple", 12, 0.3],
                    ["Fruit", "Orange", 23, 0.2],
                    ["Dairy", "Milk", 4, 1],
                    ["Dairy", "Cheese", 1.0, 9.5],
                    ["Meat", "Pork", 8, 11],
                    ["Meat", "Buffalo", 2, 18],
                    ["Fruit", "Strawberry", 45, 2.2]],
                   columns=["Type", "Item", "Quantity", "Price"])

df1 = df0.loc[df0.loc[:, "Price"] < 10, ["Type", "Item", "Price"]]  # a copy(?)
df2 = df0.loc[df0.loc[:, "Price"] < 10]  # also a copy, but maybe this is not always the case?

df1.loc[1, "Item"] = "Banana"  # works fine
df2.loc[1, "Item"] = "Banana"  # raises SettingWithCopyWarning

似乎df1始终是副本,而df2并不总是副本(这次df0不会改变)。为什么会这样?我更理解理由而不是避免警告本身。我读过pandas' documentation on view vs copy,但对于loc重新调整,它并不太具启发性。引用:

  

dfmi.loc保证是dfmi本身具有修改的索引行为

然后立即:

  

当然,dfmi.loc .__ getitem __(idx)可能是dfmi的视图或副本

我看到了许多有趣的讨论,hereherehere(以及其他一些),但没有一个提供可重复的示例,并且没有一个真正解释了{{1}时会发生什么} 用来。我看到有时会出现误报并且有一些解决方法(关闭警告,设置.loc),但这些都无法主动解决问题。

有什么见解?为什么可以修改.is_copy = False而不是df1

0 个答案:

没有答案