Backport of #2897 (ndindex failing)

This backports all relevant changes from:

https://github.com/numpy/numpy/pull/2897

I squashed them into just one commit.