Indexing NumPy arrays with floats …

… now you can’t.

I recently updated to the latest version of NumPy (1.12.1), and today I discovered that some of my spreadsheets using NumPy arrays (via xlwings) were returning errors like:

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

The problem was that using floats to index NumPy arrays previously generated a warning, but now generates an error:

DeprecationWarning to error
•Indexing with floats raises IndexError, e.g., a[0, 0.0].
•Indexing with non-integer array_like raises IndexError, e.g., a[‘1’, ‘2’]
•Indexing with multiple ellipsis raises IndexError, e.g., a[…, …].
•Non-integers used as index values raise TypeError, e.g., in reshape, take, and specifying reduce axis

You might ask, why use floats to index an array in the first place?  The reason is that if a range is copied from Excel to Python then by default all the values are passed as float64.  The array could be converted to all integers, but if the values are mixed integers and floats (such as an array of node numbers and coordinates) that won’t work.  In previous versions the values to be used as array indices could just be left as floats, but now it is necessary to convert them to integers before using them as index values.

I will be working through my Python code posted here and update as required, but if you come across an IndexError anywhere, please let me know.

Advertisements
This entry was posted in Arrays, Excel, Link to Python, NumPy and SciPy, UDFs, VBA and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s