-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: share code for set-like ops in DTI/TDI/PI #31335
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
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5184606
REF: share code for set-like ops in DTI/TDI/PI
jbrockmendel 0351105
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel c1c28ca
fixup unused import
jbrockmendel 128dfe9
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 15ac491
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 300529c
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel 06a245d
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel c0c901c
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -697,12 +697,28 @@ def _assert_can_do_setop(self, other): | |
if isinstance(other, PeriodIndex) and self.freq != other.freq: | ||
raise raise_on_incompatible(self, other) | ||
|
||
def intersection(self, other, sort=False): | ||
def _setop(self, other, sort, opname: str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eventually if you can type / doc-string this |
||
""" | ||
Perform a set operation by dispatching to the Int64Index implementation. | ||
""" | ||
self._validate_sort_keyword(sort) | ||
self._assert_can_do_setop(other) | ||
res_name = get_op_result_name(self, other) | ||
other = ensure_index(other) | ||
|
||
i8self = Int64Index._simple_new(self.asi8) | ||
i8other = Int64Index._simple_new(other.asi8) | ||
i8result = getattr(i8self, opname)(i8other, sort=sort) | ||
|
||
parr = type(self._data)(np.asarray(i8result, dtype=np.int64), dtype=self.dtype) | ||
result = type(self)._simple_new(parr, name=res_name) | ||
return result | ||
|
||
def intersection(self, other, sort=False): | ||
self._validate_sort_keyword(sort) | ||
self._assert_can_do_setop(other) | ||
other = ensure_index(other) | ||
|
||
if self.equals(other): | ||
return self._get_reconciled_name_object(other) | ||
|
||
|
@@ -712,35 +728,24 @@ def intersection(self, other, sort=False): | |
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 | ||
return self._setop(other, sort, opname="intersection") | ||
|
||
def difference(self, other, sort=None): | ||
self._validate_sort_keyword(sort) | ||
self._assert_can_do_setop(other) | ||
res_name = get_op_result_name(self, other) | ||
other = ensure_index(other) | ||
|
||
if self.equals(other): | ||
# pass an empty PeriodArray with the appropriate dtype | ||
return self._shallow_copy(self._data[:0]) | ||
return type(self)._simple_new(self._data[:0], name=self.name) | ||
|
||
if is_object_dtype(other): | ||
return self.astype(object).difference(other).astype(self.dtype) | ||
|
||
elif not is_dtype_equal(self.dtype, other.dtype): | ||
return self | ||
|
||
i8self = Int64Index._simple_new(self.asi8) | ||
i8other = Int64Index._simple_new(other.asi8) | ||
i8result = i8self.difference(i8other, sort=sort) | ||
|
||
result = self._shallow_copy(np.asarray(i8result, dtype=np.int64), name=res_name) | ||
return result | ||
return self._setop(other, sort, opname="difference") | ||
|
||
def _union(self, other, sort): | ||
if not len(other) or self.equals(other) or not len(self): | ||
|
@@ -754,13 +759,7 @@ def _union(self, other, sort): | |
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 | ||
return self._setop(other, sort, opname="_union") | ||
|
||
# ------------------------------------------------------------------------ | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.