Skip to content
Advertisement

Too many indices for array | python

I’m trying to copy arrays to a pandas Dataframe and get the error “too many indices for array”.

temp =  pd.date_range(date_from, date_to)[:len(pr_daily)]

for index in range(len(a_id)):
    if index == 0:
        finalDataframe['date'] = temp
    finalDataframe[f'pr_{a_id[index]}'] = pr_daily[:, index]
    finalDataframe[f'gloabl_irradiance_tilted_in_kWh_per_m2_{a_id[index]}'] = rad_daily[:, index]
    finalDataframe[f'system_id_{a_id[index]}'] = a_id[index]

The error occurs in these lines:

finalDataframe[f'pr_{a_id[index]}'] = pr_daily[:, index]
finalDataframe[f'gloabl_irradiance_tilted_in_kWh_per_m2_{a_id[index]}'] = rad_daily[:, index]

pr_daily and rad_daily are numpy arrays of of the same length.

Traceback (most recent call last):

File “C:Users…Downloadspython_scriptspv.py”, line 277, in finalDataframe[f’pr_{a_id[index]}’] = (pr_daily[:,index])

IndexError: too many indices for array

Advertisement

Answer

This error is thrown when you try to access an array element by providing too much indices.

e.g You try to access the second dimension of a 1-dimension array.

Check the shape of pr_daily and rad_daily if they indeed are 2D arrays.

a = np.random.rand(5,)
b = np.random.rand(5,5)

print(f'b[:,1] :: {b[:,1]}') --> OK
print(f'{a[:,1]}') --> IndexError: too many indices for array

You can access a numpy array shape with the shape attribute

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement