def union(self, other, sort=None):
self._validate_sort_keyword(sort)<EOL>self._assert_can_do_setop(other)<EOL>other = ensure_index(other)<EOL>if len(other) == <NUM_LIT:0> or self.equals(other):<EOL><INDENT>return self._get_reconciled_name_object(other)<EOL><DEDENT>if len(self) == <NUM_LIT:0>:<EOL><INDENT>return other._get_reconciled_name_object(self)<EOL><DEDENT>if not is_dtype_union_equal(self.dtype, other.dtype):<EOL><INDENT>this = self.astype('<STR_LIT:O>')<EOL>other = other.astype('<STR_LIT:O>')<EOL>return this.union(other, sort=sort)<EOL><DEDENT>if is_period_dtype(self) or is_datetime64tz_dtype(self):<EOL><INDENT>lvals = self._ndarray_values<EOL><DEDENT>else:<EOL><INDENT>lvals = self._values<EOL><DEDENT>if is_period_dtype(other) or is_datetime64tz_dtype(other):<EOL><INDENT>rvals = other._ndarray_values<EOL><DEDENT>else:<EOL><INDENT>rvals = other._values<EOL><DEDENT>if sort is None and self.is_monotonic and other.is_monotonic:<EOL><INDENT>try:<EOL><INDENT>result = self._outer_indexer(lvals, rvals)[<NUM_LIT:0>]<EOL><DEDENT>except TypeError:<EOL><INDENT>result = list(lvals)<EOL>value_set = set(lvals)<EOL>result.extend([x for x in rvals if x not in value_set])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>indexer = self.get_indexer(other)<EOL>indexer, = (indexer == -<NUM_LIT:1>).nonzero()<EOL>if len(indexer) > <NUM_LIT:0>:<EOL><INDENT>other_diff = algos.take_nd(rvals, indexer,<EOL>allow_fill=False)<EOL>result = _concat._concat_compat((lvals, other_diff))<EOL><DEDENT>else:<EOL><INDENT>result = lvals<EOL><DEDENT>if sort is None:<EOL><INDENT>try:<EOL><INDENT>result = sorting.safe_sort(result)<EOL><DEDENT>except TypeError as e:<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>".format(e),<EOL>RuntimeWarning, stacklevel=<NUM_LIT:3>)<EOL><DEDENT><DEDENT><DEDENT>return self._wrap_setop_result(other, result)<EOL>
Form the union of two Index objects.

Parameters
----------
other : Index or array-like
sort : bool or None, default None
    Whether to sort the resulting Index.

    * None : Sort the result, except when

      1. `self` and `other` are equal.
      2. `self` or `other` has length 0.
      3. Some values in `self` or `other` cannot be compared.
         A RuntimeWarning is issued in this case.

    * False : do not sort the result.

    .. versionadded:: 0.24.0

    .. versionchanged:: 0.24.1

       Changed the default value from ``True`` to ``None``
       (without change in behaviour).

Returns
-------
union : Index

Examples
--------

>>> idx1 = pd.Index([1, 2, 3, 4])
>>> idx2 = pd.Index([3, 4, 5, 6])
>>> idx1.union(idx2)
Int64Index([1, 2, 3, 4, 5, 6], dtype='int64')