python 3.x - pandas SettingWithCopyWarning, returning view vs copy -
i quite confused when settingwithcopywarning
raised. example:
import pandas 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"]] # copy(?) df2 = df0.loc[df0.loc[:, "price"] < 10] # copy, maybe not case? df1.loc[1, "item"] = "banana" # works fine df2.loc[1, "item"] = "banana" # raises settingwithcopywarning
it seems df1
copy, whereas, df2
not copy (this time df0
does not change though). why happen? more interested in understanding reason avoiding warning itself. read pandas' documentation on view vs copy, not enlightening on loc
returs. quoting:
dfmi.loc guaranteed dfmi modified indexing behavior
and afterwards:
of course, dfmi.loc.__ getitem__(idx) may view or copy of dfmi
i saw many interesting discussions, here, here, here (and few more), none provides reproducable example , none explains happens when .loc
used. saw false positives can exist , there workarounds (turn off warning, set .is_copy = false
), these fail actively tackle problem.
any insight? why ok modify df1
not df2
?
Comments
Post a Comment