Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your answer lies in the pandas docs: <a href="http://pandas-docs.github.io/pandas-docs-travis/indexing.html?highlight=view#indexing-view-versus-copy" rel="noreferrer">returning-a-view-versus-a-copy</a>.</p> <blockquote> <p>Whenever an array of labels or a boolean vector are involved in the indexing operation, <strong>the result will be a copy</strong>. With single label / scalar indexing and slicing, e.g. df.ix[3:6] or df.ix[:, 'A'], <strong>a view will be returned</strong>.</p> </blockquote> <p>In your example, <code>bar</code> is a <strong>view</strong> of slices of <code>foo</code>. If you wanted a <strong>copy</strong>, you could have used the <code>copy</code> method. Modifying <code>bar</code> also modifies <code>foo</code>. pandas does not appear to have a copy-on-write mechanism.</p> <p>See my code example below to illustrate:</p> <pre><code>In [1]: import pandas as pd ...: import numpy as np ...: foo = pd.DataFrame(np.random.random((10,5))) ...: In [2]: pd.__version__ Out[2]: '0.12.0.dev-35312e4' In [3]: np.__version__ Out[3]: '1.7.1' In [4]: # DataFrame has copy method ...: foo_copy = foo.copy() In [5]: bar = foo.iloc[3:5,1:4] In [6]: bar == foo.iloc[3:5,1:4] == foo_copy.iloc[3:5,1:4] Out[6]: 1 2 3 3 True True True 4 True True True In [7]: # Changing the view ...: bar.ix[3,1] = 5 In [8]: # View and DataFrame still equal ...: bar == foo.iloc[3:5,1:4] Out[8]: 1 2 3 3 True True True 4 True True True In [9]: # It is now different from a copy of original ...: bar == foo_copy.iloc[3:5,1:4] Out[9]: 1 2 3 3 False True True 4 True True True </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload