Skip to content

Commit

Permalink
add tests for as_indexable
Browse files Browse the repository at this point in the history
  • Loading branch information
keewis committed Sep 22, 2024
1 parent cc4f5bb commit a10e97a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions xarray/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,46 @@ def test_posify_mask_subindexer(indices, expected) -> None:
np.testing.assert_array_equal(expected, actual)


class ArrayWithNamespace:
def __array_namespace__(self, version=None):
pass


class ArrayWithArrayFunction:
def __array_function__(self, func, types, args, kwargs):
pass


@pytest.mark.parametrize(
["array", "expected_type"],
(
pytest.param(
indexing.CopyOnWriteArray(np.array([1, 2])),
indexing.CopyOnWriteArray,
id="ExplicitlyIndexed",
),
pytest.param(
np.array([1, 2]), indexing.NumpyIndexingAdapter, id="numpy.ndarray"
),
pytest.param(
pd.Index([1, 2]), indexing.PandasIndexingAdapter, id="pandas.Index"
),
pytest.param(
ArrayWithNamespace(), indexing.ArrayApiIndexingAdapter, id="array_api"
),
pytest.param(
ArrayWithArrayFunction(),
indexing.NdArrayLikeIndexingAdapter,
id="array_like",
),
),
)
def test_as_indexable(array, expected_type):
actual = indexing.as_indexable(array)

assert isinstance(actual, expected_type)


def test_indexing_1d_object_array() -> None:
items = (np.arange(3), np.arange(6))
arr = DataArray(np.array(items, dtype=object))
Expand Down

0 comments on commit a10e97a

Please sign in to comment.