Skip to content

REF: PeriodIndex._union #30803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2310,11 +2310,11 @@ def _union(self, other, sort):
return other._get_reconciled_name_object(self)

# TODO(EA): setops-refactor, clean all this up
if is_period_dtype(self) or is_datetime64tz_dtype(self):
if is_datetime64tz_dtype(self):
lvals = self._ndarray_values
else:
lvals = self._values
if is_period_dtype(other) or is_datetime64tz_dtype(other):
if is_datetime64tz_dtype(other):
rvals = other._ndarray_values
else:
rvals = other._values
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ def values(self):

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

@Appender(_index_shared_docs["contains"] % _index_doc_kwargs)
Expand Down
26 changes: 20 additions & 6 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,12 +768,6 @@ def _assert_can_do_setop(self, other):
if isinstance(other, PeriodIndex) and self.freq != other.freq:
raise raise_on_incompatible(self, other)

def _wrap_setop_result(self, other, result):
name = get_op_result_name(self, other)
result = self._apply_meta(result)
result.name = name
return result

def intersection(self, other, sort=False):
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
Expand Down Expand Up @@ -819,6 +813,26 @@ def difference(self, other, sort=None):
result = self._shallow_copy(np.asarray(i8result, dtype=np.int64), name=res_name)
return result

def _union(self, other, sort):
if not len(other) or self.equals(other) or not len(self):
return super()._union(other, sort=sort)

# We are called by `union`, which is responsible for this validation
assert isinstance(other, type(self))

if not is_dtype_equal(self.dtype, other.dtype):
this = self.astype("O")
other = other.astype("O")
return this._union(other, sort=sort)

i8self = Int64Index._simple_new(self.asi8)
i8other = Int64Index._simple_new(other.asi8)
i8result = i8self._union(i8other, sort=sort)

res_name = get_op_result_name(self, other)
result = self._shallow_copy(np.asarray(i8result, dtype=np.int64), name=res_name)
return result

# ------------------------------------------------------------------------

def _apply_meta(self, rawarr):
Expand Down