Skip to content

Commit c74ab72

Browse files
authored
TST/REF: collect index tests (#39495)
1 parent d71531b commit c74ab72

File tree

8 files changed

+91
-115
lines changed

8 files changed

+91
-115
lines changed

pandas/tests/indexes/common.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class Base:
3232
""" base class for index sub-class tests """
3333

3434
_holder: Type[Index]
35-
_compat_props = ["shape", "ndim", "size", "nbytes"]
3635

3736
def create_index(self) -> Index:
3837
raise NotImplementedError("Method not implemented")
@@ -191,29 +190,6 @@ def test_logical_compat(self):
191190
with pytest.raises(TypeError, match="cannot perform any"):
192191
idx.any()
193192

194-
def test_reindex_base(self):
195-
idx = self.create_index()
196-
expected = np.arange(idx.size, dtype=np.intp)
197-
198-
actual = idx.get_indexer(idx)
199-
tm.assert_numpy_array_equal(expected, actual)
200-
201-
with pytest.raises(ValueError, match="Invalid fill method"):
202-
idx.get_indexer(idx, method="invalid")
203-
204-
def test_ndarray_compat_properties(self):
205-
idx = self.create_index()
206-
assert idx.T.equals(idx)
207-
assert idx.transpose().equals(idx)
208-
209-
values = idx.values
210-
for prop in self._compat_props:
211-
assert getattr(idx, prop) == getattr(values, prop)
212-
213-
# test for validity
214-
idx.nbytes
215-
idx.values.nbytes
216-
217193
def test_repr_roundtrip(self):
218194

219195
idx = self.create_index()
@@ -681,21 +657,6 @@ def test_map_str(self):
681657
expected = Index([str(x) for x in index], dtype=object)
682658
tm.assert_index_equal(result, expected)
683659

684-
def test_putmask_with_wrong_mask(self):
685-
# GH18368
686-
index = self.create_index()
687-
fill = index[0]
688-
689-
msg = "putmask: mask and data must be the same size"
690-
with pytest.raises(ValueError, match=msg):
691-
index.putmask(np.ones(len(index) + 1, np.bool_), fill)
692-
693-
with pytest.raises(ValueError, match=msg):
694-
index.putmask(np.ones(len(index) - 1, np.bool_), fill)
695-
696-
with pytest.raises(ValueError, match=msg):
697-
index.putmask("foo", fill)
698-
699660
@pytest.mark.parametrize("copy", [True, False])
700661
@pytest.mark.parametrize("name", [None, "foo"])
701662
@pytest.mark.parametrize("ordered", [True, False])
@@ -760,25 +721,6 @@ def test_getitem_2d_deprecated(self):
760721

761722
assert isinstance(res, np.ndarray), type(res)
762723

763-
def test_contains_requires_hashable_raises(self):
764-
idx = self.create_index()
765-
766-
msg = "unhashable type: 'list'"
767-
with pytest.raises(TypeError, match=msg):
768-
[] in idx
769-
770-
msg = "|".join(
771-
[
772-
r"unhashable type: 'dict'",
773-
r"must be real number, not dict",
774-
r"an integer is required",
775-
r"\{\}",
776-
r"pandas\._libs\.interval\.IntervalTree' is not iterable",
777-
]
778-
)
779-
with pytest.raises(TypeError, match=msg):
780-
{} in idx._engine
781-
782724
def test_copy_shares_cache(self):
783725
# GH32898, GH36840
784726
idx = self.create_index()

pandas/tests/indexes/multi/conftest.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pandas import Index, MultiIndex
66

77

8+
# Note: identical the the "multi" entry in the top-level "index" fixture
89
@pytest.fixture
910
def idx():
1011
# a MultiIndex used to test the general functionality of the
@@ -49,12 +50,6 @@ def index_names():
4950
return ["first", "second"]
5051

5152

52-
@pytest.fixture
53-
def compat_props():
54-
# a MultiIndex must have these properties associated with it
55-
return ["shape", "ndim", "size"]
56-
57-
5853
@pytest.fixture
5954
def narrow_multi_index():
6055
"""

pandas/tests/indexes/multi/test_compat.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,6 @@ def test_logical_compat(idx, method):
3535
getattr(idx, method)()
3636

3737

38-
def test_boolean_context_compat(idx):
39-
40-
msg = (
41-
"The truth value of a MultiIndex is ambiguous. "
42-
r"Use a.empty, a.bool\(\), a.item\(\), a.any\(\) or a.all\(\)."
43-
)
44-
with pytest.raises(ValueError, match=msg):
45-
bool(idx)
46-
47-
48-
def test_boolean_context_compat2():
49-
50-
# boolean context compat
51-
# GH7897
52-
i1 = MultiIndex.from_tuples([("A", 1), ("A", 2)])
53-
i2 = MultiIndex.from_tuples([("A", 1), ("A", 3)])
54-
common = i1.intersection(i2)
55-
56-
msg = (
57-
r"The truth value of a MultiIndex is ambiguous\. "
58-
r"Use a\.empty, a\.bool\(\), a\.item\(\), a\.any\(\) or a\.all\(\)\."
59-
)
60-
with pytest.raises(ValueError, match=msg):
61-
bool(common)
62-
63-
6438
def test_inplace_mutation_resets_values():
6539
levels = [["a", "b", "c"], [4]]
6640
levels2 = [[1, 2, 3], ["a"]]
@@ -124,19 +98,6 @@ def test_inplace_mutation_resets_values():
12498
assert "_values" in mi2._cache
12599

126100

127-
def test_ndarray_compat_properties(idx, compat_props):
128-
assert idx.T.equals(idx)
129-
assert idx.transpose().equals(idx)
130-
131-
values = idx.values
132-
for prop in compat_props:
133-
assert getattr(idx, prop) == getattr(values, prop)
134-
135-
# test for validity
136-
idx.nbytes
137-
idx.values.nbytes
138-
139-
140101
def test_pickle_compat_construction():
141102
# this is testing for pickle compat
142103
# need an object to create with

pandas/tests/indexes/period/test_period.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import pytest
33

44
from pandas._libs.tslibs.period import IncompatibleFrequency
5-
import pandas.util._test_decorators as td
65

76
import pandas as pd
87
from pandas import (
@@ -329,10 +328,6 @@ def test_shift(self):
329328
# This is tested in test_arithmetic
330329
pass
331330

332-
@td.skip_if_32bit
333-
def test_ndarray_compat_properties(self):
334-
super().test_ndarray_compat_properties()
335-
336331
def test_negative_ordinals(self):
337332
Period(ordinal=-1000, freq="A")
338333
Period(ordinal=0, freq="A")

pandas/tests/indexes/ranges/test_range.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
class TestRangeIndex(Numeric):
2020
_holder = RangeIndex
21-
_compat_props = ["shape", "ndim", "size"]
2221

2322
@pytest.fixture(
2423
params=[

pandas/tests/indexes/test_any_index.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212

1313
def test_boolean_context_compat(index):
14+
# GH#7897
1415
with pytest.raises(ValueError, match="The truth value of a"):
1516
if index:
1617
pass
1718

19+
with pytest.raises(ValueError, match="The truth value of a"):
20+
bool(index)
21+
1822

1923
def test_sort(index):
2024
msg = "cannot sort an Index object in-place, use sort_values instead"
@@ -27,6 +31,12 @@ def test_hash_error(index):
2731
hash(index)
2832

2933

34+
def test_copy_dtype_deprecated(index):
35+
# GH#35853
36+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
37+
index.copy(dtype=object)
38+
39+
3040
def test_mutability(index):
3141
if not len(index):
3242
return

pandas/tests/indexes/test_common.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pytest
1010

1111
from pandas._libs.tslibs import iNaT
12+
from pandas.compat import IS64
1213

1314
from pandas.core.dtypes.common import is_period_dtype, needs_i8_conversion
1415

@@ -398,3 +399,25 @@ def test_sort_values_with_missing(request, index_with_missing, na_position):
398399

399400
result = index_with_missing.sort_values(na_position=na_position)
400401
tm.assert_index_equal(result, expected)
402+
403+
404+
def test_ndarray_compat_properties(index):
405+
if isinstance(index, PeriodIndex) and not IS64:
406+
pytest.skip("Overflow")
407+
idx = index
408+
assert idx.T.equals(idx)
409+
assert idx.transpose().equals(idx)
410+
411+
values = idx.values
412+
413+
assert idx.shape == values.shape
414+
assert idx.ndim == values.ndim
415+
assert idx.size == values.size
416+
417+
if not isinstance(index, (RangeIndex, MultiIndex)):
418+
# These two are not backed by an ndarray
419+
assert idx.nbytes == values.nbytes
420+
421+
# test for validity
422+
idx.nbytes
423+
idx.values.nbytes

pandas/tests/indexes/test_indexing.py

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Index,
2525
Int64Index,
2626
IntervalIndex,
27+
MultiIndex,
2728
PeriodIndex,
2829
Series,
2930
TimedeltaIndex,
@@ -140,6 +141,26 @@ def test_contains_with_float_index(self):
140141
assert 1.0 not in float_index
141142
assert 1 not in float_index
142143

144+
def test_contains_requires_hashable_raises(self, index):
145+
if isinstance(index, MultiIndex):
146+
return # TODO: do we want this to raise?
147+
148+
msg = "unhashable type: 'list'"
149+
with pytest.raises(TypeError, match=msg):
150+
[] in index
151+
152+
msg = "|".join(
153+
[
154+
r"unhashable type: 'dict'",
155+
r"must be real number, not dict",
156+
r"an integer is required",
157+
r"\{\}",
158+
r"pandas\._libs\.interval\.IntervalTree' is not iterable",
159+
]
160+
)
161+
with pytest.raises(TypeError, match=msg):
162+
{} in index._engine
163+
143164

144165
class TestGetValue:
145166
@pytest.mark.parametrize(
@@ -161,19 +182,30 @@ def test_get_value(self, index):
161182

162183

163184
class TestGetIndexer:
185+
def test_get_indexer_base(self, index):
186+
187+
if index._index_as_unique:
188+
expected = np.arange(index.size, dtype=np.intp)
189+
actual = index.get_indexer(index)
190+
tm.assert_numpy_array_equal(expected, actual)
191+
else:
192+
msg = "Reindexing only valid with uniquely valued Index objects"
193+
with pytest.raises(InvalidIndexError, match=msg):
194+
index.get_indexer(index)
195+
196+
with pytest.raises(ValueError, match="Invalid fill method"):
197+
index.get_indexer(index, method="invalid")
198+
164199
def test_get_indexer_consistency(self, index):
165200
# See GH#16819
166-
if isinstance(index, IntervalIndex):
167-
# requires index.is_non_overlapping
168-
return
169201

170-
if index.is_unique:
202+
if index._index_as_unique:
171203
indexer = index.get_indexer(index[0:2])
172204
assert isinstance(indexer, np.ndarray)
173205
assert indexer.dtype == np.intp
174206
else:
175-
e = "Reindexing only valid with uniquely valued Index objects"
176-
with pytest.raises(InvalidIndexError, match=e):
207+
msg = "Reindexing only valid with uniquely valued Index objects"
208+
with pytest.raises(InvalidIndexError, match=msg):
177209
index.get_indexer(index[0:2])
178210

179211
indexer, _ = index.get_indexer_non_unique(index[0:2])
@@ -197,6 +229,25 @@ def test_convert_almost_null_slice(self, index):
197229
index._convert_slice_indexer(key, "loc")
198230

199231

232+
class TestPutmask:
233+
def test_putmask_with_wrong_mask(self, index):
234+
# GH#18368
235+
if not len(index):
236+
return
237+
238+
fill = index[0]
239+
240+
msg = "putmask: mask and data must be the same size"
241+
with pytest.raises(ValueError, match=msg):
242+
index.putmask(np.ones(len(index) + 1, np.bool_), fill)
243+
244+
with pytest.raises(ValueError, match=msg):
245+
index.putmask(np.ones(len(index) - 1, np.bool_), fill)
246+
247+
with pytest.raises(ValueError, match=msg):
248+
index.putmask("foo", fill)
249+
250+
200251
@pytest.mark.parametrize(
201252
"idx", [Index([1, 2, 3]), Index([0.1, 0.2, 0.3]), Index(["a", "b", "c"])]
202253
)

0 commit comments

Comments
 (0)