Hierarchical seems to run np.min too many times. I suggest apply stack() to dists and sort the dataframe first, to avoid running min repeatedly.