Repository: scipy/scipy

Issue:
TestIQR.test_rng failing on ARMv8
There is an issue with error handling in stats.iqr which causes the TestIQR.test_rng test to fail on ARMv8 (and likely other non-intel architectures).

There is no explicit error checking for NaNs and expected ValueError exception is not thrown on ARMv8. This issue remains undetected in intel because there the ValueError is thrown as a side effect of undefined behavior caused by a cast of np.nan to intp (this cast is not detected by numpy I think because of numpy/numpy#4592). On intel NaN casts to a large negative number which causes numpy to throw ValueError in following context:

```
Traceback (most recent call last):
  File "/tmp/test_stats.py", line 75, in <module>
    stats.iqr(x, rng=(np.nan, 25))
  File "/home/pfigiel/np/venv/lib/python3.5/site-packages/scipy-1.1.0-py3.5-linux-x86_64.egg/scipy/stats/stats.py", line 2460, in iqr
    keepdims=keepdims, contains_nan=contains_nan)
  File "/home/pfigiel/np/venv/lib/python3.5/site-packages/scipy-1.1.0-py3.5-linux-x86_64.egg/scipy/stats/stats.py", line 2494, in _iqr_percentile
    interpolation=interpolation)
  File "/home/pfigiel/np/venv/lib/python3.5/site-packages/numpy/lib/function_base.py", line 3540, in percentile
    a, q, axis, out, overwrite_input, interpolation, keepdims)
  File "/home/pfigiel/np/venv/lib/python3.5/site-packages/numpy/lib/function_base.py", line 3652, in _quantile_unchecked
    interpolation=interpolation)
  File "/home/pfigiel/np/venv/lib/python3.5/site-packages/numpy/lib/function_base.py", line 3250, in _ureduce
    r = func(a, **kwargs)
  File "/home/pfigiel/np/venv/lib/python3.5/site-packages/numpy/lib/function_base.py", line 3754, in _quantile_ureduce_func
    ap.partition(concatenate((indices_below, indices_above)), axis=axis)
ValueError: kth(=-9223372036854775803) out of bounds (5)
```

On 64-bit ARM NaN casts to 0 which isn't out of bounds for concatenate() and for this reason no exception is thrown.

### Reproducing code example:

This is reproducible on aarch64 ubuntu running in docker with use of qemu (I provided some hints how to setup this on PC under #9515).

### Error message:
```
../../venv/lib/python3.5/site-packages/scipy-1.3.0.dev0+b635563-py3.5-linux-aarch64.egg/scipy/stats/tests/test_stats.py ........................................................ [ 19%]
.......................................................................................F........................................................................................ [ 80%]
..........................................................                                                                                                                       [100%]

======================================================================================= FAILURES =======================================================================================
___________________________________________________________________________________ TestIQR.test_rng ___________________________________________________________________________________

self = <scipy.stats.tests.test_stats.TestIQR object at 0x4011715ef0>

    def test_rng(self):
        x = np.arange(5)
        assert_equal(stats.iqr(x), 2)
        assert_equal(stats.iqr(x, rng=(25, 87.5)), 2.5)
        assert_equal(stats.iqr(x, rng=(12.5, 75)), 2.5)
        assert_almost_equal(stats.iqr(x, rng=(10, 50)), 1.6)  # 3-1.4
    
        assert_raises(ValueError, stats.iqr, x, rng=(0, 101))
>       assert_raises(ValueError, stats.iqr, x, rng=(np.nan, 25))
E       Failed: DID NOT RAISE <class 'ValueError'>

/venv/lib/python3.5/site-packages/scipy-1.3.0.dev0+b635563-py3.5-linux-aarch64.egg/scipy/stats/tests/test_stats.py:1877: Failed
  ...
```

### Scipy/Numpy/Python version information:
```
1.3.0.dev0+b635563 1.15.4 sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
```
This is also reproducible with 1.1.0.


Discussion:

Resolving patch:
diff --git a/scipy/stats/stats.py b/scipy/stats/stats.py
--- a/scipy/stats/stats.py
+++ b/scipy/stats/stats.py
@@ -2483,6 +2483,9 @@ def iqr(x, axis=None, rng=(25, 75), scale='raw', nan_policy='propagate',
     if len(rng) != 2:
         raise TypeError("quantile range must be two element sequence")
 
+    if np.isnan(rng).any():
+        raise ValueError("range must not contain NaNs")
+
     rng = sorted(rng)
     pct = percentile_func(x, rng, axis=axis, interpolation=interpolation,
                           keepdims=keepdims, contains_nan=contains_nan)


Test patch: