1

I want to read from a .npy file to do some signal processing tasks but during this task I received this error:

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

this is my code:

import numpy as np
import matplotlib.pyplot as plt


file  = '/signal/data.npy'
d = np.load(file,allow_pickle=True,encoding = 'latin1')

d['soma'][0]

There are same questions but I could not use them to solve this one.So can anyone help me to fix It? Thanks

This the error: enter image description here

This is part of my data( d is equal to res):

enter image description here

6
  • Please include your data.npy so it is reproducible. Thank you. Commented Apr 16, 2020 at 4:50
  • I will put a sample of data, I could not upload it. Commented Apr 16, 2020 at 5:04
  • So your array includes bunch of dictionaries. Are you looking to extract ALL elements that include keys ["results"]["lfp"] for example, or a specific element with that key? Commented Apr 16, 2020 at 5:09
  • Yes I want to pick all with those keys. Commented Apr 16, 2020 at 5:13
  • 2
    res is a object dtype array containing dictionaries. res[0] is one of those dictionaries. res[0]['soma'] should work. The indexing order matters. Commented Apr 16, 2020 at 6:44

2 Answers 2

2

your data consists of arrays of dictionaries. for each array you have some keys with its values. the solution as @ hpaulj said is:

   res[array_index]["your_key"]
Sign up to request clarification or add additional context in comments.

Comments

2

You have a numpy array d and you are trying to access e.g. "soma" index which is not possible. Numpy indexing rule is:
only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices.

If your numpy array includes dictionaries, you need to extract dictionaries. d['soma'] does not extract elements of numpy array.
This loops over array d and extracts the first element of values of key 'soma' for all dictionaries in d that has key 'soma':

lfp = [i['soma'][0] for i in d if 'soma' in i]

And if it is a dataframe instead of numpy array, try:

d = pd.read_pickle(file)

2 Comments

@Mhasa87 Edited the post. Your picture does not include an example that has key results. So I assume its value is also a dictionary that necessarily includes key "lfp". You can include another if statement if you need to check the existence of key in the nested dictionary as well.
Thanks a lot for your help. I think I have some problems, I will change this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.