Skip to content

record array is made up of numpy.void, not numpy.record #271

Closed

Description

@potyt

If I manually create a record array, from say, a list of tuples, I can individually pull out "rows" of this and they are of type numpy.record. This means I can do things, such as:

arr[0].price

and it works.

If I get an array from table.read, or read_where, and then do a .view(np.recarray) I get a record array that looks correct at first glance, but is actually made up of numpy.void objects. And the above code fails because arr[0] is a numpy.void and I cannot call .price on it.

From a users perspective, you shouldn't have seemingly identical looking objects (np.recarray) that behave differently depending on how they were created. I suspect this is a NumPy bug, but interested in your thoughts seeing as it is a fairly common use-case for PyTables.

Thanks

Activity

  1. scopatz commented on Aug 5, 2013

    @scopatz
    Member

    Don't you need to do .view(type=np.recarry)?

  2. potyt commented on Aug 6, 2013

    @potyt
    Author

    Exactly, that's what I am asking about:

    >>> import numpy as np
    >>> a = (1, 2, 3)
    >>> ra = np.rec.fromrecords([a])
    >>> na = np.array([a], dtype='i8,i8,i8').view(type=np.recarray)
    >>> na
    rec.array([(1, 2, 3)], 
          dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<i8')])
    >>> ra
    rec.array([(1, 2, 3)], 
          dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<i8')])
    >>> type(ra)
    <class 'numpy.core.records.recarray'>
    >>> type(na)
    <class 'numpy.core.records.recarray'>
    >>> ra[0]
    (1, 2, 3)
    >>> na[0]
    (1, 2, 3)
    >>> type(ra[0])
    <class 'numpy.core.records.record'>
    >>> type(na[0])
    <class 'numpy.void'>
    

    Now, I realise the example above is NumPy only, but the same type of object, a sort of 'fake recarray', gets returned from table.read, so wanted to see if you guys were aware of this

  3. scopatz commented on Aug 6, 2013

    @scopatz
    Member

    Hi @potyt, I was not aware of this behavior. (I almost never use recarrays _.) But this is not surprising. Reading data out of Tables returns numpy.ndarrays. After reading, I would expect these to perform exactly the same way as the pure numpy example above. I think that you should report this to numpy. Since it is outside the scope of PyTables, would you mind if I closed this as invalid?

  4. potyt commented on Aug 6, 2013

    @potyt
    Author

    Sure, I'll raise this on a NumPy list, ah, it's on github too...

  5. avalentino commented on Aug 6, 2013

    @avalentino
    Member

    Just for the record, a simple workaround could be to swap the order of operations: arr.price[0] instead of arr[0].price

    In [14]: na[0].f0
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-14-cca549dcc2ad> in <module>()
    ----> 1 na[0].f0
    
    AttributeError: 'numpy.void' object has no attribute 'f0'
    
    In [15]: na.f0
    Out[15]: array([1])
    
    In [16]: na.f0[0]
    Out[16]: 1
    
    In [17]: ra.f0[0]
    Out[17]: 1
    
    
  6. potyt commented on Aug 6, 2013

    @potyt
    Author

    Indeed, been making these sorts of compromises for a while, but on the occasion that you need to row-by-row iterator through an array, rather than access the data column-wise, this isn't so easy (or efficient I would think, as you would index into the overall array twice with something like arr.price[0] > arr.limit[0] rather than row.price > row.limit

    Not a big deal for one simple comparison, but if you are picking out a lot of these columns, and acting on their values in a loop that runs a lot, it could add up!?

    More importantly, it also means keeping track of your own row index(es), which could introduce the odd bug, reduces the readability of the code and takes away some of the elegance of the row-based approach.

    Thanks for the v.quick responses btw.

  7. FrancescAlted commented on Apr 27, 2015

    @FrancescAlted
    Member

    Out of scope of PyTables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions