-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support .sel
with method
kwarg for slices
#10711
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
Open
TomNicholas
wants to merge
18
commits into
pydata:main
Choose a base branch
from
TomNicholas:support_method_sel_float_coords
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−17
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
63c1b6a
sketch of solution
TomNicholas c03b0e9
initial test
TomNicholas e65b759
test passing
TomNicholas 20a6d3b
update existing test
TomNicholas ee1da65
support non-zero step
TomNicholas 308d8c9
convince myself that negative slices do work as they should
TomNicholas 61c1412
remove print
TomNicholas d7ec746
remove todo
TomNicholas 8842b23
whatsnew
TomNicholas 1fe2961
lint
TomNicholas bb36ae7
Merge branch 'main' into support_method_sel_float_coords
TomNicholas 4dd2d63
remove return type hint
TomNicholas ae391b3
Merge branch 'main' into support_method_sel_float_coords
TomNicholas f10a3a2
use a single get_indexer call
TomNicholas 74f3d07
Merge branch 'support_method_sel_float_coords' of https://github.com/…
TomNicholas d05ab1a
handle no match case
TomNicholas 199765e
explicitly handle non-unique coordinate values
TomNicholas 8cce331
support passing tolerance but not method
TomNicholas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2164,8 +2164,13 @@ def test_sel_method(self) -> None: | |
actual = data.sel(dim2=[1.45], method="backfill") | ||
assert_identical(expected, actual) | ||
|
||
with pytest.raises(NotImplementedError, match=r"slice objects"): | ||
data.sel(dim2=slice(1, 3), method="ffill") | ||
expected = data.isel(dim2=slice(2, 7)) | ||
actual = data.sel(dim2=slice(1, 3), method="ffill") | ||
assert_identical(expected, actual) | ||
|
||
expected = data.isel(dim2=slice(2, 7, 2)) | ||
actual = data.sel(dim2=slice(1, 3, 2), method="ffill") | ||
assert_identical(expected, actual) | ||
Comment on lines
+2167
to
+2173
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a test for |
||
|
||
with pytest.raises(TypeError, match=r"``method``"): | ||
# this should not pass silently | ||
|
@@ -2175,6 +2180,110 @@ def test_sel_method(self) -> None: | |
with pytest.raises(ValueError, match=r"cannot supply"): | ||
data.sel(dim1=0, method="nearest") | ||
|
||
def test_sel_method_with_slice(self) -> None: | ||
# regression test for https://github.com/pydata/xarray/issues/10710 | ||
|
||
data_int_coords = xr.Dataset(coords={"lat": ("lat", [20, 21, 22, 23])}) | ||
expected = xr.Dataset(coords={"lat": ("lat", [21, 22])}) | ||
actual = data_int_coords.sel(lat=slice(21, 22), method="nearest") | ||
assert_identical(expected, actual) | ||
|
||
# check non-zero step | ||
expected = xr.Dataset(coords={"lat": ("lat", [21])}) | ||
actual = data_int_coords.sel(lat=slice(21, 22, 2), method="nearest") | ||
assert_identical(expected, actual) | ||
|
||
# check consistency with not passing method kwarg, for case of ints, where method kwarg should be irrelevant | ||
expected = data_int_coords.sel(lat=slice(21, 22)) | ||
actual = data_int_coords.sel(lat=slice(21, 22), method="nearest") | ||
assert_identical(expected, actual) | ||
|
||
data_float_coords = xr.Dataset( | ||
coords={"lat": ("lat", [20.1, 21.1, 22.1, 23.1])} | ||
) | ||
expected = xr.Dataset(coords={"lat": ("lat", [21.1, 22.1])}) | ||
actual = data_float_coords.sel(lat=slice(21, 22), method="nearest") | ||
assert_identical(expected, actual) | ||
|
||
# "no match" case - should return zero-size slice | ||
expected = xr.Dataset(coords={"lat": ("lat", [])}) | ||
actual = data_float_coords.sel( | ||
lat=slice(21.5, 21.6), method="nearest", tolerance=1e-3 | ||
) | ||
assert_identical(expected, actual) | ||
|
||
# test supposed default behaviour | ||
expected = xr.Dataset(coords={"lat": ("lat", [21.1, 22.1])}) | ||
actual = data_float_coords.sel(lat=slice(21.0, 22.2)) | ||
assert_identical(expected, actual) | ||
|
||
# tolerance specified but method not specified | ||
expected = xr.Dataset(coords={"lat": ("lat", [21.1, 22.1])}) | ||
actual = data_float_coords.sel( | ||
lat=slice(21.0, 22.2), | ||
tolerance=1.0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tolerance=0.0 might be a cleaner test here |
||
) | ||
assert_identical(expected, actual) | ||
# test this matches default behaviour without tolerance specified | ||
default = data_float_coords.sel(lat=slice(21.0, 22.2)) | ||
assert_identical(default, actual) | ||
|
||
# "no match" case - should return zero-size slice | ||
expected = xr.Dataset(coords={"lat": ("lat", [])}) | ||
actual = data_float_coords.sel( | ||
lat=slice(21.5, 21.6), method="nearest", tolerance=1e-3 | ||
) | ||
assert_identical(expected, actual) | ||
|
||
# non-unique coordinate values | ||
data_non_unique = xr.Dataset( | ||
coords={"lat": ("lat", [20.1, 21.1, 21.1, 22.1, 22.1, 23.1])} | ||
) | ||
expected = xr.Dataset(coords={"lat": ("lat", [21.1, 21.1, 22.1, 22.1])}) | ||
with pytest.raises( | ||
NotImplementedError, | ||
match="slice object as an indexer and an index with non-unique values", | ||
): | ||
data_non_unique.sel(lat=slice(21.0, 22.2), method="nearest") | ||
|
||
# check non-zero step | ||
data_float_coords = xr.Dataset( | ||
coords={"lat": ("lat", [20.1, 21.1, 22.1, 23.1])} | ||
) | ||
expected = xr.Dataset(coords={"lat": ("lat", [21.1])}) | ||
actual = data_float_coords.sel(lat=slice(21, 22, 2), method="nearest") | ||
assert_identical(expected, actual) | ||
|
||
# backwards slices | ||
data_int_coords = xr.Dataset(coords={"lat": ("lat", [23, 22, 21, 20])}) | ||
expected = xr.Dataset(coords={"lat": ("lat", [22, 21])}) | ||
actual = data_int_coords.sel(lat=slice(22, 21), method="nearest") | ||
assert_identical(expected, actual) | ||
|
||
data_float_coords = xr.Dataset( | ||
coords={"lat": ("lat", [23.1, 22.1, 21.1, 20.1])} | ||
) | ||
expected = xr.Dataset(coords={"lat": ("lat", [22.1, 21.1])}) | ||
actual = data_float_coords.sel(lat=slice(22, 21), method="nearest") | ||
assert_identical(expected, actual) | ||
|
||
def test_sel_negative_slices(self) -> None: | ||
data_int_coords = xr.Dataset(coords={"lat": ("lat", [-23, -22, -21, -20])}) | ||
expected = xr.Dataset(coords={"lat": ("lat", [-22, -21])}) | ||
actual = data_int_coords.sel(lat=slice(-22, -21)) | ||
assert_identical(expected, actual) | ||
|
||
expected = xr.Dataset(coords={"lat": ("lat", [-22, -21])}) | ||
actual = data_int_coords.sel(lat=slice(-22, -21), method="nearest") | ||
assert_identical(expected, actual) | ||
|
||
data_float_coords = xr.Dataset( | ||
coords={"lat": ("lat", [-23.1, -22.1, -21.1, -20.1])} | ||
) | ||
expected = xr.Dataset(coords={"lat": ("lat", [-22.1, -21.1])}) | ||
actual = data_float_coords.sel(lat=slice(-22, -21), method="nearest") | ||
assert_identical(expected, actual) | ||
|
||
def test_loc(self) -> None: | ||
data = create_test_data() | ||
expected = data.sel(dim3="a") | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the default is inclusive?