-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API / CoW: detect and raise error for chained assignment under Copy-on-Write #49467
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 all commits
16e31e9
2961701
017ed3e
e170b6c
08adb25
8d50d16
4521d87
446863a
dc71236
085dd70
1c3a4bb
a7089ba
e2bb7e1
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 |
---|---|---|
|
@@ -320,6 +320,42 @@ class SettingWithCopyWarning(Warning): | |
""" | ||
|
||
|
||
class ChainedAssignmentError(ValueError): | ||
""" | ||
Exception raised when trying to set using chained assignment. | ||
|
||
When the ``mode.copy_on_write`` option is enabled, chained assignment can | ||
never work. In such a situation, we are always setting into a temporary | ||
object that is the result of an indexing operation (getitem), which under | ||
Copy-on-Write always behaves as a copy. Thus, assigning through a chain | ||
can never update the original Series or DataFrame. | ||
|
||
For more information on view vs. copy, | ||
see :ref:`the user guide<indexing.view_versus_copy>`. | ||
|
||
Examples | ||
-------- | ||
>>> pd.options.mode.copy_on_write = True | ||
>>> df = pd.DataFrame({'A': [1, 1, 1, 2, 2]}, columns=['A']) | ||
>>> df["A"][0:3] = 10 # doctest: +SKIP | ||
... # ChainedAssignmentError: ... | ||
""" | ||
|
||
|
||
_chained_assignment_msg = ( | ||
"A value is trying to be set on a copy of a DataFrame or Series " | ||
"through chained assignment.\n" | ||
"When using the Copy-on-Write mode, such chained assignment never works " | ||
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. never updates the original.... 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. That's on the next line? 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. Ah sorry, should have been clearer: I would reword to:
This sounds better to me |
||
"to update the original DataFrame or Series, because the intermediate " | ||
"object on which we are setting values always behaves as a copy.\n\n" | ||
"Try using '.loc[row_indexer, col_indexer] = value' instead, to perform " | ||
"the assignment in a single step.\n\n" | ||
"See the caveats in the documentation: " | ||
"https://pandas.pydata.org/pandas-docs/stable/user_guide/" | ||
"indexing.html#returning-a-view-versus-a-copy" | ||
) | ||
|
||
|
||
class NumExprClobberingError(NameError): | ||
""" | ||
Exception raised when trying to use a built-in numexpr name as a variable name. | ||
|
Uh oh!
There was an error while loading. Please reload this page.