'Piecewise constant or 0-degree spline interpolation in Python/Scipy

Despite scipy's documentation indicating that scipy.interpolate.UnivariateSpline will run on order k<=5, under the hood there is an additional constraint for order >=1. Does anyone know of a way to achieve either 0-degree spline or piecewise constant interpolation with Numpy/Scipy?



Solution 1:[1]

You can use interp1d with kind='zero' or kind='nearest'.

Solution 2:[2]

I have tried a simple trick 1) do numerical integration of step-wise constant function and you will get broken line 2) use 1. degree spline fit 3) calculate a derivative of the spline (method .derivative())

it has worked, but it could be better.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel
Solution 2 Tomas