@@ -290,6 +290,52 @@ and attributes without holding entire tree in memory (:issue:`45442`).
290
290
.. _`lxml's iterparse` : https://lxml.de/3.2/parsing.html#iterparse-and-iterwalk
291
291
.. _`etree's iterparse` : https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.iterparse
292
292
293
+ .. _whatsnew_150.enhancements.copy_on_write :
294
+
295
+ Copy on Write
296
+ ^^^^^^^^^^^^^
297
+
298
+ A new feature ``copy_on_write `` was added (:issue: `46958 `). Copy on write ensures that
299
+ any DataFrame or Series derived from another in any way always behaves as a copy.
300
+ Copy on write disallows updating any other object than the object the method
301
+ was applied to.
302
+
303
+ Copy on write can be enabled through:
304
+
305
+ .. code-block :: python
306
+
307
+ pd.set_option(" mode.copy_on_write" , True )
308
+ pd.options.mode.copy_on_write = True
309
+
310
+ Alternatively, copy on write can be enabled locally through:
311
+
312
+ .. code-block :: python
313
+
314
+ with pd.option_context(" mode.copy_on_write" , True ):
315
+ ...
316
+
317
+ Without copy on write, the parent :class: `DataFrame ` is updated when updating a child
318
+ :class: `DataFrame ` that was derived from this :class: `DataFrame `.
319
+
320
+ .. ipython :: python
321
+
322
+ df = pd.DataFrame({" foo" : [1 , 2 , 3 ], " bar" : 1 })
323
+ view = df[" foo" ]
324
+ view.iloc[0 ]
325
+ df
326
+
327
+ With copy on write enabled, df won't be updated anymore:
328
+
329
+ .. ipython :: python
330
+
331
+ with pd.option_context(" mode.copy_on_write" , True ):
332
+ df = pd.DataFrame({" foo" : [1 , 2 , 3 ], " bar" : 1 })
333
+ view = df[" foo" ]
334
+ view.iloc[0 ]
335
+ df
336
+
337
+ A more detailed explanation can be found `here <https://phofl.github.io/cow-introduction.html >`_.
338
+
293
339
.. _whatsnew_150.enhancements.other :
294
340
295
341
Other enhancements
0 commit comments