-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
add a drop_conflicts strategy for merging attrs #4827
Merged
Merged
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
767a668
add the drop_conflicts option for merging attrs
keewis 909c5cd
mention drop_conflicts in a few docstrings
keewis d35a213
add a test for merge_attrs
keewis 7f9481c
rewrite the drop_conflicts algorithm
keewis 1930d68
verify that drop_conflicts works correctly when passed to concat
keewis a14a4d9
add a test for combine_nested with combine_attrs="drop_conflicts"
keewis edc8428
remove test_merge_attrs in favor of test_merge_arrays_attrs
keewis 9344b93
use a dict comprehension instead of a dict intersection
keewis 1791111
reorganize the drop_conflicts concat test
keewis 2ec123f
use different data for the drop_conflicts merge test
keewis fad94a0
add another test to make sure attrs on variables is also merged
keewis 86c7c5f
correctly set expect_exception
keewis 3531198
skip the combine_attrs merge variables test
keewis be5ad6a
rewrite the concat combine_attrs test and use different data
keewis 5a6b551
also skip the concat combine_attrs variables test
keewis af72603
use utils.equivalent instead of == so ndarray objects can be compared
keewis dbc510a
make sure conflicting keys cannot be added back by other objects
keewis 344d822
don't compare the value with itself if key is not in attrs
keewis 6ed88f7
also check the coordinates
keewis f43c0be
add docstrings to explain the purpose of the tests
keewis ac3c102
update whats-new.rst
keewis ea24b32
add a test for combine_attrs="drop_conflicts" with more than two attrs
keewis 5dd73be
Merge branch 'master' into keep_attrs-drop_conflicts
keewis 81c8271
Merge branch 'master' into keep_attrs-drop_conflicts
keewis 4a4faa5
Merge branch 'master' into keep_attrs-drop_conflicts
keewis 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -92,6 +92,13 @@ def test_merge_arrays_attrs_default(self): | |
{"a": 1, "b": 2}, | ||
False, | ||
), | ||
( | ||
"drop_conflicts", | ||
{"a": 1, "b": 2, "c": 3}, | ||
{"b": 1, "c": 3, "d": 4}, | ||
{"a": 1, "c": 3, "d": 4}, | ||
False, | ||
), | ||
], | ||
) | ||
def test_merge_arrays_attrs( | ||
|
@@ -109,6 +116,64 @@ def test_merge_arrays_attrs( | |
expected.attrs = expected_attrs | ||
assert_identical(actual, expected) | ||
|
||
@pytest.mark.skip(reason="not implemented, yet (see #4827)") | ||
@pytest.mark.parametrize( | ||
"combine_attrs, var1_attrs, var2_attrs, expected_attrs, expect_exception", | ||
[ | ||
( | ||
"no_conflicts", | ||
{"a": 1, "b": 2}, | ||
{"a": 1, "c": 3}, | ||
{"a": 1, "b": 2, "c": 3}, | ||
False, | ||
), | ||
("no_conflicts", {"a": 1, "b": 2}, {}, {"a": 1, "b": 2}, False), | ||
("no_conflicts", {}, {"a": 1, "c": 3}, {"a": 1, "c": 3}, False), | ||
( | ||
"no_conflicts", | ||
{"a": 1, "b": 2}, | ||
{"a": 4, "c": 3}, | ||
{"a": 1, "b": 2, "c": 3}, | ||
True, | ||
), | ||
("drop", {"a": 1, "b": 2}, {"a": 1, "c": 3}, {}, False), | ||
("identical", {"a": 1, "b": 2}, {"a": 1, "b": 2}, {"a": 1, "b": 2}, False), | ||
("identical", {"a": 1, "b": 2}, {"a": 1, "c": 3}, {"a": 1, "b": 2}, True), | ||
( | ||
"override", | ||
{"a": 1, "b": 2}, | ||
{"a": 4, "b": 5, "c": 3}, | ||
{"a": 1, "b": 2}, | ||
False, | ||
), | ||
( | ||
"drop_conflicts", | ||
{"a": 1, "b": 2, "c": 3}, | ||
{"b": 1, "c": 3, "d": 4}, | ||
{"a": 1, "c": 3, "d": 4}, | ||
False, | ||
), | ||
], | ||
) | ||
def test_merge_arrays_attrs_variables( | ||
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. dito |
||
self, combine_attrs, var1_attrs, var2_attrs, expected_attrs, expect_exception | ||
): | ||
data = create_test_data() | ||
data1 = data.copy() | ||
data1.var1.attrs = var1_attrs | ||
data2 = data.copy() | ||
data2.var1.attrs = var2_attrs | ||
|
||
if expect_exception: | ||
with raises_regex(MergeError, "combine_attrs"): | ||
actual = xr.merge([data1, data2], combine_attrs=combine_attrs) | ||
else: | ||
actual = xr.merge([data1, data2], combine_attrs=combine_attrs) | ||
expected = data.copy() | ||
expected.var1.attrs = expected_attrs | ||
|
||
assert_identical(actual, expected) | ||
|
||
def test_merge_attrs_override_copy(self): | ||
ds1 = xr.Dataset(attrs={"x": 0}) | ||
ds2 = xr.Dataset(attrs={"x": 1}) | ||
|
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.
or
data_vars
(took me a while to figure out what was different from above)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.
thanks, I forgot to check the coords. The purpose of these tests is to check whether
combine_attrs
is applied to the variables (coords and data variables) in addition to the top-levelDataset
/DataArray
. Would it help to have a comment explaining the difference?