Add ndarray.keys() and ndarray.values() methods
See original GitHub issueIt would be useful for duck typing if ndarray had keys() and values() methods. Here’s how I would implement them in Python:
def keys(self):
return self.dtype.names
def values(self):
return self.flat
The way I’ve shown, keys() would return a tuple and values() would return an iterator (as dict does in Python 3).
This would make it easier to write generic code which handles structured arrays, dicts, and other mapping types transparently, for example:
np.array([(1, 3), (4, 5)], dtype=[('a', int), ('b', int)])
{ 'a': [1, 4], 'b': [3, 5] }
Without this, I currently have ugly code such as:
def hasColumn(data, name):
columns = data.dtype.names if isinstance(data, np.ndarray) else data.keys()
return name in columns
As far as I know, these methods would actually be implemented in C, specifically by adding to array_methods
in multiarray/methods.c (and using flat
from multiarray/getset.c).
Issue Analytics
- State:
- Created 9 years ago
- Comments:18 (17 by maintainers)
Top Results From Across the Web
numpy.ndarray — NumPy v1.24 Manual
numpy.ndarray# ; dumps (). Returns the pickle of the array as a string. ; fill (value). Fill the array with a scalar value....
Read more >Dictionary keys and values to separate numpy arrays
You can use np.fromiter to directly create numpy arrays from the dictionary key and values views: In python 3: keys = np.fromiter(Samples.keys(), ...
Read more >Numpy | ndarray - GeeksforGeeks
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In...
Read more >Array.prototype.keys() - JavaScript - MDN Web Docs
A new Array iterator object. Description. When used on sparse arrays, the keys() method iterates empty slots as if they have the value...
Read more >pandas.DataFrame.to_numpy — pandas 1.5.2 documentation
The dtype to pass to numpy.asarray() . ... The default value depends on dtype and the dtypes of the DataFrame columns. ... Similar...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The proposed
keys()
would return a set of valid arguments to__getitem__
, though of coursendarray
supports many other arguments too. I think most people would consider the names of columns in a structured array to be its “keys” even if indexing is also possible by row numbers, boolean masks, etc.If your impression is that what I am suggesting is for
keys()
to return things not usable with__getitem__
then I will have to apologize for miscommunicating. It absolutely is my intention that indexing would be possible using the results ofkeys()
.What’s proposed in #7552 is good enough to satisfy this.