Exemple #1
0
    def get_indexer(self, target, method=None):
        """

        Parameters
        ----------
        target : Index
        method :

        Returns
        -------
        (indexer, mask)
        """
        if method:
            method = method.upper()

        aliases = {
            'FFILL' : 'PAD',
            'BFILL' : 'BACKFILL'
        }

        target = _ensure_index(target)

        method = aliases.get(method, method)
        indexer, mask = _tseries.getFillVec(self, target, self.indexMap,
                                            target.indexMap, method)
        return indexer, mask
Exemple #2
0
    def get_indexer(self, target, method=None):
        """
        Compute indexer and mask for new index given the current index. The
        indexer should be then used as an input to ndarray.take to align the
        current data to the new index. The mask determines whether labels are
        found or not in the current index

        Parameters
        ----------
        target : MultiIndex or Index (of tuples)
        method : {'pad', 'ffill', 'backfill', 'bfill'}
            pad / ffill: propagate LAST valid observation forward to next valid
            backfill / bfill: use NEXT valid observation to fill gap

        Notes
        -----
        This is a low-level method and probably should be used at your own risk

        Examples
        --------
        >>> indexer, mask = index.get_indexer(new_index)
        >>> new_values = cur_values.take(indexer)
        >>> new_values[-mask] = np.nan

        Returns
        -------
        (indexer, mask) : (ndarray, ndarray)
        """
        if method:
            method = method.upper()

        aliases = {
            'FFILL' : 'PAD',
            'BFILL' : 'BACKFILL'
        }
        method = aliases.get(method, method)

        if isinstance(target, MultiIndex):
            target_index = target.get_tuple_index()
        else:
            if len(target) > 0:
                val = target[0]
                if not isinstance(val, tuple) or len(val) != self.nlevels:
                    raise ValueError('can only pass MultiIndex or '
                                     'array of tuples')

            target_index = target

        self_index = self.get_tuple_index()
        indexer, mask = _tseries.getFillVec(self_index, target_index,
                                            self_index.indexMap,
                                            target.indexMap, method)
        return indexer, mask
Exemple #3
0
    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))
Exemple #4
0
    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))
Exemple #5
0
    def get_indexer(self, target, method=None):
        """
        Compute indexer and mask for new index given the current index. The
        indexer should be then used as an input to ndarray.take to align the
        current data to the new index. The mask determines whether labels are
        found or not in the current index

        Parameters
        ----------
        target : Index
        method : {'pad', 'ffill', 'backfill', 'bfill'}
            pad / ffill: propagate LAST valid observation forward to next valid
            backfill / bfill: use NEXT valid observation to fill gap

        Notes
        -----
        This is a low-level method and probably should be used at your own risk

        Examples
        --------
        >>> indexer, mask = index.get_indexer(new_index)
        >>> new_values = cur_values.take(indexer)
        >>> new_values[-mask] = np.nan

        Returns
        -------
        (indexer, mask) : (ndarray, ndarray)
        """
        if method:
            method = method.upper()

        aliases = {
            'FFILL' : 'PAD',
            'BFILL' : 'BACKFILL'
        }

        target = _ensure_index(target)

        method = aliases.get(method, method)
        indexer, mask = _tseries.getFillVec(self, target, self.indexMap,
                                            target.indexMap, method)
        return indexer, mask
Exemple #6
0
    def get_indexer(self, target, method=None):
        """

        Parameters
        ----------
        target : Index
        method :

        Returns
        -------
        (indexer, mask)
        """
        if method:
            method = method.upper()

        aliases = {
            'FFILL' : 'PAD',
            'BFILL' : 'BACKFILL'
        }
        method = aliases.get(method, method)

        if isinstance(target, MultiIndex):
            target_index = target.get_tuple_index()
        else:
            if len(target) > 0:
                val = target[0]
                if not isinstance(val, tuple) or len(val) != self.nlevels:
                    raise ValueError('can only pass MultiIndex or '
                                     'array of tuples')

            target_index = target

        self_index = self.get_tuple_index()
        indexer, mask = _tseries.getFillVec(self_index, target_index,
                                            self_index.indexMap,
                                            target.indexMap, method)
        return indexer, mask