determining if structured/rec array
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
On Sun, Mar 21, 2010 at 12:12 PM, Skipper Seabold <jsseabold@gmail.com> wrote:
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?
Not commenting on the utility, but numpy as lib/recfunctions.py that would seem to be a sensible spot for such a function. Certainly, since it deals just with numpy arrays, it should live in numpy and not scipy. Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma
On Sun, Mar 21, 2010 at 1:48 PM, Ryan May <rmay31@gmail.com> wrote:
On Sun, Mar 21, 2010 at 12:12 PM, Skipper Seabold <jsseabold@gmail.com> wrote:
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?
Not commenting on the utility, but numpy as lib/recfunctions.py that would seem to be a sensible spot for such a function. Certainly, since it deals just with numpy arrays, it should live in numpy and not scipy.
Agreed. Is there general interest in something like this? It's really simply obviously, but the function is convenient and more transparent than what I've been doing. Skipper
On Mar 21, 2010, at 12:12 PM, Skipper Seabold wrote:
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
Meh. As long as dtype.names is not None, you have a structured array (recarrays are by definition structured arrays). Looks like a function that doesn't really do anything... -1
On Sun, Mar 21, 2010 at 9:43 PM, Pierre GM <pgmdevlist@gmail.com> wrote:
On Mar 21, 2010, at 12:12 PM, Skipper Seabold wrote:
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
Meh. As long as dtype.names is not None, you have a structured array (recarrays are by definition structured arrays). Looks like a function that doesn't really do anything... -1
Hmm, so that's definitely simpler. I thought there was a corner case that returned an error, so I used this if statement, but I wrote this a while ago and never bothered messing with it since. Thanks, Skipper
participants (3)
-
Pierre GM
-
Ryan May
-
Skipper Seabold