[Scipy-svn] r5159 - in trunk/scipy/interpolate: . tests

scipy-svn@scip... scipy-svn@scip...
Fri Nov 21 15:22:55 CST 2008


Author: ptvirtan
Date: 2008-11-21 15:09:29 -0600 (Fri, 21 Nov 2008)
New Revision: 5159

Modified:
   trunk/scipy/interpolate/interpolate.py
   trunk/scipy/interpolate/tests/test_interpolate.py
Log:
interp1d: cast input arrays to a floating-point type (possibly complex), so that eg. fill values don't cause surprises

Modified: trunk/scipy/interpolate/interpolate.py
===================================================================
--- trunk/scipy/interpolate/interpolate.py	2008-11-21 21:09:00 UTC (rev 5158)
+++ trunk/scipy/interpolate/interpolate.py	2008-11-21 21:09:29 UTC (rev 5159)
@@ -216,6 +216,10 @@
         if y.ndim == 0:
             raise ValueError("the y array must have at least one dimension.")
 
+        # Force-cast y to a floating-point type, if it's not yet one
+        if not isinstance(y.dtype.type, np.inexact):
+            y = y.astype(np.float_)
+
         # Normalize the axis to ensure that it is positive.
         self.axis = axis % len(y.shape)
         self._kind = kind

Modified: trunk/scipy/interpolate/tests/test_interpolate.py
===================================================================
--- trunk/scipy/interpolate/tests/test_interpolate.py	2008-11-21 21:09:00 UTC (rev 5158)
+++ trunk/scipy/interpolate/tests/test_interpolate.py	2008-11-21 21:09:29 UTC (rev 5159)
@@ -208,10 +208,19 @@
         assert_raises(ValueError, raises_bounds_error, 11.0)
         raises_bounds_error([0.0, 5.0, 9.0])
 
+    def _bounds_check_int_nan_fill(self, kind='linear'):
+        x = np.arange(10).astype(np.int_)
+        y = np.arange(10).astype(np.int_)
+        c = interp1d(x, y, kind=kind, fill_value=np.nan, bounds_error=False)
+        yi = c(x - 1)
+        assert np.isnan(yi[0])
+        assert_array_almost_equal(yi, np.r_[np.nan, y[:-1]])
+
     def test_bounds(self):
         for kind in ('linear', 'cubic', 'nearest',
                      'slinear', 'zero', 'quadratic'):
             yield self._bounds_check, kind
+            yield self._bounds_check_int_nan_fill, kind
 
     def _nd_check_interp(self, kind='linear'):
         """Check the behavior when the inputs and outputs are multidimensional.



More information about the Scipy-svn mailing list