-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
mask based multi-index assignment of column values described #34461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
c69bc1e
1bd5d9d
14af646
de9f6bd
5aa53c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1870,25 +1870,34 @@ This is the correct access method: | |
|
||
.. ipython:: python | ||
|
||
dfc = pd.DataFrame({'A': ['aaa', 'bbb', 'ccc'], 'B': [1, 2, 3]}) | ||
dfc.loc[0, 'A'] = 11 | ||
dfc | ||
dfc = pd.DataFrame({'a': ['one', 'one', 'two', | ||
'three', 'two', 'one', 'six'], | ||
'c': np.arange(7)}) | ||
dfd = dfc.copy() | ||
# Setting multiple items using a mask (recommended) | ||
mask = dfd['a'].str.startswith('o') | ||
dfd.loc[mask, 'c'] = 42 | ||
dfd | ||
|
||
# Setting a single item (recommended) | ||
dfd.loc[2, 'a'] = 11 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you copy first, like L1876. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in de9f6bd There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please resolve at will |
||
dfd | ||
|
||
This *can* work at times, but it is not guaranteed to, and therefore should be avoided: | ||
The following *can* work at times, but it is not guaranteed to, and therefore should be avoided: | ||
|
||
.. ipython:: python | ||
:okwarning: | ||
|
||
dfc = dfc.copy() | ||
dfc['A'][0] = 111 | ||
dfc | ||
dfd = dfc.copy() | ||
dfd['a'][2] = 111 | ||
dfd | ||
|
||
This will **not** work at all, and so should be avoided: | ||
Last, the subsequent example will **not** work at all, and so should be avoided: | ||
|
||
:: | ||
|
||
>>> pd.set_option('mode.chained_assignment','raise') | ||
>>> dfc.loc[0]['A'] = 1111 | ||
>>> dfd.loc[0]['a'] = 1111 | ||
Traceback (most recent call last) | ||
... | ||
SettingWithCopyException: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need the word recommended here (instead you can use it around L1869)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put it there as @TomAugspurger suggested it in #34383 if I understood correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Jeff is saying that "This is the correct access method" implies that this is the recommended way to set things.
Perhaps change "This is the correct access method" to something like "Multiple items can be set with
.loc
and a mask."There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fine with me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 5aa53c6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please resolve at will