diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fcfe7819..f7d1b976 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -97,7 +97,7 @@ jobs: python -m pytest --cov=astropy_xarray --cov-report=xml - name: Upload code coverage to Codecov - uses: codecov/codecov-action@v5.4.2 + uses: codecov/codecov-action@v5.4.3 with: file: ./coverage.xml flags: unittests diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 98429224..3d8f4987 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: - id: end-of-file-fixer - id: check-docstring-first - repo: https://github.com/rbubley/mirrors-prettier - rev: v3.5.3 + rev: v3.6.2 hooks: - id: prettier args: ["--cache-location=.prettier_cache/cache"] @@ -31,13 +31,13 @@ repos: hooks: - id: black-jupyter - repo: https://github.com/keewis/blackdoc - rev: v0.3.9 + rev: v0.4.1 hooks: - id: blackdoc additional_dependencies: ["black==25.1.0"] - id: blackdoc-autoupdate-black - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.11.9 + rev: v0.12.3 hooks: - id: ruff args: [--fix] diff --git a/astropy_xarray/index.py b/astropy_xarray/index.py index 8e37ec52..af0712d8 100644 --- a/astropy_xarray/index.py +++ b/astropy_xarray/index.py @@ -67,11 +67,11 @@ def stack(cls, variables, dim): def unstack(self): raise NotImplementedError() - def sel(self, labels): + def sel(self, labels, **options): converted_labels = conversion.convert_indexer_units(labels, self.units) stripped_labels = conversion.strip_indexer_units(converted_labels) - return self.index.sel(stripped_labels) + return self.index.sel(stripped_labels, **options) def isel(self, indexers): subset = self.index.isel(indexers) diff --git a/astropy_xarray/tests/test_index.py b/astropy_xarray/tests/test_index.py index 09aa2a3e..dfca77b2 100644 --- a/astropy_xarray/tests/test_index.py +++ b/astropy_xarray/tests/test_index.py @@ -104,6 +104,42 @@ def test_sel(labels, expected): ) +@pytest.mark.parametrize( + ["labels", "expected"], + ( + ( + {"x": ureg.Quantity(1.1, "m")}, + IndexSelResult(dim_indexers={"x": np.array(0)}), + ), + ( + {"x": ureg.Quantity(3100, "mm")}, + IndexSelResult(dim_indexers={"x": np.array(2)}), + ), + ( + {"x": ureg.Quantity(0.0021, "km")}, + IndexSelResult(dim_indexers={"x": np.array(1)}), + ), + ( + {"x": ureg.Quantity([0.0021, 0.0041], "km")}, + IndexSelResult(dim_indexers={"x": np.array([1, 3])}), + ), + ), +) +def test_sel_nearest(labels, expected): + index = AstropyIndex( + index=PandasIndex(pd.Index([1, 2, 3, 4]), dim="x"), units={"x": ureg.Unit("m")} + ) + + actual = index.sel(labels, method="nearest") + + assert isinstance(actual, IndexSelResult) + assert actual.dim_indexers.keys() == expected.dim_indexers.keys() + assert all( + indexer_equal(actual.dim_indexers[k], expected.dim_indexers[k]) + for k in expected.dim_indexers.keys() + ) + + @pytest.mark.parametrize( "indexers", ({"y": 0}, {"y": [1, 2]}, {"y": slice(0, None, 2)}, {"y": xr.Variable("y", [1])}),