|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import itertools
|
| 4 | +from unittest import mock |
4 | 5 |
|
5 | 6 | import numpy as np
|
6 | 7 | import pandas as pd
|
7 | 8 | import pytest
|
8 | 9 |
|
9 | 10 | import xarray as xr
|
| 11 | +from xarray.core import indexing |
10 | 12 | from xarray.core.missing import (
|
11 | 13 | NumpyInterpolator,
|
12 | 14 | ScipyInterpolator,
|
@@ -772,3 +774,29 @@ def test_interpolators_complex_out_of_bounds():
|
772 | 774 | f = interpolator(xi, yi, method=method)
|
773 | 775 | actual = f(x)
|
774 | 776 | assert_array_equal(actual, expected)
|
| 777 | + |
| 778 | + |
| 779 | +@requires_scipy |
| 780 | +def test_indexing_localize(): |
| 781 | + # regression test for GH10287 |
| 782 | + ds = xr.Dataset( |
| 783 | + { |
| 784 | + "sigma_a": xr.DataArray( |
| 785 | + data=np.ones((16, 8, 36811)), |
| 786 | + dims=["p", "t", "w"], |
| 787 | + coords={"w": np.linspace(0, 30000, 36811)}, |
| 788 | + ) |
| 789 | + } |
| 790 | + ) |
| 791 | + |
| 792 | + original_func = indexing.NumpyIndexingAdapter.__getitem__ |
| 793 | + |
| 794 | + def wrapper(self, indexer): |
| 795 | + return original_func(self, indexer) |
| 796 | + |
| 797 | + with mock.patch.object( |
| 798 | + indexing.NumpyIndexingAdapter, "__getitem__", side_effect=wrapper, autospec=True |
| 799 | + ) as mock_func: |
| 800 | + ds["sigma_a"].interp(w=15000.5) |
| 801 | + actual_indexer = mock_func.mock_calls[0].args[1]._key |
| 802 | + assert actual_indexer == (slice(None), slice(None), slice(18404, 18408)) |
0 commit comments