-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Raise exception when using diff
with a non-existent dimension
#8962
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7726,6 +7726,7 @@ def diff( | |
n: int = 1, | ||
*, | ||
label: Literal["upper", "lower"] = "upper", | ||
dims_found=False, | ||
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. Do we need this? I think it can be defined inside the function |
||
) -> Self: | ||
"""Calculate the n-th order discrete difference along given axis. | ||
|
||
|
@@ -7798,14 +7799,25 @@ def diff( | |
variables[name] = var.isel(slice_end) - var.isel(slice_start) | ||
else: | ||
variables[name] = var.isel(slice_new) | ||
dims_found = True | ||
else: | ||
variables[name] = var | ||
|
||
difference = self._replace_with_new_dims(variables, indexes=indexes) | ||
|
||
if n > 1: | ||
return difference.diff(dim, n - 1) | ||
return difference.diff(dim, n - 1, dims_found=dims_found) | ||
else: | ||
if dims_found: | ||
return difference | ||
else: | ||
from warnings import warn | ||
Comment on lines
+7811
to
+7814
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. (these can be reduced to a single |
||
|
||
warn( | ||
"Expected dim to be present in at least one DataArray found in DataSet, " | ||
"dim provided not present. This will raise a KeyError in the future.", | ||
DeprecationWarning, | ||
) | ||
return difference | ||
|
||
def shift( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3861,6 +3861,12 @@ def test_dataarray_diff_n1(self) -> None: | |
expected = DataArray(np.diff(da.values, axis=1), dims=["x", "y"]) | ||
assert_equal(expected, actual) | ||
|
||
def test_dataarray_diff_dim_nonexist(self) -> None: | ||
# uncomment this code for test corresponding to pushed changes | ||
# with pytest.raises(KeyError, match=r"dim provided not present"): | ||
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. This can use |
||
# DataArray(np.random.randn(3, 4), dims=["x", "y"]).diff("z") | ||
pass | ||
|
||
def test_coordinate_diff(self) -> None: | ||
# regression test for GH634 | ||
arr = DataArray(range(0, 20, 2), dims=["lon"], coords=[range(10)]) | ||
|
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.
This is fine, though we can also simplify the language a bit — cut words like
consequently
/parameterized with
!