Python is a high-level programming language used by developers and programmers all over the world. It is a worldwide accepted language that can help to build web applications and web pages for users. There is no replacement for Python as it is an easy-to-code language, and that’s the reason beginners can try projects to become professionals. Python updates and news are still popping up on Google that only programmers and developers keep notes of. It is always good to be updated with the changes and adapt to them. When working with python, you may experience the error warning “indexerror: arrays used as indices must be of integer (or boolean) type”.
IndexError signifies that the code being accessed has an invalid index. When you get this error warning, you don’t need to worry as you are here to look for the solution, and we at Tutopal try our best to provide the solution that can fix the error. Check out how the error pops up
How the error shows up
You get the error message when you code the following script
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap data = np.loadtxt('out (copie).txt') lats = data[:,0] lons = data[:,1] codg_tec = data[:,2] m = Basemap(projection = 'merc', llcrnrlon= -9, llcrnrlat=19, urcrnrlon= 12, urcrnrlat= 37, resolution= 'i') m.drawcoastlines() lon, lat = np.meshgrid(lons, lats) x, y = m(lon, lat) cb = m.pcolormesh(x, y, np.squeeze(data[codg_tec]) , shading='flat', cmap=plt.cm.jet) cbar = m.colorbar(cb, location = 'right', pad = '10%') m.drawmapboundary() m.drawmapscale() m.drawmeridians(np.arange(-9,12,5), labels=[False,False,False,True]) m.drawparallels(np.arange(19,38,5), labels=[True,False,False,False]) m.drawstates() m.drawcountries() plt.title('CODG-vTEC on 02-01-2015') plt.show()
After running the program, you get into the following error
Traceback (most recent call last): File "color.py", line 21, in <module> cb = m.pcolor(x, y, data[codg_tec] , shading='flat', cmap=plt.cm.jet) IndexError: arrays used as indices must be of integer (or boolean) type
How To Handle the Exception “indexerror: arrays used as indices must be of integer (or boolean) type”
The root cause of the error is the use of float data type. Numpy only accepts Boolean or Integer and using any other data type throws an error warning.
Solution 1 – Convert float to int
Converting the data type to an integer can fix the error. Have a look at the code
import numpy as np indices_array = np.array ([[0,1,2.1],[0,2,4.1]]) array = np.array ([[1,3,5] , [7,9,11]]) indices = indices_array [:,0].astype(int) print (array [indices])
[[1 3 5] [1 3 5]]
Solution 2 – Convert to Boolean
The error will go away when you convert the data type to Boolean. The values that are considered True can only be implemented that can also correspond to the index value. Have a look at the code
import numpy as np indices_array = np.array ([[0,1,2.1],[1,2,4.1]]) array = np.array ([[1,3,5] , [7,9,11]]) indices = indices_array [:,0].astype(bool) print (indices) print (array [indices])
[False True] [[7 9 11]]
And that’s exactly how to tackle the error “indexerror: arrays used as indices must be of integer (or boolean) type”. I hope you enjoyed it and find it useful!
I wish you luck!