Skip to content

ENH: Implement PeriodIndex.intersection without object-dtype cast #30666

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 3 commits into from
Jan 4, 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
21 changes: 5 additions & 16 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2408,14 +2408,8 @@ def intersection(self, other, sort=False):
return this.intersection(other, sort=sort)

# TODO(EA): setops-refactor, clean all this up
if is_period_dtype(self):
lvals = self._ndarray_values
else:
lvals = self._values
if is_period_dtype(other):
rvals = other._ndarray_values
else:
rvals = other._values
lvals = self._values
rvals = other._values

if self.is_monotonic and other.is_monotonic:
try:
Expand All @@ -2434,18 +2428,13 @@ def intersection(self, other, sort=False):
indexer = indexer[indexer != -1]

taken = other.take(indexer)
res_name = get_op_result_name(self, other)

if sort is None:
taken = algos.safe_sort(taken.values)
if self.name != other.name:
name = None
else:
name = self.name
return self._shallow_copy(taken, name=name)

if self.name != other.name:
taken.name = None
return self._shallow_copy(taken, name=res_name)

taken.name = res_name
return taken

def difference(self, other, sort=None):
Expand Down
9 changes: 3 additions & 6 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,9 @@ def intersection(self, other, sort=False):
# this point, depending on the values.

result._set_freq(None)
if hasattr(self, "tz"):
result = self._shallow_copy(
result._values, name=result.name, tz=result.tz, freq=None
)
else:
result = self._shallow_copy(result._values, name=result.name, freq=None)
result = self._shallow_copy(
result._data, name=result.name, dtype=result.dtype, freq=None
)
if result.freq is None:
result._set_freq("infer")
return result
Expand Down
28 changes: 28 additions & 0 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ensure_platform_int,
is_bool_dtype,
is_datetime64_any_dtype,
is_dtype_equal,
is_float,
is_float_dtype,
is_integer,
Expand Down Expand Up @@ -782,6 +783,9 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)
return self._apply_meta(result), lidx, ridx
return self._apply_meta(result)

# ------------------------------------------------------------------------
# Set Operation Methods

def _assert_can_do_setop(self, other):
super()._assert_can_do_setop(other)

Expand All @@ -799,6 +803,30 @@ def _wrap_setop_result(self, other, result):
result.name = name
return result

def intersection(self, other, sort=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type at some point

self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
res_name = get_op_result_name(self, other)
other = ensure_index(other)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this code be shared with superclasses?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think so, its going to be a few steps before we get there

if self.equals(other):
return self._get_reconciled_name_object(other)

if not is_dtype_equal(self.dtype, other.dtype):
# TODO: fastpath for if we have a different PeriodDtype
this = self.astype("O")
other = other.astype("O")
return this.intersection(other, sort=sort)

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

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

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

def _apply_meta(self, rawarr):
if not isinstance(rawarr, PeriodIndex):
rawarr = PeriodIndex._simple_new(rawarr, freq=self.freq, name=self.name)
Expand Down