Skip to content

REF: collect Index setops tests #30529

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 4 commits into from
Dec 30, 2019
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
2 changes: 1 addition & 1 deletion pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_can_hold_identifiers(self):
(lambda idx: ["a", "b"] + idx, "__radd__"),
],
)
def test_disallow_set_ops(self, func, op_name):
def test_disallow_addsub_ops(self, func, op_name):
# GH 10039
# set ops (+/-) raise TypeError
idx = pd.Index(pd.Categorical(["a", "b"]))
Expand Down
19 changes: 0 additions & 19 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,6 @@ def test_no_millisecond_field(self):
with pytest.raises(AttributeError, match=msg):
DatetimeIndex([]).millisecond

@pytest.mark.parametrize("sort", [None, False])
def test_difference_freq(self, sort):
# GH14323: difference of Period MUST preserve frequency
# but the ability to union results must be preserved

index = period_range("20160920", "20160925", freq="D")

other = period_range("20160921", "20160924", freq="D")
expected = PeriodIndex(["20160920", "20160925"], freq="D")
idx_diff = index.difference(other, sort)
tm.assert_index_equal(idx_diff, expected)
tm.assert_attr_equal("freq", idx_diff, expected)

other = period_range("20160922", "20160925", freq="D")
idx_diff = index.difference(other, sort)
expected = PeriodIndex(["20160920", "20160921"], freq="D")
tm.assert_index_equal(idx_diff, expected)
tm.assert_attr_equal("freq", idx_diff, expected)

def test_hash_error(self):
index = period_range("20010101", periods=10)
msg = f"unhashable type: '{type(index).__name__}'"
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexes/period/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,22 @@ def test_difference(self, sort):
if sort is None:
expected = expected.sort_values()
tm.assert_index_equal(result_difference, expected)

@pytest.mark.parametrize("sort", [None, False])
def test_difference_freq(self, sort):
# GH14323: difference of Period MUST preserve frequency
# but the ability to union results must be preserved

index = period_range("20160920", "20160925", freq="D")

other = period_range("20160921", "20160924", freq="D")
expected = PeriodIndex(["20160920", "20160925"], freq="D")
idx_diff = index.difference(other, sort)
tm.assert_index_equal(idx_diff, expected)
tm.assert_attr_equal("freq", idx_diff, expected)

other = period_range("20160922", "20160925", freq="D")
idx_diff = index.difference(other, sort)
expected = PeriodIndex(["20160920", "20160921"], freq="D")
tm.assert_index_equal(idx_diff, expected)
tm.assert_attr_equal("freq", idx_diff, expected)
172 changes: 0 additions & 172 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from datetime import datetime, timedelta

import numpy as np
import pytest

Expand Down Expand Up @@ -464,176 +462,6 @@ def test_join_self(self, join_type):
joined = index.join(index, how=join_type)
assert index is joined

@pytest.mark.parametrize("sort", [None, False])
def test_intersection(self, sort):
# intersect with Int64Index
index = self.create_index()
other = Index(np.arange(1, 6))
result = index.intersection(other, sort=sort)
expected = Index(np.sort(np.intersect1d(index.values, other.values)))
tm.assert_index_equal(result, expected)

result = other.intersection(index, sort=sort)
expected = Index(
np.sort(np.asarray(np.intersect1d(index.values, other.values)))
)
tm.assert_index_equal(result, expected)

# intersect with increasing RangeIndex
other = RangeIndex(1, 6)
result = index.intersection(other, sort=sort)
expected = Index(np.sort(np.intersect1d(index.values, other.values)))
tm.assert_index_equal(result, expected)

# intersect with decreasing RangeIndex
other = RangeIndex(5, 0, -1)
result = index.intersection(other, sort=sort)
expected = Index(np.sort(np.intersect1d(index.values, other.values)))
tm.assert_index_equal(result, expected)

# reversed (GH 17296)
result = other.intersection(index, sort=sort)
tm.assert_index_equal(result, expected)

# GH 17296: intersect two decreasing RangeIndexes
first = RangeIndex(10, -2, -2)
other = RangeIndex(5, -4, -1)
expected = first.astype(int).intersection(other.astype(int), sort=sort)
result = first.intersection(other, sort=sort).astype(int)
tm.assert_index_equal(result, expected)

# reversed
result = other.intersection(first, sort=sort).astype(int)
tm.assert_index_equal(result, expected)

index = RangeIndex(5)

# intersect of non-overlapping indices
other = RangeIndex(5, 10, 1)
result = index.intersection(other, sort=sort)
expected = RangeIndex(0, 0, 1)
tm.assert_index_equal(result, expected)

other = RangeIndex(-1, -5, -1)
result = index.intersection(other, sort=sort)
expected = RangeIndex(0, 0, 1)
tm.assert_index_equal(result, expected)

# intersection of empty indices
other = RangeIndex(0, 0, 1)
result = index.intersection(other, sort=sort)
expected = RangeIndex(0, 0, 1)
tm.assert_index_equal(result, expected)

result = other.intersection(index, sort=sort)
tm.assert_index_equal(result, expected)

# intersection of non-overlapping values based on start value and gcd
index = RangeIndex(1, 10, 2)
other = RangeIndex(0, 10, 4)
result = index.intersection(other, sort=sort)
expected = RangeIndex(0, 0, 1)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("sort", [False, None])
def test_union_noncomparable(self, sort):
# corner case, non-Int64Index
index = self.create_index()
other = Index([datetime.now() + timedelta(i) for i in range(4)], dtype=object)
result = index.union(other, sort=sort)
expected = Index(np.concatenate((index, other)))
tm.assert_index_equal(result, expected)

result = other.union(index, sort=sort)
expected = Index(np.concatenate((other, index)))
tm.assert_index_equal(result, 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):

# memory savings vs int index
Expand Down
Loading