Another solution using essentially a dilate operator:
import numpy as np
from scipy.ndimage import rank_filter
def find_local_maxima(x):
x_dilate = rank_filter(x, -1, size=3)
return x_dilate == x
and for the minima:
def find_local_minima(x):
x_erode = rank_filter(x, -0, size=3)
return x_erode == x