May I know why ndarray allows floating point index accessing, and what does that mean?
>>> wk1 = numpy.arange(10)
>>> wk1[1:2.8]
array([1])
>>> wk1 = [1,2,3,4,5,6,7,8,9,10]
>>> wk1[1:2.8]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method
>>>
This can be useful, and I wonder why other classes don’t do it the way numpy does.
One particularly helpful time when I’ve noticed this is if your numpy array is an image, and you have an event handler for mouse clicks which give you
event.xdataandevent.ydataas floats, then you can still get a region of interest using the slices without having to convert them to pixel coordinates. For example, suppose you were cropping an image or zooming in an image by clicking and dragging a selection – the mouse position in the image will generally be on sub-pixel coordinates except for the special case where the image is displayed 1:1 scale.As a side note, non-integer slice notation (even complex numbers in slices) can be used in their index tricks classes
r_andc_, for example: