Skip to content
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

BUG: Fix for xarray master #1578

Merged
merged 1 commit into from Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/metpy/xarray.py
Expand Up @@ -922,7 +922,7 @@ def mapping_func(da):
# Apply across all variables and coordinates
return (
self._dataset
.map(mapping_func, keep_attrs=True)
.map(mapping_func)
.assign_coords({
coord_name: mapping_func(coord_var)
for coord_name, coord_var in self._dataset.coords.items()
Expand All @@ -931,11 +931,11 @@ def mapping_func(da):

def quantify(self):
"""Return new dataset with all numeric variables quantified and cached data loaded."""
return self._dataset.map(lambda da: da.metpy.quantify(), keep_attrs=True)
return self._dataset.map(lambda da: da.metpy.quantify())

def dequantify(self):
"""Return new dataset with variables cast to magnitude and units on attribute."""
return self._dataset.map(lambda da: da.metpy.dequantify(), keep_attrs=True)
return self._dataset.map(lambda da: da.metpy.dequantify())


def _assign_axis(attributes, axis):
Expand Down
10 changes: 9 additions & 1 deletion tests/test_xarray.py
Expand Up @@ -169,11 +169,13 @@ def test_quantify(test_ds_generic):

def test_dequantify():
"""Test dequantify method for converting data away from Quantity."""
original = xr.DataArray(units.Quantity([280, 290, 300], 'K'))
original = xr.DataArray(units.Quantity([280, 290, 300], 'K'),
attrs={'standard_name': 'air_temperature'})
result = original.metpy.dequantify()
assert isinstance(result.data, np.ndarray)
assert result.attrs['units'] == 'kelvin'
np.testing.assert_array_almost_equal(result.data, original.data.magnitude)
assert result.attrs['standard_name'] == 'air_temperature'


def test_dataset_quantify(test_ds_generic):
Expand Down Expand Up @@ -1026,6 +1028,8 @@ def test_update_attribute_dictionary(test_ds_generic):
'test': 'Filler data',
'c': 'The third coordinate'
}
test_ds_generic.c.attrs['units'] = 'K'
test_ds_generic.a.attrs['standard_name'] = 'air_temperature'
result = test_ds_generic.metpy.update_attribute('description', descriptions)

# Test attribute updates
Expand All @@ -1036,6 +1040,10 @@ def test_update_attribute_dictionary(test_ds_generic):
assert 'description' not in result['e'].attrs
assert result['test'].attrs['description'] == 'Filler data'

# Test that other attributes remain
assert result['c'].attrs['units'] == 'K'
assert result['a'].attrs['standard_name'] == 'air_temperature'

# Test for no side effects
assert 'description' not in test_ds_generic['c'].attrs
assert 'description' not in test_ds_generic['test'].attrs
Expand Down