def get_indexer(source, target, fill_method): if fill_method: fill_method = fill_method.upper() indexer, mask = tseries.getFillVec(source, target, source.indexMap, target.indexMap, fill_method) return indexer, mask
def reindex(self, new_index, method=None, fillMethod=None): """Conform Series to new Index Parameters ---------- new_index : array-like Preferably an Index object (to avoid duplicating data) method : {'backfill', 'pad', None} Method to use for filling holes in reindexed Series pad : propagate last valid observation forward to next valid backfill : use NEXT valid observation to fill gap Returns ------- reindexed : Series """ if fillMethod is not None: # pragma: no cover warnings.warn("'fillMethod' is deprecated. Use 'method' instead", FutureWarning) method = fillMethod if self.index.equals(new_index): return self.copy() if not isinstance(new_index, Index): new_index = Index(new_index) if len(self.index) == 0: return Series(NaN, index=new_index) if method is not None: method = method.upper() # Cython for blazing speed fillVec, mask = tseries.getFillVec(self.index, new_index, self.index.indexMap, new_index.indexMap, kind=method) newValues = self.values.take(fillVec) notmask = -mask if notmask.any(): if issubclass(newValues.dtype.type, np.int_): newValues = newValues.astype(float) elif issubclass(newValues.dtype.type, np.bool_): newValues = newValues.astype(object) np.putmask(newValues, notmask, NaN) return Series(newValues, index=new_index)
def test_pad(self): old = Index([1, 5, 10]) new = Index(range(12)) filler, mask = tseries.getFillVec(old, new, old.indexMap, new.indexMap, "PAD") expect_filler = [-1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] expect_mask = np.ones(12, dtype=bool) expect_mask[0] = False self.assert_(np.array_equal(filler, expect_filler)) self.assert_(np.array_equal(mask, expect_mask)) # corner case old = Index([5, 10]) new = Index(range(5)) filler, mask = tseries.getFillVec(old, new, old.indexMap, new.indexMap, "PAD") expect_filler = [-1, -1, -1, -1, -1] expect_mask = np.zeros(5, dtype=bool) self.assert_(np.array_equal(filler, expect_filler)) self.assert_(np.array_equal(mask, expect_mask))
def test_getMergeVec(self): old = Index([1, 5, 10]) new = Index(range(12)) filler, mask = tseries.getFillVec(old, new, old.indexMap, new.indexMap, None) expect_filler = [-1, 0, -1, -1, -1, 1, -1, -1, -1, -1, 2, -1] expect_mask = np.zeros(12, dtype=bool) expect_mask[[1, 5, 10]] = True self.assert_(np.array_equal(filler, expect_filler)) self.assert_(np.array_equal(mask, expect_mask)) # corner case old = Index([1, 4]) new = Index(range(5, 10)) filler, mask = tseries.getFillVec(old, new, old.indexMap, new.indexMap, None) expect_filler = [-1, -1, -1, -1, -1] expect_mask = np.zeros(5, dtype=bool) self.assert_(np.array_equal(filler, expect_filler)) self.assert_(np.array_equal(mask, expect_mask))
def reindex(self, newIndex, fillMethod=None): """Overloaded version of reindex for TimeSeries. Supports filling with values based on new index. See analogous method for DataFrame, will be faster for multiple TimeSeries Parameters ---------- newIndex : array-like, preferably an Index object (to avoid duplicating data) fillMethod : {'backfill', 'pad', 'interpolate', None} Method to use for filling holes in reindexed Series Returns ------- TimeSeries """ if self.index.equals(newIndex): return self.copy() if not isinstance(newIndex, Index): newIndex = Index(newIndex) if len(self.index) == 0: return Series.fromValue(NaN, index=newIndex) if fillMethod is not None: fillMethod = fillMethod.upper() # Cython for blazing speed fillVec, mask = tseries.getFillVec(self.index, newIndex, self.index.indexMap, newIndex.indexMap, kind=fillMethod) newValues = self.values.take(fillVec) notmask = -mask if notmask.any(): if issubclass(newValues.dtype.type, np.int_): newValues = newValues.astype(float) elif issubclass(newValues.dtype.type, np.bool_): newValues = newValues.astype(object) np.putmask(newValues, notmask, NaN) return Series(newValues, index=newIndex)
def test_pad(self): old = Index([1, 5, 10]) new = Index(range(12)) filler, mask = tseries.getFillVec(old, new, old.indexMap, new.indexMap, 'PAD') expect_filler = [-1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] expect_mask = np.ones(12, dtype=bool) expect_mask[0] = False self.assert_(np.array_equal(filler, expect_filler)) self.assert_(np.array_equal(mask, expect_mask)) # corner case old = Index([5, 10]) new = Index(range(5)) filler, mask = tseries.getFillVec(old, new, old.indexMap, new.indexMap, 'PAD') expect_filler = [-1, -1, -1, -1, -1] expect_mask = np.zeros(5, dtype=bool) self.assert_(np.array_equal(filler, expect_filler)) self.assert_(np.array_equal(mask, expect_mask))