Skip to content

Cannot get a view from a composite array with object data members #3253

Closed

Description

@mwtoews

With NumPy 1.7.1 on Win64 with Python 2.7 I see a new issue that was not a problem on a previous version (e.g., 1.6.2).

Consider a composite data type, where a view is obtained from:

import numpy as np
dat1 = np.zeros(3, [('A', 'i'), ('B', 'f')])
new1 = dat1[['B', 'A']]
print(repr(new1))
# array([(0.0, 0), (0.0, 0), (0.0, 0)], 
#      dtype=[('B', '<f4'), ('A', '<i4')])

cool (note: back in 1.4.1 although a subset could be made, the order was the same as the original).

However, if one of types used in the composite data type is '|O8', I can no longer obtain a view:

dat2 = np.zeros(3, [('A', 'i'), ('B', '|O8')])
new2 = dat2[['B', 'A']]

Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\site-packages\numpy\core_internal.py", line 296, in _index_fields
view = ary.view(dtype=view_dtype)
TypeError: Cannot change data-type for object array.

Activity

  1. imstochastic commented on Nov 21, 2013

    @imstochastic

    FWIW, still the case on "1.7.2.dev-3b1a809".

    I have the use case where I want to have two datatypes: some base type 'dt' and another type 'dtSuper' which is a fixed-size array of 'dt' elements. I'd like to cast a properly-sized array with dtype dt to a (1,) array with dtype 'dtSuper', like so:

    dt = np.dtype([('a','O'),('b','f8')])
    a = np.zeros((5,),dtype=dt)
    dtSuper = np.dtype([('samples',dt,(5,))])
    a.view(dtSuper)
    

    Object types, I imagine, could be dangerous if we're allowed to re-interpret them as arbitrary data types, as that gives us direct access to memory addresses and suddenly we're programming in C, with enough rope to hang ourselves with :-). I'm speculating that this is the underlying reason why we are now seeing this error in 1.7.1.

    However the use case I just described doesn't have this problem. It seems that it would make sense to special-case the code, or else (to be more general) somehow check to ensure that every object pointer in the old dtype maps to an object pointer in the new dtype.

  2. charris commented on Feb 22, 2014

    @charris
    Member

    Still open in 1.9-devel. Note that is error occurs in _internal.py, so might not be too difficult to fix.

  3. charris commented on Feb 22, 2014

    @charris
    Member

    So labelled "easy fix" until proved otherwise ;)

  4. added 10 commits that reference this issue on Jun 4, 2015
    907c541
    b7fece9
    4e3841c
    8171464
    34bdab2
    00a31fe
    432ddcd
    f3903e3
    958bf63
    e2cd6fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions