Skip to content

TST/REF: collect index tests #39495

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 5 commits into from
Feb 1, 2021
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
58 changes: 0 additions & 58 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 1 addition & 6 deletions pandas/tests/indexes/multi/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
"""
Expand Down
39 changes: 0 additions & 39 deletions pandas/tests/indexes/multi/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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")
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

class TestRangeIndex(Numeric):
_holder = RangeIndex
_compat_props = ["shape", "ndim", "size"]

@pytest.fixture(
params=[
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
63 changes: 57 additions & 6 deletions pandas/tests/indexes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Index,
Int64Index,
IntervalIndex,
MultiIndex,
PeriodIndex,
Series,
TimedeltaIndex,
Expand Down Expand Up @@ -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(
Expand All @@ -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])
Expand All @@ -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"])]
)
Expand Down