[Numpy-discussion] Determining the 'viewness' of an array, and flags.owndata confusion

Kurt Smith kwmsmith@gmail....
Tue Feb 28 17:01:12 CST 2012

For an arbitrary numpy array 'a', what does 'a.flags.owndata' indicate?

I originally thought that owndata is False iff 'a' is a view.  But
that is incorrect.

Consider the following:

In [119]: a = np.zeros((3,3))

In [120]: a.flags.owndata  # should be True; zeros() creates and
returns a non-view array.
Out[120]: True

In [121]: a_view1 = a[2:, :]

In [122]: a_view1.flags.owndata   # expected to be False
Out[122]: False

In [123]: a_fancy1 = a[[0,1], :]

In [124]: a_fancy1.flags.owndata  # expected to be True, a_fancy1 is a
fancy-indexed array.
Out[124]: True

In [125]: a_fancy2 = a[:, [0,1]]

In [126]: a_fancy2.flags.owndata # expected to be True, a_fancy2 is a
fancy-indexed array.
Out[126]: False

So when I query an array's flags.owndata, what is it telling me?  What
I want to know is whether an array is a view or not.  If flags.owndata
has nothing to do with the 'viewness' of an array, how would I
determine if an array is a view?

In the previous example, a_fancy2 does not own its data, as indicated
by 'owndata' being False.  But when I modify a_fancy2, 'a' is not
modified, as expected, but contrary to what 'owndata' would seem to

The numpybook's documentation of owndata could perhaps be a bit
clearer on the subject:

OWNDATA (O) the array owns the memory it uses or if it borrows it from
another object (if this is False, the base attribute retrieves a
reference to the object this array obtained its data from)

>From my reading, owndata has nothing to do with the 'viewness' of an
array.  Is this correct?  What is the intent of this flag, then?  Its
wording could perhaps be improved: owndata is True iff (1) the array
owns the memory it uses or (2) the array borrows it from another
object.  The second clause seems to indicate that the array **does
not** own its data since it is borrowing it from another object.
However, flags.owndata will be true in this case.

If I cannot use flags.owndata, what is a reliable way to determine
whether or not an array is a view?

More information about the NumPy-Discussion mailing list