Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python ndarray with dtype=object

How can I access data inside a numpy array with dtype=object ?

b = numpy.array({"a":[1,2,3]}, dtype=object)

The following gives IndexError.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print(b["a"])
IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

>Solution :

Since you passed in the dict to numpy.array() without putting it in a list, this is a zero-dimensional array. To index into a zero-dimensional array, you can use b.item() to access the element inside. For completeness, to access the data in the "a" key in your dictionary, you can use this.

>>> b.item()["a"]
[1, 2, 3]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading