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

Fixed a problem with diff'ing masked_arrays with units. #1345

Merged
merged 1 commit into from
Mar 31, 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
2 changes: 1 addition & 1 deletion src/metpy/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def diff(x, **kwargs):
# Can't just use units because of how things like temperature work
it = x.flat
true_units = (next(it) - next(it)).units
return ret * true_units
return true_units * ret
else:
return np.diff(x, **kwargs)

Expand Down
14 changes: 14 additions & 0 deletions tests/calc/test_calc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,20 @@ def test_first_derivative_masked():
assert_array_equal(df_dx.mask, truth.mask)


def test_first_derivative_masked_units():
"""Test that first_derivative properly propagates masks with units."""
data = units('K') * np.ma.arange(7)
data[3] = np.ma.masked
x = units('m') * np.ma.arange(7)
df_dx = first_derivative(data, x=x)

truth = units('K / m') * np.ma.array(
[1., 1., 1., 1., 1., 1., 1.],
mask=[False, False, True, True, True, False, False])
assert_array_almost_equal(df_dx, truth)
assert_array_equal(df_dx.mask, truth.mask)


def test_second_derivative(deriv_1d_data):
"""Test second_derivative with a simple 1D array."""
d2v_dx2 = second_derivative(deriv_1d_data.values, x=deriv_1d_data.x)
Expand Down