Is there an efficient way to resample a numpy array using zero-order hold? Ideally something with a signature like that of numpy.interp?
I’m aware of the scipy.interpolate.interp1d, but I’m sure that a vectorised alternative would be available for dealing with cases like this.
Numpy < 1.12
Since you won’t be interpolating any new values, the most efficient way would be to leave the original array as is and index it with floats. This is effectively a zero-order hold.
Numpy >= 1.12
Indexing with floats was deprecated in numpy v1.11 and removed in v1.12 (Jan 2017). The code shown above raises
IndexErrorexception in current numpy versions.You can reproduce the float indexing behavior of older numpy versions by using a wrapper to access the array, converting float indices to integers on the fly. When memory efficiency is a concern, this would avoid the need to pre-emptively interpolate intermediate values using
numpy.interporscipy.interpolate.interp1d.