[Numpy-discussion] Proposal: add ndarray.keys() to return dtype.names

John Zwinck jzwinck at gmail.com
Tue Sep 30 06:49:47 EDT 2014

I first proposed this on GitHub:
https://github.com/numpy/numpy/issues/5134 ; jaimefrio requested that
I bring it to this list for discussion.

My proposal is to add a keys() method to NumPy's array class ndarray.
The behavior would be to return self.dtype.names, i.e. the "column
names" for a structured array (and None when dtype.names is None,
which it is for pure numeric arrays without named columns).

I originally proposed to add a values() method also, but I am tabling
that for now so we needn't discuss it in this thread.

The motivation is to enhance the ability to use duck typing with NumPy
arrays, Python dicts, and other types like Pandas DataFrames, h5py
Files, and more.  It's a fairly common thing to want to get the "keys"
of a container, where "keys" is understood to be a sequence of values
one can pass to __getitem__(), and this is exactly what I'm aiming at.


John Zwinck

More information about the NumPy-Discussion mailing list