BUG: fix reference count, thread safety and dtypes of np.where

Objects must go over the objects copyswap function for correct reference
counting.
The GIL must be taken before arrays are DECREF'd

min_scalar_type_num must also handle `NaN` to avoid unecessary upcasts in np.where