8000 Pass kwargs to sel by bpatel2107 · Pull Request #304 · xarray-contrib/pint-xarray · GitHub
[go: up one dir, main page]

Skip to content

Pass kwargs to sel #304

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
Jun 14, 2025
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
3 changes: 2 additions & 1 deletion docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ What's new
==========
0.5.1 (*unreleased*)
--------------------

- Pass ``sel`` options to the wrapped array (:pull:`304`, :issue:`303`)
By `Bhavin Patel <https://github.com/bpatel2107>`_.

0.5 (09 Jun 2025)
------------------
Expand Down
4 changes: 2 additions & 2 deletions pint_xarray/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,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)
Expand Down
36 changes: 36 additions & 0 deletions pint_xarray/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = PintIndex(
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])}),
Expand Down
Loading
0