-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: change Index set ops sort=True -> sort=None #25063
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
TomAugspurger
merged 24 commits into
pandas-dev:master
from
jorisvandenbossche:TomAugspurger-24959-union-default_0241
Feb 1, 2019
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
aac172c
[WIP]: API: Change default for Index.union sort
TomAugspurger d4bcc55
update test
TomAugspurger 45c827c
fixups
TomAugspurger 68b72a6
Merge remote-tracking branch 'upstream/master' into 24959-union-default
TomAugspurger 8716f97
multi
TomAugspurger f7056d5
doc typo
TomAugspurger e82cbb1
intersection
TomAugspurger 2a2de25
whatsnew
TomAugspurger 5c3da74
update whatsnew
TomAugspurger ce6d1db
Merge remote-tracking branch 'upstream/master' into 24959-union-default
TomAugspurger 52a2f2f
symdiff
TomAugspurger bb848f1
whatsnew
TomAugspurger b15dc7e
doc
TomAugspurger 27b5b16
index multi
TomAugspurger 1564d4f
Change True to None; disallow True
jorisvandenbossche cb54640
fix concat tests
jorisvandenbossche 05a0ed0
pep8
jorisvandenbossche 5e1b831
keep inconsistent behaviour for MultiIndex.difference
jorisvandenbossche 32a5966
docstring updates
jorisvandenbossche d234a1d
fix linting + catch remaining RuntimeWarning
jorisvandenbossche 260aba2
more linting
jorisvandenbossche 41c24f0
update whatsnew
TomAugspurger 2e181ac
xfail trues
TomAugspurger 1c5a037
fixup [ci skip]
TomAugspurger 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
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 |
---|---|---|
|
@@ -2245,18 +2245,37 @@ def _get_reconciled_name_object(self, other): | |
return self._shallow_copy(name=name) | ||
return self | ||
|
||
def union(self, other, sort=True): | ||
def _validate_sort_keyword(self, sort): | ||
if sort not in [None, False]: | ||
raise ValueError("The 'sort' keyword only takes the values of " | ||
"None or False; {0} was passed.".format(sort)) | ||
|
||
def union(self, other, sort=None): | ||
""" | ||
Form the union of two Index objects. | ||
|
||
Parameters | ||
---------- | ||
other : Index or array-like | ||
sort : bool, default True | ||
Sort the resulting index if possible | ||
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 | ||
|
@@ -2269,6 +2288,7 @@ def union(self, other, sort=True): | |
>>> idx1.union(idx2) | ||
Int64Index([1, 2, 3, 4, 5, 6], dtype='int64') | ||
""" | ||
self._validate_sort_keyword(sort) | ||
self._assert_can_do_setop(other) | ||
other = ensure_index(other) | ||
|
||
|
@@ -2319,7 +2339,7 @@ def union(self, other, sort=True): | |
else: | ||
result = lvals | ||
|
||
if sort: | ||
if sort is None: | ||
try: | ||
result = sorting.safe_sort(result) | ||
except TypeError as e: | ||
|
@@ -2342,14 +2362,19 @@ def intersection(self, other, sort=False): | |
Parameters | ||
---------- | ||
other : Index or array-like | ||
sort : bool, default False | ||
Sort the resulting index if possible | ||
sort : False or None, default False | ||
Whether to sort the resulting index. | ||
|
||
* False : do not sort the result. | ||
* None : sort the result, except when `self` and `other` are equal | ||
or when the values cannot be compared. | ||
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. "...are equal, either one is empty, or when ..." |
||
|
||
.. versionadded:: 0.24.0 | ||
|
||
.. versionchanged:: 0.24.1 | ||
|
||
Changed the default from ``True`` to ``False``. | ||
Changed the default from ``True`` to ``False``, to match | ||
the behaviour of 0.23.4 and earlier. | ||
|
||
Returns | ||
------- | ||
|
@@ -2363,6 +2388,7 @@ def intersection(self, other, sort=False): | |
>>> idx1.intersection(idx2) | ||
Int64Index([3, 4], dtype='int64') | ||
""" | ||
self._validate_sort_keyword(sort) | ||
self._assert_can_do_setop(other) | ||
other = ensure_index(other) | ||
|
||
|
@@ -2402,7 +2428,7 @@ def intersection(self, other, sort=False): | |
|
||
taken = other.take(indexer) | ||
|
||
if sort: | ||
if sort is None: | ||
taken = sorting.safe_sort(taken.values) | ||
if self.name != other.name: | ||
name = None | ||
|
@@ -2415,7 +2441,7 @@ def intersection(self, other, sort=False): | |
|
||
return taken | ||
|
||
def difference(self, other, sort=True): | ||
def difference(self, other, sort=None): | ||
""" | ||
Return a new Index with elements from the index that are not in | ||
`other`. | ||
|
@@ -2425,11 +2451,22 @@ def difference(self, other, sort=True): | |
Parameters | ||
---------- | ||
other : Index or array-like | ||
sort : bool, default True | ||
Sort the resulting index if possible | ||
sort : False or None, default None | ||
Whether to sort the resulting index. By default, the | ||
values are attempted to be sorted, but any TypeError from | ||
incomparable elements is caught by pandas. | ||
|
||
* None : Attempt to sort the result, but catch any TypeErrors | ||
from comparing incomparable elements. | ||
* 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 | ||
------- | ||
difference : Index | ||
|
@@ -2444,6 +2481,7 @@ def difference(self, other, sort=True): | |
>>> idx1.difference(idx2, sort=False) | ||
Int64Index([2, 1], dtype='int64') | ||
""" | ||
self._validate_sort_keyword(sort) | ||
self._assert_can_do_setop(other) | ||
|
||
if self.equals(other): | ||
|
@@ -2460,27 +2498,38 @@ def difference(self, other, sort=True): | |
label_diff = np.setdiff1d(np.arange(this.size), indexer, | ||
assume_unique=True) | ||
the_diff = this.values.take(label_diff) | ||
if sort: | ||
if sort is None: | ||
try: | ||
the_diff = sorting.safe_sort(the_diff) | ||
except TypeError: | ||
pass | ||
|
||
return this._shallow_copy(the_diff, name=result_name, freq=None) | ||
|
||
def symmetric_difference(self, other, result_name=None, sort=True): | ||
def symmetric_difference(self, other, result_name=None, sort=None): | ||
""" | ||
Compute the symmetric difference of two Index objects. | ||
|
||
Parameters | ||
---------- | ||
other : Index or array-like | ||
result_name : str | ||
sort : bool, default True | ||
Sort the resulting index if possible | ||
sort : False or None, default None | ||
Whether to sort the resulting index. By default, the | ||
values are attempted to be sorted, but any TypeError from | ||
incomparable elements is caught by pandas. | ||
|
||
* None : Attempt to sort the result, but catch any TypeErrors | ||
from comparing incomparable elements. | ||
* 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 | ||
------- | ||
symmetric_difference : Index | ||
|
@@ -2504,6 +2553,7 @@ def symmetric_difference(self, other, result_name=None, sort=True): | |
>>> idx1 ^ idx2 | ||
Int64Index([1, 5], dtype='int64') | ||
""" | ||
self._validate_sort_keyword(sort) | ||
self._assert_can_do_setop(other) | ||
other, result_name_update = self._convert_can_do_setop(other) | ||
if result_name is None: | ||
|
@@ -2524,7 +2574,7 @@ def symmetric_difference(self, other, result_name=None, sort=True): | |
right_diff = other.values.take(right_indexer) | ||
|
||
the_diff = _concat._concat_compat([left_diff, right_diff]) | ||
if sort: | ||
if sort is None: | ||
try: | ||
the_diff = sorting.safe_sort(the_diff) | ||
except TypeError: | ||
|
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"...are not identical or either is empty."?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For intersection that'd be empty, which we can claim is sorted :)