Skip to content

Commit 67b56dd

Browse files
jbrockmendeljreback
authored andcommitted
REF: PeriodIndex._union (#30803)
1 parent b62ef64 commit 67b56dd

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

pandas/core/indexes/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,11 +2310,11 @@ def _union(self, other, sort):
23102310
return other._get_reconciled_name_object(self)
23112311

23122312
# TODO(EA): setops-refactor, clean all this up
2313-
if is_period_dtype(self) or is_datetime64tz_dtype(self):
2313+
if is_datetime64tz_dtype(self):
23142314
lvals = self._ndarray_values
23152315
else:
23162316
lvals = self._values
2317-
if is_period_dtype(other) or is_datetime64tz_dtype(other):
2317+
if is_datetime64tz_dtype(other):
23182318
rvals = other._ndarray_values
23192319
else:
23202320
rvals = other._values

pandas/core/indexes/category.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ def values(self):
388388

389389
def _wrap_setop_result(self, other, result):
390390
name = get_op_result_name(self, other)
391+
# We use _shallow_copy rather than the Index implementation
392+
# (which uses _constructor) in order to preserve dtype.
391393
return self._shallow_copy(result, name=name)
392394

393395
@Appender(_index_shared_docs["contains"] % _index_doc_kwargs)

pandas/core/indexes/period.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,6 @@ def _assert_can_do_setop(self, other):
768768
if isinstance(other, PeriodIndex) and self.freq != other.freq:
769769
raise raise_on_incompatible(self, other)
770770

771-
def _wrap_setop_result(self, other, result):
772-
name = get_op_result_name(self, other)
773-
result = self._apply_meta(result)
774-
result.name = name
775-
return result
776-
777771
def intersection(self, other, sort=False):
778772
self._validate_sort_keyword(sort)
779773
self._assert_can_do_setop(other)
@@ -819,6 +813,26 @@ def difference(self, other, sort=None):
819813
result = self._shallow_copy(np.asarray(i8result, dtype=np.int64), name=res_name)
820814
return result
821815

816+
def _union(self, other, sort):
817+
if not len(other) or self.equals(other) or not len(self):
818+
return super()._union(other, sort=sort)
819+
820+
# We are called by `union`, which is responsible for this validation
821+
assert isinstance(other, type(self))
822+
823+
if not is_dtype_equal(self.dtype, other.dtype):
824+
this = self.astype("O")
825+
other = other.astype("O")
826+
return this._union(other, sort=sort)
827+
828+
i8self = Int64Index._simple_new(self.asi8)
829+
i8other = Int64Index._simple_new(other.asi8)
830+
i8result = i8self._union(i8other, sort=sort)
831+
832+
res_name = get_op_result_name(self, other)
833+
result = self._shallow_copy(np.asarray(i8result, dtype=np.int64), name=res_name)
834+
return result
835+
822836
# ------------------------------------------------------------------------
823837

824838
def _apply_meta(self, rawarr):

0 commit comments

Comments
 (0)