

def union(self, other):
    "\n        Form the union of two Index objects and sorts if possible.\n\n        Parameters\n        ----------\n        other : Index or array-like\n\n        Returns\n        -------\n        union : Index\n\n        Examples\n        --------\n\n        >>> idx1 = pd.Index([1, 2, 3, 4])\n        >>> idx2 = pd.Index([3, 4, 5, 6])\n        >>> idx1.union(idx2)\n        Int64Index([1, 2, 3, 4, 5, 6], dtype='int64')\n\n        "
    self._assert_can_do_setop(other)
    other = _ensure_index(other)
    if ((len(other) == 0) or self.equals(other)):
        return self
    if (len(self) == 0):
        return other
    if (not com.is_dtype_equal(self.dtype, other.dtype)):
        this = self.astype('O')
        other = other.astype('O')
        return this.union(other)
    if (self.is_monotonic and other.is_monotonic):
        try:
            result = self._outer_indexer(self.values, other._values)[0]
        except TypeError:
            result = list(self.values)
            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()
        if (len(indexer) > 0):
            other_diff = algos.take_nd(other._values, indexer, allow_fill=False)
            result = _concat._concat_compat((self.values, other_diff))
            try:
                (self.values[0] < other_diff[0])
            except TypeError as e:
                warnings.warn(('%s, sort order is undefined for incomparable objects' % e), RuntimeWarning, stacklevel=3)
            else:
                types = frozenset((self.inferred_type, other.inferred_type))
                if (not (types & _unsortable_types)):
                    result.sort()
        else:
            result = self.values
            try:
                result = np.sort(result)
            except TypeError as e:
                warnings.warn(('%s, sort order is undefined for incomparable objects' % e), RuntimeWarning, stacklevel=3)
    return self._wrap_union_result(other, result)

dataset/ETHPy150Open pydata/pandas/pandas/indexes/base.py Index.union/original