Skip to content

Commit 500a7dc

Browse files
authored
TST/REF: collect Index setop, indexing tests (#37773)
1 parent 76fd718 commit 500a7dc

File tree

7 files changed

+366
-337
lines changed

7 files changed

+366
-337
lines changed

pandas/tests/indexes/common.py

Lines changed: 3 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ def test_numpy_argsort(self, index):
363363
# cannot be changed at the moment due to
364364
# backwards compatibility concerns
365365
if isinstance(type(index), (CategoricalIndex, RangeIndex)):
366+
# TODO: why type(index)?
366367
msg = "the 'axis' parameter is not supported"
367368
with pytest.raises(ValueError, match=msg):
368369
np.argsort(index, axis=1)
@@ -375,49 +376,6 @@ def test_numpy_argsort(self, index):
375376
with pytest.raises(ValueError, match=msg):
376377
np.argsort(index, order=("a", "b"))
377378

378-
def test_take(self, index):
379-
indexer = [4, 3, 0, 2]
380-
if len(index) < 5:
381-
# not enough elements; ignore
382-
return
383-
384-
result = index.take(indexer)
385-
expected = index[indexer]
386-
assert result.equals(expected)
387-
388-
if not isinstance(index, (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
389-
# GH 10791
390-
msg = r"'(.*Index)' object has no attribute 'freq'"
391-
with pytest.raises(AttributeError, match=msg):
392-
index.freq
393-
394-
def test_take_invalid_kwargs(self):
395-
idx = self.create_index()
396-
indices = [1, 2]
397-
398-
msg = r"take\(\) got an unexpected keyword argument 'foo'"
399-
with pytest.raises(TypeError, match=msg):
400-
idx.take(indices, foo=2)
401-
402-
msg = "the 'out' parameter is not supported"
403-
with pytest.raises(ValueError, match=msg):
404-
idx.take(indices, out=indices)
405-
406-
msg = "the 'mode' parameter is not supported"
407-
with pytest.raises(ValueError, match=msg):
408-
idx.take(indices, mode="clip")
409-
410-
def test_take_minus1_without_fill(self, index):
411-
# -1 does not get treated as NA unless allow_fill=True is passed
412-
if len(index) == 0:
413-
# Test is not applicable
414-
return
415-
416-
result = index.take([0, 0, -1])
417-
418-
expected = index.take([0, 0, len(index) - 1])
419-
tm.assert_index_equal(result, expected)
420-
421379
def test_repeat(self):
422380
rep = 2
423381
i = self.create_index()
@@ -456,117 +414,6 @@ def test_where(self, klass):
456414
result = i.where(klass(cond))
457415
tm.assert_index_equal(result, expected)
458416

459-
@pytest.mark.parametrize("case", [0.5, "xxx"])
460-
@pytest.mark.parametrize(
461-
"method", ["intersection", "union", "difference", "symmetric_difference"]
462-
)
463-
def test_set_ops_error_cases(self, case, method, index):
464-
# non-iterable input
465-
msg = "Input must be Index or array-like"
466-
with pytest.raises(TypeError, match=msg):
467-
getattr(index, method)(case)
468-
469-
def test_intersection_base(self, index):
470-
if isinstance(index, CategoricalIndex):
471-
return
472-
473-
first = index[:5]
474-
second = index[:3]
475-
intersect = first.intersection(second)
476-
assert tm.equalContents(intersect, second)
477-
478-
if is_datetime64tz_dtype(index.dtype):
479-
# The second.values below will drop tz, so the rest of this test
480-
# is not applicable.
481-
return
482-
483-
# GH 10149
484-
cases = [klass(second.values) for klass in [np.array, Series, list]]
485-
for case in cases:
486-
result = first.intersection(case)
487-
assert tm.equalContents(result, second)
488-
489-
if isinstance(index, MultiIndex):
490-
msg = "other must be a MultiIndex or a list of tuples"
491-
with pytest.raises(TypeError, match=msg):
492-
first.intersection([1, 2, 3])
493-
494-
def test_union_base(self, index):
495-
first = index[3:]
496-
second = index[:5]
497-
everything = index
498-
union = first.union(second)
499-
assert tm.equalContents(union, everything)
500-
501-
if is_datetime64tz_dtype(index.dtype):
502-
# The second.values below will drop tz, so the rest of this test
503-
# is not applicable.
504-
return
505-
506-
# GH 10149
507-
cases = [klass(second.values) for klass in [np.array, Series, list]]
508-
for case in cases:
509-
if not isinstance(index, CategoricalIndex):
510-
result = first.union(case)
511-
assert tm.equalContents(result, everything), (
512-
result,
513-
everything,
514-
type(case),
515-
)
516-
517-
if isinstance(index, MultiIndex):
518-
msg = "other must be a MultiIndex or a list of tuples"
519-
with pytest.raises(TypeError, match=msg):
520-
first.union([1, 2, 3])
521-
522-
def test_difference_base(self, sort, index):
523-
first = index[2:]
524-
second = index[:4]
525-
if isinstance(index, CategoricalIndex) or index.is_boolean():
526-
answer = []
527-
else:
528-
answer = index[4:]
529-
result = first.difference(second, sort)
530-
assert tm.equalContents(result, answer)
531-
532-
# GH 10149
533-
cases = [klass(second.values) for klass in [np.array, Series, list]]
534-
for case in cases:
535-
if isinstance(index, (DatetimeIndex, TimedeltaIndex)):
536-
assert type(result) == type(answer)
537-
tm.assert_numpy_array_equal(
538-
result.sort_values().asi8, answer.sort_values().asi8
539-
)
540-
else:
541-
result = first.difference(case, sort)
542-
assert tm.equalContents(result, answer)
543-
544-
if isinstance(index, MultiIndex):
545-
msg = "other must be a MultiIndex or a list of tuples"
546-
with pytest.raises(TypeError, match=msg):
547-
first.difference([1, 2, 3], sort)
548-
549-
def test_symmetric_difference(self, index):
550-
if isinstance(index, CategoricalIndex):
551-
return
552-
553-
first = index[1:]
554-
second = index[:-1]
555-
answer = index[[0, -1]]
556-
result = first.symmetric_difference(second)
557-
assert tm.equalContents(result, answer)
558-
559-
# GH 10149
560-
cases = [klass(second.values) for klass in [np.array, Series, list]]
561-
for case in cases:
562-
result = first.symmetric_difference(case)
563-
assert tm.equalContents(result, answer)
564-
565-
if isinstance(index, MultiIndex):
566-
msg = "other must be a MultiIndex or a list of tuples"
567-
with pytest.raises(TypeError, match=msg):
568-
first.symmetric_difference([1, 2, 3])
569-
570417
def test_insert_base(self, index):
571418
result = index[1:4]
572419

@@ -601,7 +448,8 @@ def test_delete_base(self, index):
601448

602449
def test_equals(self, index):
603450
if isinstance(index, IntervalIndex):
604-
# IntervalIndex tested separately
451+
# IntervalIndex tested separately, the index.equals(index.astype(object))
452+
# fails for IntervalIndex
605453
return
606454

607455
assert index.equals(index)

pandas/tests/indexes/numeric/test_indexing.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import Float64Index, Int64Index, Series, UInt64Index
4+
from pandas import Float64Index, Index, Int64Index, Series, UInt64Index
55
import pandas._testing as tm
66

77

@@ -241,3 +241,20 @@ def test_contains_float64_nans(self):
241241
def test_contains_float64_not_nans(self):
242242
index = Float64Index([1.0, 2.0, np.nan])
243243
assert 1.0 in index
244+
245+
246+
class TestGetSliceBounds:
247+
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
248+
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
249+
def test_get_slice_bounds_within(self, kind, side, expected):
250+
index = Index(range(6))
251+
result = index.get_slice_bound(4, kind=kind, side=side)
252+
assert result == expected
253+
254+
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
255+
@pytest.mark.parametrize("side", ["left", "right"])
256+
@pytest.mark.parametrize("bound, expected", [(-1, 0), (10, 6)])
257+
def test_get_slice_bounds_outside(self, kind, side, expected, bound):
258+
index = Index(range(6))
259+
result = index.get_slice_bound(bound, kind=kind, side=side)
260+
assert result == expected

pandas/tests/indexes/test_base.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,27 +1082,6 @@ def test_symmetric_difference_non_index(self, sort):
10821082
assert tm.equalContents(result, expected)
10831083
assert result.name == "new_name"
10841084

1085-
def test_difference_type(self, index, sort):
1086-
# GH 20040
1087-
# If taking difference of a set and itself, it
1088-
# needs to preserve the type of the index
1089-
if not index.is_unique:
1090-
return
1091-
result = index.difference(index, sort=sort)
1092-
expected = index.drop(index)
1093-
tm.assert_index_equal(result, expected)
1094-
1095-
def test_intersection_difference(self, index, sort):
1096-
# GH 20040
1097-
# Test that the intersection of an index with an
1098-
# empty index produces the same index as the difference
1099-
# of an index with itself. Test for all types
1100-
if not index.is_unique:
1101-
return
1102-
inter = index.intersection(index.drop(index))
1103-
diff = index.difference(index, sort=sort)
1104-
tm.assert_index_equal(inter, diff)
1105-
11061085
def test_is_mixed_deprecated(self):
11071086
# GH#32922
11081087
index = self.create_index()

pandas/tests/indexes/test_common.py

Lines changed: 0 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -71,139 +71,6 @@ def test_getitem_error(self, index, itm):
7171
with pytest.raises(IndexError):
7272
index[itm]
7373

74-
@pytest.mark.parametrize(
75-
"fname, sname, expected_name",
76-
[
77-
("A", "A", "A"),
78-
("A", "B", None),
79-
("A", None, None),
80-
(None, "B", None),
81-
(None, None, None),
82-
],
83-
)
84-
def test_corner_union(self, index, fname, sname, expected_name):
85-
# GH 9943 9862
86-
# Test unions with various name combinations
87-
# Do not test MultiIndex or repeats
88-
89-
if isinstance(index, MultiIndex) or not index.is_unique:
90-
pytest.skip("Not for MultiIndex or repeated indices")
91-
92-
# Test copy.union(copy)
93-
first = index.copy().set_names(fname)
94-
second = index.copy().set_names(sname)
95-
union = first.union(second)
96-
expected = index.copy().set_names(expected_name)
97-
tm.assert_index_equal(union, expected)
98-
99-
# Test copy.union(empty)
100-
first = index.copy().set_names(fname)
101-
second = index.drop(index).set_names(sname)
102-
union = first.union(second)
103-
expected = index.copy().set_names(expected_name)
104-
tm.assert_index_equal(union, expected)
105-
106-
# Test empty.union(copy)
107-
first = index.drop(index).set_names(fname)
108-
second = index.copy().set_names(sname)
109-
union = first.union(second)
110-
expected = index.copy().set_names(expected_name)
111-
tm.assert_index_equal(union, expected)
112-
113-
# Test empty.union(empty)
114-
first = index.drop(index).set_names(fname)
115-
second = index.drop(index).set_names(sname)
116-
union = first.union(second)
117-
expected = index.drop(index).set_names(expected_name)
118-
tm.assert_index_equal(union, expected)
119-
120-
@pytest.mark.parametrize(
121-
"fname, sname, expected_name",
122-
[
123-
("A", "A", "A"),
124-
("A", "B", None),
125-
("A", None, None),
126-
(None, "B", None),
127-
(None, None, None),
128-
],
129-
)
130-
def test_union_unequal(self, index, fname, sname, expected_name):
131-
if isinstance(index, MultiIndex) or not index.is_unique:
132-
pytest.skip("Not for MultiIndex or repeated indices")
133-
134-
# test copy.union(subset) - need sort for unicode and string
135-
first = index.copy().set_names(fname)
136-
second = index[1:].set_names(sname)
137-
union = first.union(second).sort_values()
138-
expected = index.set_names(expected_name).sort_values()
139-
tm.assert_index_equal(union, expected)
140-
141-
@pytest.mark.parametrize(
142-
"fname, sname, expected_name",
143-
[
144-
("A", "A", "A"),
145-
("A", "B", None),
146-
("A", None, None),
147-
(None, "B", None),
148-
(None, None, None),
149-
],
150-
)
151-
def test_corner_intersect(self, index, fname, sname, expected_name):
152-
# GH35847
153-
# Test intersections with various name combinations
154-
155-
if isinstance(index, MultiIndex) or not index.is_unique:
156-
pytest.skip("Not for MultiIndex or repeated indices")
157-
158-
# Test copy.intersection(copy)
159-
first = index.copy().set_names(fname)
160-
second = index.copy().set_names(sname)
161-
intersect = first.intersection(second)
162-
expected = index.copy().set_names(expected_name)
163-
tm.assert_index_equal(intersect, expected)
164-
165-
# Test copy.intersection(empty)
166-
first = index.copy().set_names(fname)
167-
second = index.drop(index).set_names(sname)
168-
intersect = first.intersection(second)
169-
expected = index.drop(index).set_names(expected_name)
170-
tm.assert_index_equal(intersect, expected)
171-
172-
# Test empty.intersection(copy)
173-
first = index.drop(index).set_names(fname)
174-
second = index.copy().set_names(sname)
175-
intersect = first.intersection(second)
176-
expected = index.drop(index).set_names(expected_name)
177-
tm.assert_index_equal(intersect, expected)
178-
179-
# Test empty.intersection(empty)
180-
first = index.drop(index).set_names(fname)
181-
second = index.drop(index).set_names(sname)
182-
intersect = first.intersection(second)
183-
expected = index.drop(index).set_names(expected_name)
184-
tm.assert_index_equal(intersect, expected)
185-
186-
@pytest.mark.parametrize(
187-
"fname, sname, expected_name",
188-
[
189-
("A", "A", "A"),
190-
("A", "B", None),
191-
("A", None, None),
192-
(None, "B", None),
193-
(None, None, None),
194-
],
195-
)
196-
def test_intersect_unequal(self, index, fname, sname, expected_name):
197-
if isinstance(index, MultiIndex) or not index.is_unique:
198-
pytest.skip("Not for MultiIndex or repeated indices")
199-
200-
# test copy.intersection(subset) - need sort for unicode and string
201-
first = index.copy().set_names(fname)
202-
second = index[1:].set_names(sname)
203-
intersect = first.intersection(second).sort_values()
204-
expected = index[1:].set_names(expected_name).sort_values()
205-
tm.assert_index_equal(intersect, expected)
206-
20774
def test_to_flat_index(self, index):
20875
# 22866
20976
if isinstance(index, MultiIndex):
@@ -329,13 +196,6 @@ def test_get_unique_index(self, index):
329196
result = i._get_unique_index(dropna=dropna)
330197
tm.assert_index_equal(result, expected)
331198

332-
def test_mutability(self, index):
333-
if not len(index):
334-
pytest.skip("Skip check for empty Index")
335-
msg = "Index does not support mutable operations"
336-
with pytest.raises(TypeError, match=msg):
337-
index[0] = index[0]
338-
339199
def test_view(self, index):
340200
assert index.view().name == index.name
341201

0 commit comments

Comments
 (0)