# [Scipy-svn] r2566 - trunk/Lib/sandbox/timeseries

scipy-svn at scipy.org scipy-svn at scipy.org
Mon Jan 15 13:21:02 CST 2007

```Author: mattknox_ca
Date: 2007-01-15 13:20:59 -0600 (Mon, 15 Jan 2007)
New Revision: 2566

Modified:
trunk/Lib/sandbox/timeseries/tcore.py
Log:

Modified: trunk/Lib/sandbox/timeseries/tcore.py
===================================================================
--- trunk/Lib/sandbox/timeseries/tcore.py	2007-01-15 19:11:14 UTC (rev 2565)
+++ trunk/Lib/sandbox/timeseries/tcore.py	2007-01-15 19:20:59 UTC (rev 2566)
@@ -1,5 +1,6 @@
import numpy
+from scipy.interpolate import fitpack

#####---------------------------------------------------------------------------
@@ -155,3 +156,92 @@
return flatten_sequence(args)

+#####---------------------------------------------------------------------------
+#---- --- Functions for filling in masked values in a masked array ---
+#####---------------------------------------------------------------------------
+
+def forward_fill(marr, maxgap=None):
+    """forward_fill(marr, maxgap=None)
+
+forward fill masked values in a 1-d array when there are <= maxgap
+consecutive masked values. If maxgap is None, then forward fill all
+
+    assert(marr.ndim == 1)
+
+    result = marr.copy()
+
+
+    currGap = 0
+
+    if maxgap is not None:
+
+        for i in range(1, result.size):
+                currGap += 1
+                if currGap <= maxgap and not result.mask[i-1]:
+                    result.data[i] = result.data[i-1]
+                elif currGap == maxgap + 1:
+            else:
+                currGap = 0
+
+    else:
+
+        for i in range(1, result.size):
+                result.data[i] = result.data[i-1]
+
+    return result
+
+
+def backward_fill(marr, maxgap=None):
+    """backward_fill(marr, maxgap=None)
+
+backward fill masked values in a 1-d array when there are <= maxgap
+consecutive masked values. If maxgap is None, then backward fill all
+
+    return forward_fill(marr[::-1], maxgap=maxgap)[::-1]
+
+
+
+Interpolate masked values in marr according to method kind.
+kind must be one of 'constant', 'linear', 'cubic', quintic'
+"""
+
+    kind = kind.lower()
+
+    if kind == 'constant': return forward_fill(marr)
+
+    if marr.ndim != 1: raise ValueError("array must be 1 dimensional!")
+
+
+    if unmaskedIndices.size < 2: return marr.copy()
+
+    try:
+        k = {'linear' : 1,
+             'cubic' : 3,
+             'quintic' : 5}[kind.lower()]
+    except KeyError:
+        raise ValueError("Unsupported interpolation type.")
+
+
+
+    tck = fitpack.splrep(unmaskedIndices, vals, k=k)
+