Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve(?) explanation of SettingWithCopy warning
After playing with R a bunch, I started feeling like the explanation of `SettingWithCopy` wasn't getting to the core of the matter, which is actually an essential consequence of python slice assignment semantics. Here's how python handles chained assignment: ```python df['foo']['bar'] = quux df.__getitem__('foo').__setitem__('bar', quux) ``` whereas in R, it's this: ```R df["foo"]["bar"] <- quux df["foo"] <- `[<-`(df["foo"], "bar", quux) df <- `[<-`(df, "foo", `[<-`(`[`(df, "foo"), "bar", quux)) ``` That last is a lot of line noise, though the R method names `` `[` `` and `` `[<-` `` are more concise than `__getitem__` and `__setitem__`! But imagine that you could call `__setitem__` with a kwarg `inplace=False` that would cause it to return a modified copy instead of modifying the original object. Then the R version would translate to this in python: ```python df = df.__setitem__('foo', df.__getitem__('foo') .__setitem__('bar', quux, inplace=False), inplace=False) ``` This is incredibly awkward, but it has the advantage of making `SettingWithCopy` unnecessary— *everything* is a copy, and yet things get set nonetheless. So this commit is an attempt to explain this without requiring the reader to know R.
- Loading branch information