[SciPy-User] determining if structured/rec array

Skipper Seabold jsseabold@gmail....
Sun Mar 21 12:12:35 CDT 2010


Is there anywhere in scipy that works on structured arrays
dynamically?  If not, is this a good way to determine if a structured/
rec array was given to a function or is there a better/easier way?

import numpy as np

def isstructarray(X):
    """
    Returns a bool indicating if X is a structured or recarray.

    Parameters
    -----------
    X : ndarray or subclass

    Returns
    -------
    True or False, indicating if X is a structured or recarray.
    """
    if X.__class__ is np.recarray or (isinstance(X, np.ndarray) and\
            X.dtype.names):
        return True
    else:
        return False

X = np.array([('1', 1.0), ('1', 1.0), ('1', 1.0)],
      dtype=[('foo', 'a1'), ('bar', '<f8')])

Y = np.rec.array(X)

Z = np.ones((2,3))

L = [1,2,3]

isstructarray(X)
# True
isstructarray(Y)
# True
isstructarray(Z)
# False
isstructarray(L)
# False

-Skipper


More information about the SciPy-User mailing list