diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 96fc85fcf4ae6..b756ad6051a86 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -32,7 +32,6 @@ class Base: """ base class for index sub-class tests """ _holder: Type[Index] - _compat_props = ["shape", "ndim", "size", "nbytes"] def create_index(self) -> Index: raise NotImplementedError("Method not implemented") @@ -191,29 +190,6 @@ def test_logical_compat(self): with pytest.raises(TypeError, match="cannot perform any"): idx.any() - def test_reindex_base(self): - idx = self.create_index() - expected = np.arange(idx.size, dtype=np.intp) - - actual = idx.get_indexer(idx) - tm.assert_numpy_array_equal(expected, actual) - - with pytest.raises(ValueError, match="Invalid fill method"): - idx.get_indexer(idx, method="invalid") - - def test_ndarray_compat_properties(self): - idx = self.create_index() - assert idx.T.equals(idx) - assert idx.transpose().equals(idx) - - values = idx.values - for prop in self._compat_props: - assert getattr(idx, prop) == getattr(values, prop) - - # test for validity - idx.nbytes - idx.values.nbytes - def test_repr_roundtrip(self): idx = self.create_index() @@ -681,21 +657,6 @@ def test_map_str(self): expected = Index([str(x) for x in index], dtype=object) tm.assert_index_equal(result, expected) - def test_putmask_with_wrong_mask(self): - # GH18368 - index = self.create_index() - fill = index[0] - - msg = "putmask: mask and data must be the same size" - with pytest.raises(ValueError, match=msg): - index.putmask(np.ones(len(index) + 1, np.bool_), fill) - - with pytest.raises(ValueError, match=msg): - index.putmask(np.ones(len(index) - 1, np.bool_), fill) - - with pytest.raises(ValueError, match=msg): - index.putmask("foo", fill) - @pytest.mark.parametrize("copy", [True, False]) @pytest.mark.parametrize("name", [None, "foo"]) @pytest.mark.parametrize("ordered", [True, False]) @@ -760,25 +721,6 @@ def test_getitem_2d_deprecated(self): assert isinstance(res, np.ndarray), type(res) - def test_contains_requires_hashable_raises(self): - idx = self.create_index() - - msg = "unhashable type: 'list'" - with pytest.raises(TypeError, match=msg): - [] in idx - - msg = "|".join( - [ - r"unhashable type: 'dict'", - r"must be real number, not dict", - r"an integer is required", - r"\{\}", - r"pandas\._libs\.interval\.IntervalTree' is not iterable", - ] - ) - with pytest.raises(TypeError, match=msg): - {} in idx._engine - def test_copy_shares_cache(self): # GH32898, GH36840 idx = self.create_index() diff --git a/pandas/tests/indexes/multi/conftest.py b/pandas/tests/indexes/multi/conftest.py index a77af84ee1ed0..ce477485bb21e 100644 --- a/pandas/tests/indexes/multi/conftest.py +++ b/pandas/tests/indexes/multi/conftest.py @@ -5,6 +5,7 @@ from pandas import Index, MultiIndex +# Note: identical the the "multi" entry in the top-level "index" fixture @pytest.fixture def idx(): # a MultiIndex used to test the general functionality of the @@ -49,12 +50,6 @@ def index_names(): return ["first", "second"] -@pytest.fixture -def compat_props(): - # a MultiIndex must have these properties associated with it - return ["shape", "ndim", "size"] - - @pytest.fixture def narrow_multi_index(): """ diff --git a/pandas/tests/indexes/multi/test_compat.py b/pandas/tests/indexes/multi/test_compat.py index 72b5ed0edaa78..d2b5a595b8454 100644 --- a/pandas/tests/indexes/multi/test_compat.py +++ b/pandas/tests/indexes/multi/test_compat.py @@ -35,32 +35,6 @@ def test_logical_compat(idx, method): getattr(idx, method)() -def test_boolean_context_compat(idx): - - msg = ( - "The truth value of a MultiIndex is ambiguous. " - r"Use a.empty, a.bool\(\), a.item\(\), a.any\(\) or a.all\(\)." - ) - with pytest.raises(ValueError, match=msg): - bool(idx) - - -def test_boolean_context_compat2(): - - # boolean context compat - # GH7897 - i1 = MultiIndex.from_tuples([("A", 1), ("A", 2)]) - i2 = MultiIndex.from_tuples([("A", 1), ("A", 3)]) - common = i1.intersection(i2) - - msg = ( - r"The truth value of a MultiIndex is ambiguous\. " - r"Use a\.empty, a\.bool\(\), a\.item\(\), a\.any\(\) or a\.all\(\)\." - ) - with pytest.raises(ValueError, match=msg): - bool(common) - - def test_inplace_mutation_resets_values(): levels = [["a", "b", "c"], [4]] levels2 = [[1, 2, 3], ["a"]] @@ -124,19 +98,6 @@ def test_inplace_mutation_resets_values(): assert "_values" in mi2._cache -def test_ndarray_compat_properties(idx, compat_props): - assert idx.T.equals(idx) - assert idx.transpose().equals(idx) - - values = idx.values - for prop in compat_props: - assert getattr(idx, prop) == getattr(values, prop) - - # test for validity - idx.nbytes - idx.values.nbytes - - def test_pickle_compat_construction(): # this is testing for pickle compat # need an object to create with diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index b0de16a25bcc3..377ae7533ba08 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -2,7 +2,6 @@ import pytest from pandas._libs.tslibs.period import IncompatibleFrequency -import pandas.util._test_decorators as td import pandas as pd from pandas import ( @@ -329,10 +328,6 @@ def test_shift(self): # This is tested in test_arithmetic pass - @td.skip_if_32bit - def test_ndarray_compat_properties(self): - super().test_ndarray_compat_properties() - def test_negative_ordinals(self): Period(ordinal=-1000, freq="A") Period(ordinal=0, freq="A") diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index 40cd812ebe368..b34fb44a57c7e 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -18,7 +18,6 @@ class TestRangeIndex(Numeric): _holder = RangeIndex - _compat_props = ["shape", "ndim", "size"] @pytest.fixture( params=[ diff --git a/pandas/tests/indexes/test_any_index.py b/pandas/tests/indexes/test_any_index.py index c8629fdf1e3a6..c9c86f9eebde9 100644 --- a/pandas/tests/indexes/test_any_index.py +++ b/pandas/tests/indexes/test_any_index.py @@ -11,10 +11,14 @@ def test_boolean_context_compat(index): + # GH#7897 with pytest.raises(ValueError, match="The truth value of a"): if index: pass + with pytest.raises(ValueError, match="The truth value of a"): + bool(index) + def test_sort(index): msg = "cannot sort an Index object in-place, use sort_values instead" @@ -27,6 +31,12 @@ def test_hash_error(index): hash(index) +def test_copy_dtype_deprecated(index): + # GH#35853 + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + index.copy(dtype=object) + + def test_mutability(index): if not len(index): return diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 2b49ea00d3322..931f01cecab0b 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -9,6 +9,7 @@ import pytest from pandas._libs.tslibs import iNaT +from pandas.compat import IS64 from pandas.core.dtypes.common import is_period_dtype, needs_i8_conversion @@ -398,3 +399,25 @@ def test_sort_values_with_missing(request, index_with_missing, na_position): result = index_with_missing.sort_values(na_position=na_position) tm.assert_index_equal(result, expected) + + +def test_ndarray_compat_properties(index): + if isinstance(index, PeriodIndex) and not IS64: + pytest.skip("Overflow") + idx = index + assert idx.T.equals(idx) + assert idx.transpose().equals(idx) + + values = idx.values + + assert idx.shape == values.shape + assert idx.ndim == values.ndim + assert idx.size == values.size + + if not isinstance(index, (RangeIndex, MultiIndex)): + # These two are not backed by an ndarray + assert idx.nbytes == values.nbytes + + # test for validity + idx.nbytes + idx.values.nbytes diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index 04f7a65bd5c56..8d2637e4a06f6 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -24,6 +24,7 @@ Index, Int64Index, IntervalIndex, + MultiIndex, PeriodIndex, Series, TimedeltaIndex, @@ -140,6 +141,26 @@ def test_contains_with_float_index(self): assert 1.0 not in float_index assert 1 not in float_index + def test_contains_requires_hashable_raises(self, index): + if isinstance(index, MultiIndex): + return # TODO: do we want this to raise? + + msg = "unhashable type: 'list'" + with pytest.raises(TypeError, match=msg): + [] in index + + msg = "|".join( + [ + r"unhashable type: 'dict'", + r"must be real number, not dict", + r"an integer is required", + r"\{\}", + r"pandas\._libs\.interval\.IntervalTree' is not iterable", + ] + ) + with pytest.raises(TypeError, match=msg): + {} in index._engine + class TestGetValue: @pytest.mark.parametrize( @@ -161,19 +182,30 @@ def test_get_value(self, index): class TestGetIndexer: + def test_get_indexer_base(self, index): + + if index._index_as_unique: + expected = np.arange(index.size, dtype=np.intp) + actual = index.get_indexer(index) + tm.assert_numpy_array_equal(expected, actual) + else: + msg = "Reindexing only valid with uniquely valued Index objects" + with pytest.raises(InvalidIndexError, match=msg): + index.get_indexer(index) + + with pytest.raises(ValueError, match="Invalid fill method"): + index.get_indexer(index, method="invalid") + def test_get_indexer_consistency(self, index): # See GH#16819 - if isinstance(index, IntervalIndex): - # requires index.is_non_overlapping - return - if index.is_unique: + if index._index_as_unique: indexer = index.get_indexer(index[0:2]) assert isinstance(indexer, np.ndarray) assert indexer.dtype == np.intp else: - e = "Reindexing only valid with uniquely valued Index objects" - with pytest.raises(InvalidIndexError, match=e): + msg = "Reindexing only valid with uniquely valued Index objects" + with pytest.raises(InvalidIndexError, match=msg): index.get_indexer(index[0:2]) indexer, _ = index.get_indexer_non_unique(index[0:2]) @@ -197,6 +229,25 @@ def test_convert_almost_null_slice(self, index): index._convert_slice_indexer(key, "loc") +class TestPutmask: + def test_putmask_with_wrong_mask(self, index): + # GH#18368 + if not len(index): + return + + fill = index[0] + + msg = "putmask: mask and data must be the same size" + with pytest.raises(ValueError, match=msg): + index.putmask(np.ones(len(index) + 1, np.bool_), fill) + + with pytest.raises(ValueError, match=msg): + index.putmask(np.ones(len(index) - 1, np.bool_), fill) + + with pytest.raises(ValueError, match=msg): + index.putmask("foo", fill) + + @pytest.mark.parametrize( "idx", [Index([1, 2, 3]), Index([0.1, 0.2, 0.3]), Index(["a", "b", "c"])] )