Example #1
0
    def _join_monotonic(self, other, how='left', return_indexers=False):
        this_vals = self.values

        if self.dtype != other.dtype:
            other = Index(other, dtype=object)
        other_vals = other.values

        if how == 'left':
            join_index = self
            lidx = None
            ridx = lib.left_join_indexer_object(self, other)
        elif how == 'right':
            join_index = other
            lidx = lib.left_join_indexer_object(other, self)
            ridx = None
        elif how == 'inner':
            join_index, lidx, ridx = lib.inner_join_indexer_object(this_vals,
                                                                   other_vals)
            join_index = self._wrap_joined_index(join_index, other)
        elif how == 'outer':
            join_index, lidx, ridx = lib.outer_join_indexer_object(this_vals,
                                                                   other_vals)
            join_index = self._wrap_joined_index(join_index, other)
        else:  # pragma: no cover
            raise Exception('do not recognize join method %s' % how)

        if return_indexers:
            return join_index, lidx, ridx
        else:
            return join_index
Example #2
0
    def union(self, other):
        """
        Form the union of two Index objects and sorts if possible

        Parameters
        ----------
        other : Index or array-like

        Returns
        -------
        union : Index
        """
        if not hasattr(other, '__iter__'):
            raise Exception('Input must be iterable!')

        if len(other) == 0 or self.equals(other):
            return self
        if len(self) == 0:
            return _ensure_index(other)

        if self.is_monotonic and other.is_monotonic:
            if other.dtype != np.object_:
                other = Index(other, dtype=object)

            try:
                result = lib.outer_join_indexer_object(self, other.values)[0]
            except TypeError:
                # incomparable objects
                result = list(self.values)

                # worth making this faster? a very unusual case
                value_set = set(self.values)
                result.extend([x for x in other.values if x not in value_set])
        else:
            indexer = self.get_indexer(other)
            indexer = (indexer == -1).nonzero()[0]

            if len(indexer) > 0:
                other_diff = other.values.take(indexer)
                result = list(self) + list(other_diff)
                # timsort wins
                try:
                    result.sort()
                except Exception:
                    pass
            else:
                # contained in
                result = sorted(self)

        # for subclasses
        return self._wrap_union_result(other, result)
Example #3
0
    def union(self, other):
        """
        Form the union of two Index objects and sorts if possible

        Parameters
        ----------
        other : Index or array-like

        Returns
        -------
        union : Index
        """
        if not hasattr(other, "__iter__"):
            raise Exception("Input must be iterable!")

        if len(other) == 0 or self.equals(other):
            return self
        if len(self) == 0:
            return _ensure_index(other)

        if self.is_monotonic and other.is_monotonic:
            result = lib.outer_join_indexer_object(self, other)[0]
        else:
            indexer = self.get_indexer(other)
            indexer = (indexer == -1).nonzero()[0]

            if len(indexer) > 0:
                other_diff = other.values.take(indexer)
                result = list(self) + list(other_diff)
                # timsort wins
                try:
                    result.sort()
                except Exception:
                    pass
            else:
                # contained in
                result = sorted(self)

        # for subclasses
        return self._wrap_union_result(other, result)
Example #4
0
    def _join_monotonic(self, other, how="left", return_indexers=False):
        if how == "left":
            join_index = self
            lidx = None
            ridx = lib.left_join_indexer_object(self, other)
        elif how == "right":
            join_index = other
            lidx = lib.left_join_indexer_object(other, self)
            ridx = None
        elif how == "inner":
            join_index, lidx, ridx = lib.inner_join_indexer_object(self, other)
            join_index = Index(join_index)
        elif how == "outer":
            join_index, lidx, ridx = lib.outer_join_indexer_object(self, other)
            join_index = Index(join_index)
        else:  # pragma: no cover
            raise Exception("do not recognize join method %s" % how)

        if return_indexers:
            return join_index, lidx, ridx
        else:
            return join_index