Skip to content

Commit 60d5200

Browse files
jbrockmendeljreback
authored andcommitted
REF: collect Index setops tests (#30529)
1 parent cf99831 commit 60d5200

File tree

8 files changed

+312
-238
lines changed

8 files changed

+312
-238
lines changed

pandas/tests/indexes/categorical/test_category.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_can_hold_identifiers(self):
4343
(lambda idx: ["a", "b"] + idx, "__radd__"),
4444
],
4545
)
46-
def test_disallow_set_ops(self, func, op_name):
46+
def test_disallow_addsub_ops(self, func, op_name):
4747
# GH 10039
4848
# set ops (+/-) raise TypeError
4949
idx = pd.Index(pd.Categorical(["a", "b"]))

pandas/tests/indexes/period/test_period.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,6 @@ def test_no_millisecond_field(self):
105105
with pytest.raises(AttributeError, match=msg):
106106
DatetimeIndex([]).millisecond
107107

108-
@pytest.mark.parametrize("sort", [None, False])
109-
def test_difference_freq(self, sort):
110-
# GH14323: difference of Period MUST preserve frequency
111-
# but the ability to union results must be preserved
112-
113-
index = period_range("20160920", "20160925", freq="D")
114-
115-
other = period_range("20160921", "20160924", freq="D")
116-
expected = PeriodIndex(["20160920", "20160925"], freq="D")
117-
idx_diff = index.difference(other, sort)
118-
tm.assert_index_equal(idx_diff, expected)
119-
tm.assert_attr_equal("freq", idx_diff, expected)
120-
121-
other = period_range("20160922", "20160925", freq="D")
122-
idx_diff = index.difference(other, sort)
123-
expected = PeriodIndex(["20160920", "20160921"], freq="D")
124-
tm.assert_index_equal(idx_diff, expected)
125-
tm.assert_attr_equal("freq", idx_diff, expected)
126-
127108
def test_hash_error(self):
128109
index = period_range("20010101", periods=10)
129110
msg = f"unhashable type: '{type(index).__name__}'"

pandas/tests/indexes/period/test_setops.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,22 @@ def test_difference(self, sort):
353353
if sort is None:
354354
expected = expected.sort_values()
355355
tm.assert_index_equal(result_difference, expected)
356+
357+
@pytest.mark.parametrize("sort", [None, False])
358+
def test_difference_freq(self, sort):
359+
# GH14323: difference of Period MUST preserve frequency
360+
# but the ability to union results must be preserved
361+
362+
index = period_range("20160920", "20160925", freq="D")
363+
364+
other = period_range("20160921", "20160924", freq="D")
365+
expected = PeriodIndex(["20160920", "20160925"], freq="D")
366+
idx_diff = index.difference(other, sort)
367+
tm.assert_index_equal(idx_diff, expected)
368+
tm.assert_attr_equal("freq", idx_diff, expected)
369+
370+
other = period_range("20160922", "20160925", freq="D")
371+
idx_diff = index.difference(other, sort)
372+
expected = PeriodIndex(["20160920", "20160921"], freq="D")
373+
tm.assert_index_equal(idx_diff, expected)
374+
tm.assert_attr_equal("freq", idx_diff, expected)

pandas/tests/indexes/ranges/test_range.py

Lines changed: 0 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from datetime import datetime, timedelta
2-
31
import numpy as np
42
import pytest
53

@@ -464,176 +462,6 @@ def test_join_self(self, join_type):
464462
joined = index.join(index, how=join_type)
465463
assert index is joined
466464

467-
@pytest.mark.parametrize("sort", [None, False])
468-
def test_intersection(self, sort):
469-
# intersect with Int64Index
470-
index = self.create_index()
471-
other = Index(np.arange(1, 6))
472-
result = index.intersection(other, sort=sort)
473-
expected = Index(np.sort(np.intersect1d(index.values, other.values)))
474-
tm.assert_index_equal(result, expected)
475-
476-
result = other.intersection(index, sort=sort)
477-
expected = Index(
478-
np.sort(np.asarray(np.intersect1d(index.values, other.values)))
479-
)
480-
tm.assert_index_equal(result, expected)
481-
482-
# intersect with increasing RangeIndex
483-
other = RangeIndex(1, 6)
484-
result = index.intersection(other, sort=sort)
485-
expected = Index(np.sort(np.intersect1d(index.values, other.values)))
486-
tm.assert_index_equal(result, expected)
487-
488-
# intersect with decreasing RangeIndex
489-
other = RangeIndex(5, 0, -1)
490-
result = index.intersection(other, sort=sort)
491-
expected = Index(np.sort(np.intersect1d(index.values, other.values)))
492-
tm.assert_index_equal(result, expected)
493-
494-
# reversed (GH 17296)
495-
result = other.intersection(index, sort=sort)
496-
tm.assert_index_equal(result, expected)
497-
498-
# GH 17296: intersect two decreasing RangeIndexes
499-
first = RangeIndex(10, -2, -2)
500-
other = RangeIndex(5, -4, -1)
501-
expected = first.astype(int).intersection(other.astype(int), sort=sort)
502-
result = first.intersection(other, sort=sort).astype(int)
503-
tm.assert_index_equal(result, expected)
504-
505-
# reversed
506-
result = other.intersection(first, sort=sort).astype(int)
507-
tm.assert_index_equal(result, expected)
508-
509-
index = RangeIndex(5)
510-
511-
# intersect of non-overlapping indices
512-
other = RangeIndex(5, 10, 1)
513-
result = index.intersection(other, sort=sort)
514-
expected = RangeIndex(0, 0, 1)
515-
tm.assert_index_equal(result, expected)
516-
517-
other = RangeIndex(-1, -5, -1)
518-
result = index.intersection(other, sort=sort)
519-
expected = RangeIndex(0, 0, 1)
520-
tm.assert_index_equal(result, expected)
521-
522-
# intersection of empty indices
523-
other = RangeIndex(0, 0, 1)
524-
result = index.intersection(other, sort=sort)
525-
expected = RangeIndex(0, 0, 1)
526-
tm.assert_index_equal(result, expected)
527-
528-
result = other.intersection(index, sort=sort)
529-
tm.assert_index_equal(result, expected)
530-
531-
# intersection of non-overlapping values based on start value and gcd
532-
index = RangeIndex(1, 10, 2)
533-
other = RangeIndex(0, 10, 4)
534-
result = index.intersection(other, sort=sort)
535-
expected = RangeIndex(0, 0, 1)
536-
tm.assert_index_equal(result, expected)
537-
538-
@pytest.mark.parametrize("sort", [False, None])
539-
def test_union_noncomparable(self, sort):
540-
# corner case, non-Int64Index
541-
index = self.create_index()
542-
other = Index([datetime.now() + timedelta(i) for i in range(4)], dtype=object)
543-
result = index.union(other, sort=sort)
544-
expected = Index(np.concatenate((index, other)))
545-
tm.assert_index_equal(result, expected)
546-
547-
result = other.union(index, sort=sort)
548-
expected = Index(np.concatenate((other, index)))
549-
tm.assert_index_equal(result, expected)
550-
551-
@pytest.fixture(
552-
params=[
553-
(RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1), RI(0, 10, 1)),
554-
(RI(0, 10, 1), RI(5, 20, 1), RI(0, 20, 1), I64(range(20))),
555-
(RI(0, 10, 1), RI(10, 20, 1), RI(0, 20, 1), I64(range(20))),
556-
(RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1), RI(0, -10, -1)),
557-
(RI(0, -10, -1), RI(-10, -20, -1), RI(-19, 1, 1), I64(range(0, -20, -1))),
558-
(
559-
RI(0, 10, 2),
560-
RI(1, 10, 2),
561-
RI(0, 10, 1),
562-
I64(list(range(0, 10, 2)) + list(range(1, 10, 2))),
563-
),
564-
(
565-
RI(0, 11, 2),
566-
RI(1, 12, 2),
567-
RI(0, 12, 1),
568-
I64(list(range(0, 11, 2)) + list(range(1, 12, 2))),
569-
),
570-
(
571-
RI(0, 21, 4),
572-
RI(-2, 24, 4),
573-
RI(-2, 24, 2),
574-
I64(list(range(0, 21, 4)) + list(range(-2, 24, 4))),
575-
),
576-
(
577-
RI(0, -20, -2),
578-
RI(-1, -21, -2),
579-
RI(-19, 1, 1),
580-
I64(list(range(0, -20, -2)) + list(range(-1, -21, -2))),
581-
),
582-
(RI(0, 100, 5), RI(0, 100, 20), RI(0, 100, 5), I64(range(0, 100, 5))),
583-
(
584-
RI(0, -100, -5),
585-
RI(5, -100, -20),
586-
RI(-95, 10, 5),
587-
I64(list(range(0, -100, -5)) + [5]),
588-
),
589-
(
590-
RI(0, -11, -1),
591-
RI(1, -12, -4),
592-
RI(-11, 2, 1),
593-
I64(list(range(0, -11, -1)) + [1, -11]),
594-
),
595-
(RI(0), RI(0), RI(0), RI(0)),
596-
(RI(0, -10, -2), RI(0), RI(0, -10, -2), RI(0, -10, -2)),
597-
(RI(0, 100, 2), RI(100, 150, 200), RI(0, 102, 2), I64(range(0, 102, 2))),
598-
(
599-
RI(0, -100, -2),
600-
RI(-100, 50, 102),
601-
RI(-100, 4, 2),
602-
I64(list(range(0, -100, -2)) + [-100, 2]),
603-
),
604-
(
605-
RI(0, -100, -1),
606-
RI(0, -50, -3),
607-
RI(-99, 1, 1),
608-
I64(list(range(0, -100, -1))),
609-
),
610-
(RI(0, 1, 1), RI(5, 6, 10), RI(0, 6, 5), I64([0, 5])),
611-
(RI(0, 10, 5), RI(-5, -6, -20), RI(-5, 10, 5), I64([0, 5, -5])),
612-
(RI(0, 3, 1), RI(4, 5, 1), I64([0, 1, 2, 4]), I64([0, 1, 2, 4])),
613-
(RI(0, 10, 1), I64([]), RI(0, 10, 1), RI(0, 10, 1)),
614-
(RI(0), I64([1, 5, 6]), I64([1, 5, 6]), I64([1, 5, 6])),
615-
]
616-
)
617-
def unions(self, request):
618-
"""Inputs and expected outputs for RangeIndex.union tests"""
619-
620-
return request.param
621-
622-
def test_union_sorted(self, unions):
623-
624-
idx1, idx2, expected_sorted, expected_notsorted = unions
625-
626-
res1 = idx1.union(idx2, sort=None)
627-
tm.assert_index_equal(res1, expected_sorted, exact=True)
628-
629-
res1 = idx1.union(idx2, sort=False)
630-
tm.assert_index_equal(res1, expected_notsorted, exact=True)
631-
632-
res2 = idx2.union(idx1, sort=None)
633-
res3 = idx1._int64index.union(idx2, sort=None)
634-
tm.assert_index_equal(res2, expected_sorted, exact=True)
635-
tm.assert_index_equal(res3, expected_sorted)
636-
637465
def test_nbytes(self):
638466

639467
# memory savings vs int index

0 commit comments

Comments
 (0)