question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ndarray.keys() and ndarray.values() methods

See original GitHub issue

It 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:closed
  • Created 9 years ago
  • Comments:18 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
jzwinckcommented, Sep 29, 2014

The proposed keys() would return a set of valid arguments to __getitem__, though of course ndarray 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 of keys().

0reactions
jzwinckcommented, May 13, 2019

What’s proposed in #7552 is good enough to satisfy this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found