@@ -320,6 +320,42 @@ class SettingWithCopyWarning(Warning):
320
320
"""
321
321
322
322
323
+ class ChainedAssignmentError (ValueError ):
324
+ """
325
+ Exception raised when trying to set using chained assignment.
326
+
327
+ When the ``mode.copy_on_write`` option is enabled, chained assignment can
328
+ never work. In such a situation, we are always setting into a temporary
329
+ object that is the result of an indexing operation (getitem), which under
330
+ Copy-on-Write always behaves as a copy. Thus, assigning through a chain
331
+ can never update the original Series or DataFrame.
332
+
333
+ For more information on view vs. copy,
334
+ see :ref:`the user guide<indexing.view_versus_copy>`.
335
+
336
+ Examples
337
+ --------
338
+ >>> pd.options.mode.copy_on_write = True
339
+ >>> df = pd.DataFrame({'A': [1, 1, 1, 2, 2]}, columns=['A'])
340
+ >>> df["A"][0:3] = 10 # doctest: +SKIP
341
+ ... # ChainedAssignmentError: ...
342
+ """
343
+
344
+
345
+ _chained_assignment_msg = (
346
+ "A value is trying to be set on a copy of a DataFrame or Series "
347
+ "through chained assignment.\n "
348
+ "When using the Copy-on-Write mode, such chained assignment never works "
349
+ "to update the original DataFrame or Series, because the intermediate "
350
+ "object on which we are setting values always behaves as a copy.\n \n "
351
+ "Try using '.loc[row_indexer, col_indexer] = value' instead, to perform "
352
+ "the assignment in a single step.\n \n "
353
+ "See the caveats in the documentation: "
354
+ "https://pandas.pydata.org/pandas-docs/stable/user_guide/"
355
+ "indexing.html#returning-a-view-versus-a-copy"
356
+ )
357
+
358
+
323
359
class NumExprClobberingError (NameError ):
324
360
"""
325
361
Exception raised when trying to use a built-in numexpr name as a variable name.
0 commit comments