error: y_new and y were pointing to the same object. Thus y was also modified by the median filtering process. y_new has to be a copy of y. Fix: Set y_new as deepcopy of y. Import deepcopy from copy.