-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add methods for combining variables of differing dimensionality #1597
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
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
8c947e7
Add stack_cat and unstack_cat methods
nbren12 e997f7f
Fix to_stacked_array with new master
nbren12 3d757da
Move entry in whats-new to most recent release
nbren12 151dc71
Fix code styling errors
nbren12 8a1a8ef
Improve docstring of to_stacked_array
nbren12 e8594f1
Move "See Also" section to end of docstring
nbren12 0f1ba22
Doc and comment improvements.
nbren12 1e1f4d9
Merge remote-tracking branch 'upstream/master'
nbren12 35e0ecf
Improve documented example
nbren12 23d9246
Add name argument to to_stacked_array and test
nbren12 099d440
Allow level argument to be an int or str
nbren12 e40b6a2
Remove variable_dim argument of to_unstacked_array
nbren12 35a2365
Actually removed variable_dim
nbren12 35715dc
Merge remote-tracking branch 'upstream/master'
nbren12 5ca9a1d
Change function signature of to_stacked_array
nbren12 2979c75
Fix lint error
nbren12 c17dc09
Fix validation and failing tests
nbren12 ce3b52e
Fix typo
nbren12 4ade43d
Merge remote-tracking branch 'upstream/master'
nbren12 6d520c2
Improve docs and error messages
nbren12 2669797
Remove extra spaces
nbren12 24b2237
Merge remote-tracking branch 'upstream/master'
nbren12 13587c2
Test warning in to_unstacked_dataset
nbren12 95e2da9
Improve formatting and naming
nbren12 7aa7095
Fix flake8 error
nbren12 e08622a
Respond to @max-sixty's suggestions
nbren12 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
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
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 |
---|---|---|
|
@@ -110,6 +110,14 @@ def create_test_multiindex(): | |
return Dataset({}, {'x': mindex}) | ||
|
||
|
||
def create_test_stacked_array(): | ||
x = DataArray(pd.Index(np.r_[:10], name='x')) | ||
y = DataArray(pd.Index(np.r_[:20], name='y')) | ||
a = x * y | ||
b = x * y * y | ||
return a, b | ||
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. No need to change, but this would be ideal as test fixture |
||
|
||
|
||
class InaccessibleVariableDataStore(backends.InMemoryDataStore): | ||
def __init__(self): | ||
super(InaccessibleVariableDataStore, self).__init__() | ||
|
@@ -2449,6 +2457,61 @@ def test_stack_unstack_slow(self): | |
actual = stacked.isel(z=slice(None, None, -1)).unstack('z') | ||
assert actual.identical(ds[['b']]) | ||
|
||
def test_to_stacked_array_invalid_sample_dims(self): | ||
data = xr.Dataset( | ||
data_vars={'a': (('x', 'y'), [[0, 1, 2], [3, 4, 5]]), | ||
'b': ('x', [6, 7])}, | ||
coords={'y': ['u', 'v', 'w']} | ||
) | ||
with pytest.raises(ValueError): | ||
data.to_stacked_array("features", sample_dims=['y']) | ||
|
||
def test_to_stacked_array_name(self): | ||
name = 'adf9d' | ||
|
||
# make a two dimensional dataset | ||
a, b = create_test_stacked_array() | ||
D = xr.Dataset({'a': a, 'b': b}) | ||
sample_dims = ['x'] | ||
|
||
y = D.to_stacked_array('features', sample_dims, name=name) | ||
assert y.name == name | ||
|
||
def test_to_stacked_array_dtype_dims(self): | ||
# make a two dimensional dataset | ||
a, b = create_test_stacked_array() | ||
D = xr.Dataset({'a': a, 'b': b}) | ||
sample_dims = ['x'] | ||
y = D.to_stacked_array('features', sample_dims) | ||
assert y.indexes['features'].levels[1].dtype == D.y.dtype | ||
assert y.dims == ('x', 'features') | ||
|
||
def test_to_stacked_array_to_unstacked_dataset(self): | ||
# make a two dimensional dataset | ||
a, b = create_test_stacked_array() | ||
D = xr.Dataset({'a': a, 'b': b}) | ||
sample_dims = ['x'] | ||
y = D.to_stacked_array('features', sample_dims)\ | ||
.transpose("x", "features") | ||
|
||
x = y.to_unstacked_dataset("features") | ||
assert_identical(D, x) | ||
|
||
# test on just one sample | ||
x0 = y[0].to_unstacked_dataset("features") | ||
d0 = D.isel(x=0) | ||
assert_identical(d0, x0) | ||
|
||
def test_to_stacked_array_to_unstacked_dataset_different_dimension(self): | ||
# test when variables have different dimensionality | ||
a, b = create_test_stacked_array() | ||
sample_dims = ['x'] | ||
D = xr.Dataset({'a': a, 'b': b.isel(y=0)}) | ||
|
||
y = D.to_stacked_array('features', sample_dims) | ||
x = y.to_unstacked_dataset('features') | ||
assert_identical(D, x) | ||
|
||
def test_update(self): | ||
data = create_test_data(seed=0) | ||
expected = data.copy() | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.