-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add sort parameter to RangeIndex.union (#24471) #25788
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0b38695
ENH: Add sort parameter to RangeIndex.union (#24471)
reidy-p 2dac97c
Make tests a bit clearer using fixture
reidy-p 3d4c8d6
Fix fixture in test and move sort check
reidy-p 311b12c
Update fixture
reidy-p 7cf5269
Move aliases and add sort parameter to other union test
reidy-p 9b65818
Add in missing whitespace
reidy-p c32ca8b
Add fixture to append test
reidy-p 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
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 |
---|---|---|
|
@@ -11,6 +11,12 @@ | |
|
||
from .test_numeric import Numeric | ||
|
||
# aliases to make some tests easier to read | ||
RI = RangeIndex | ||
I64 = Int64Index | ||
F64 = Float64Index | ||
OI = Index | ||
|
||
|
||
class TestRangeIndex(Numeric): | ||
_holder = RangeIndex | ||
|
@@ -565,51 +571,73 @@ def test_intersection(self, sort): | |
expected = RangeIndex(0, 0, 1) | ||
tm.assert_index_equal(result, expected) | ||
|
||
def test_union_noncomparable(self): | ||
@pytest.mark.parametrize('sort', [False, None]) | ||
def test_union_noncomparable(self, sort): | ||
from datetime import datetime, timedelta | ||
# corner case, non-Int64Index | ||
now = datetime.now() | ||
other = Index([now + timedelta(i) for i in range(4)], dtype=object) | ||
result = self.index.union(other) | ||
result = self.index.union(other, sort=sort) | ||
expected = Index(np.concatenate((self.index, other))) | ||
tm.assert_index_equal(result, expected) | ||
|
||
result = other.union(self.index) | ||
result = other.union(self.index, sort=sort) | ||
expected = Index(np.concatenate((other, self.index))) | ||
tm.assert_index_equal(result, expected) | ||
|
||
def test_union(self): | ||
RI = RangeIndex | ||
I64 = Int64Index | ||
cases = [(RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1)), | ||
(RI(0, 10, 1), RI(5, 20, 1), RI(0, 20, 1)), | ||
(RI(0, 10, 1), RI(10, 20, 1), RI(0, 20, 1)), | ||
(RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1)), | ||
(RI(0, -10, -1), RI(-10, -20, -1), RI(-19, 1, 1)), | ||
(RI(0, 10, 2), RI(1, 10, 2), RI(0, 10, 1)), | ||
(RI(0, 11, 2), RI(1, 12, 2), RI(0, 12, 1)), | ||
(RI(0, 21, 4), RI(-2, 24, 4), RI(-2, 24, 2)), | ||
(RI(0, -20, -2), RI(-1, -21, -2), RI(-19, 1, 1)), | ||
(RI(0, 100, 5), RI(0, 100, 20), RI(0, 100, 5)), | ||
(RI(0, -100, -5), RI(5, -100, -20), RI(-95, 10, 5)), | ||
(RI(0, -11, -1), RI(1, -12, -4), RI(-11, 2, 1)), | ||
(RI(0), RI(0), RI(0)), | ||
(RI(0, -10, -2), RI(0), RI(0, -10, -2)), | ||
(RI(0, 100, 2), RI(100, 150, 200), RI(0, 102, 2)), | ||
(RI(0, -100, -2), RI(-100, 50, 102), RI(-100, 4, 2)), | ||
(RI(0, -100, -1), RI(0, -50, -3), RI(-99, 1, 1)), | ||
(RI(0, 1, 1), RI(5, 6, 10), RI(0, 6, 5)), | ||
(RI(0, 10, 5), RI(-5, -6, -20), RI(-5, 10, 5)), | ||
(RI(0, 3, 1), RI(4, 5, 1), I64([0, 1, 2, 4])), | ||
(RI(0, 10, 1), I64([]), RI(0, 10, 1)), | ||
(RI(0), I64([1, 5, 6]), I64([1, 5, 6]))] | ||
for idx1, idx2, expected in cases: | ||
res1 = idx1.union(idx2) | ||
res2 = idx2.union(idx1) | ||
res3 = idx1._int64index.union(idx2) | ||
tm.assert_index_equal(res1, expected, exact=True) | ||
tm.assert_index_equal(res2, expected, exact=True) | ||
tm.assert_index_equal(res3, expected) | ||
@pytest.fixture(params=[ | ||
(RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1)), | ||
(RI(0, 10, 1), RI(5, 20, 1), RI(0, 20, 1), I64(range(20))), | ||
(RI(0, 10, 1), RI(10, 20, 1), RI(0, 20, 1), I64(range(20))), | ||
(RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1)), | ||
(RI(0, -10, -1), RI(-10, -20, -1), RI(-19, 1, 1), | ||
I64(range(0, -20, -1))), | ||
(RI(0, 10, 2), RI(1, 10, 2), RI(0, 10, 1), | ||
I64(list(range(0, 10, 2)) + list(range(1, 10, 2)))), | ||
(RI(0, 11, 2), RI(1, 12, 2), RI(0, 12, 1), | ||
I64(list(range(0, 11, 2)) + list(range(1, 12, 2)))), | ||
(RI(0, 21, 4), RI(-2, 24, 4), RI(-2, 24, 2), | ||
I64(list(range(0, 21, 4)) + list(range(-2, 24, 4)))), | ||
(RI(0, -20, -2), RI(-1, -21, -2), RI(-19, 1, 1), | ||
I64(list(range(0, -20, -2)) + list(range(-1, -21, -2)))), | ||
(RI(0, 100, 5), RI(0, 100, 20), RI(0, 100, 5), I64(range(0, 100, 5))), | ||
(RI(0, -100, -5), RI(5, -100, -20), RI(-95, 10, 5), | ||
I64(list(range(0, -100, -5)) + [5])), | ||
(RI(0, -11, -1), RI(1, -12, -4), RI(-11, 2, 1), | ||
I64(list(range(0, -11, -1)) + [1, -11])), | ||
(RI(0), RI(0), RI(0), RI(0)), | ||
(RI(0, -10, -2), RI(0), RI(0, -10, -2), RI(0, -10, -2)), | ||
(RI(0, 100, 2), RI(100, 150, 200), RI(0, 102, 2), | ||
I64(range(0, 102, 2))), | ||
(RI(0, -100, -2), RI(-100, 50, 102), RI(-100, 4, 2), | ||
I64(list(range(0, -100, -2)) + [-100, 2])), | ||
(RI(0, -100, -1), RI(0, -50, -3), RI(-99, 1, 1), | ||
I64(list(range(0, -100, -1)))), | ||
(RI(0, 1, 1), RI(5, 6, 10), RI(0, 6, 5), I64([0, 5])), | ||
(RI(0, 10, 5), RI(-5, -6, -20), RI(-5, 10, 5), I64([0, 5, -5])), | ||
(RI(0, 3, 1), RI(4, 5, 1), I64([0, 1, 2, 4]), I64([0, 1, 2, 4])), | ||
(RI(0, 10, 1), I64([]), RI(0, 10, 1), RI(0, 10, 1)), | ||
(RI(0), I64([1, 5, 6]), I64([1, 5, 6]), I64([1, 5, 6])) | ||
]) | ||
def unions(self, request): | ||
"""Inputs and expected outputs for RangeIndex.union tests""" | ||
|
||
return request.param | ||
|
||
def test_union_sorted(self, unions): | ||
|
||
idx1, idx2, expected_sorted, expected_notsorted = unions | ||
|
||
res1 = idx1.union(idx2, sort=None) | ||
tm.assert_index_equal(res1, expected_sorted, exact=True) | ||
|
||
res1 = idx1.union(idx2, sort=False) | ||
tm.assert_index_equal(res1, expected_notsorted, exact=True) | ||
|
||
res2 = idx2.union(idx1, sort=None) | ||
res3 = idx1._int64index.union(idx2, sort=None) | ||
tm.assert_index_equal(res2, expected_sorted, exact=True) | ||
tm.assert_index_equal(res3, expected_sorted) | ||
|
||
def test_nbytes(self): | ||
|
||
|
@@ -840,38 +868,41 @@ def test_len_specialised(self): | |
i = RangeIndex(0, 5, step) | ||
assert len(i) == 0 | ||
|
||
def test_append(self): | ||
@pytest.fixture(params=[ | ||
([RI(1, 12, 5)], RI(1, 12, 5)), | ||
([RI(0, 6, 4)], RI(0, 6, 4)), | ||
([RI(1, 3), RI(3, 7)], RI(1, 7)), | ||
([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)), | ||
([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)), | ||
([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)), | ||
([RI(-4, -8), RI(-8, -12)], RI(0, 0)), | ||
([RI(-4, -8), RI(3, -4)], RI(0, 0)), | ||
([RI(-4, -8), RI(3, 5)], RI(3, 5)), | ||
([RI(-4, -2), RI(3, 5)], I64([-4, -3, 3, 4])), | ||
([RI(-2,), RI(3, 5)], RI(3, 5)), | ||
([RI(2,), RI(2)], I64([0, 1, 0, 1])), | ||
([RI(2,), RI(2, 5), RI(5, 8, 4)], RI(0, 6)), | ||
([RI(2,), RI(3, 5), RI(5, 8, 4)], I64([0, 1, 3, 4, 5])), | ||
([RI(-2, 2), RI(2, 5), RI(5, 8, 4)], RI(-2, 6)), | ||
([RI(3,), I64([-1, 3, 15])], I64([0, 1, 2, -1, 3, 15])), | ||
([RI(3,), F64([-1, 3.1, 15.])], F64([0, 1, 2, -1, 3.1, 15.])), | ||
([RI(3,), OI(['a', None, 14])], OI([0, 1, 2, 'a', None, 14])), | ||
([RI(3, 1), OI(['a', None, 14])], OI(['a', None, 14])) | ||
]) | ||
def appends(self, request): | ||
"""Inputs and expected outputs for RangeIndex.append test""" | ||
|
||
return request.param | ||
|
||
def test_append(self, appends): | ||
# GH16212 | ||
RI = RangeIndex | ||
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. can you parmaterize this one as well (similar to above) |
||
I64 = Int64Index | ||
F64 = Float64Index | ||
OI = Index | ||
cases = [([RI(1, 12, 5)], RI(1, 12, 5)), | ||
([RI(0, 6, 4)], RI(0, 6, 4)), | ||
([RI(1, 3), RI(3, 7)], RI(1, 7)), | ||
([RI(1, 5, 2), RI(5, 6)], RI(1, 6, 2)), | ||
([RI(1, 3, 2), RI(4, 7, 3)], RI(1, 7, 3)), | ||
([RI(-4, 3, 2), RI(4, 7, 2)], RI(-4, 7, 2)), | ||
([RI(-4, -8), RI(-8, -12)], RI(0, 0)), | ||
([RI(-4, -8), RI(3, -4)], RI(0, 0)), | ||
([RI(-4, -8), RI(3, 5)], RI(3, 5)), | ||
([RI(-4, -2), RI(3, 5)], I64([-4, -3, 3, 4])), | ||
([RI(-2,), RI(3, 5)], RI(3, 5)), | ||
([RI(2,), RI(2)], I64([0, 1, 0, 1])), | ||
([RI(2,), RI(2, 5), RI(5, 8, 4)], RI(0, 6)), | ||
([RI(2,), RI(3, 5), RI(5, 8, 4)], I64([0, 1, 3, 4, 5])), | ||
([RI(-2, 2), RI(2, 5), RI(5, 8, 4)], RI(-2, 6)), | ||
([RI(3,), I64([-1, 3, 15])], I64([0, 1, 2, -1, 3, 15])), | ||
([RI(3,), F64([-1, 3.1, 15.])], F64([0, 1, 2, -1, 3.1, 15.])), | ||
([RI(3,), OI(['a', None, 14])], OI([0, 1, 2, 'a', None, 14])), | ||
([RI(3, 1), OI(['a', None, 14])], OI(['a', None, 14])) | ||
] | ||
|
||
for indices, expected in cases: | ||
result = indices[0].append(indices[1:]) | ||
tm.assert_index_equal(result, expected, exact=True) | ||
|
||
if len(indices) == 2: | ||
# Append single item rather than list | ||
result2 = indices[0].append(indices[1]) | ||
tm.assert_index_equal(result2, expected, exact=True) | ||
|
||
indices, expected = appends | ||
|
||
result = indices[0].append(indices[1:]) | ||
tm.assert_index_equal(result, expected, exact=True) | ||
|
||
if len(indices) == 2: | ||
# Append single item rather than list | ||
result2 = indices[0].append(indices[1]) | ||
tm.assert_index_equal(result2, expected, exact=True) |
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.