Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

nathanredmond
Copy link

Copy link

welcome bot commented Apr 22, 2024

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

Copy link
Collaborator

@max-sixty max-sixty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @nathanredmond ! Thanks for your first PR!

@@ -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"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can use pytest.warns

@@ -7726,6 +7726,7 @@ def diff(
n: int = 1,
*,
label: Literal["upper", "lower"] = "upper",
dims_found=False,
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Comment on lines +39 to +41
- Fix for :py:meth:`DataSet.diff` and consequently :py:meth:`DataArray.diff` to
raise a KeyError when `diff` is parameterized with a dimension that does not
exist in callee's DataSet/DataArray. (:issue:`7748`)
Copy link
Collaborator

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!

@dcherian dcherian changed the title Addresses GH7748 Raise exception when using diff with a non-existent dimension Apr 22, 2024
@headtr1ck
Copy link
Collaborator

@nathanredmond are you planning on finishing this? Seems like a good idea.
Not sure if we should really depreciate or directly raise, but I guess deprecation doesn't hurt.

Comment on lines +7811 to +7814
if dims_found:
return difference
else:
from warnings import warn
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(these can be reduced to a single if not dims_found, and then it'll fall through to the return difference below)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

diff('non existing dimension') does not raise exception
5 participants