6
6
Copy-on-Write (CoW)
7
7
*******************
8
8
9
- .. ipython :: python
10
- :suppress:
11
-
12
- pd.options.mode.copy_on_write = True
13
-
14
9
Copy-on-Write was first introduced in version 1.5.0. Starting from version 2.0 most of the
15
10
optimizations that become possible through CoW are implemented and supported. A complete list
16
11
can be found at :ref: `Copy-on-Write optimizations <copy_on_write.optimizations >`.
@@ -21,6 +16,36 @@ CoW will lead to more predictable behavior since it is not possible to update mo
21
16
one object with one statement, e.g. indexing operations or methods won't have side-effects. Additionally, through
22
17
delaying copies as long as possible, the average performance and memory usage will improve.
23
18
19
+ Previous behavior
20
+ -----------------
21
+
22
+ pandas indexing behavior is tricky to understand. Some operations return views while
23
+ other return copies. Depending on the result of the operation, mutation one object
24
+ might accidentally mutate another:
25
+
26
+ .. ipython :: python
27
+
28
+ df = pd.DataFrame({" foo" : [1 , 2 , 3 ], " bar" : [4 , 5 , 6 ]})
29
+ subset = df[" foo" ]
30
+ subset.iloc[0 ] = 100
31
+ df
32
+
33
+ Mutating ``subset ``, e.g. updating its values, also updates ``df ``. The exact behavior is
34
+ hard to predict. Copy-on-Write solves accidentally modifying more than one object,
35
+ it explicitly disallows this. With CoW enabled, ``df `` is unchanged:
36
+
37
+ .. ipython :: python
38
+
39
+ pd.options.mode.copy_on_write = True
40
+
41
+ df = pd.DataFrame({" foo" : [1 , 2 , 3 ], " bar" : [4 , 5 , 6 ]})
42
+ subset = df[" foo" ]
43
+ subset.iloc[0 ] = 100
44
+ df
45
+
46
+ The following sections will explain what this means and how it impacts existing
47
+ applications.
48
+
24
49
Description
25
50
-----------
26
51
0 commit comments