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 bug that showed when trying to establish equivalence of two a… #837

Closed
wants to merge 8 commits into from
13 changes: 12 additions & 1 deletion xarray/core/ops.py
Expand Up @@ -187,7 +187,18 @@ def array_equiv(arr1, arr2):
arr1, arr2 = as_like_arrays(arr1, arr2)
if arr1.shape != arr2.shape:
return False
return bool(((arr1 == arr2) | (isnull(arr1) & isnull(arr2))).all())

flag_array = (arr1 == arr2)
try:

flag_array |= (isnull(arr1) & isnull(arr2))

except TypeError:
Copy link
Member

Choose a reason for hiding this comment

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

could you add a brief comment explaining why we allow TypeError here? a reference to this issue (e.g., GH837)` could be nice, too.

pass

return bool(flag_array.all())

# return bool(((arr1 == arr2) | (isnull(arr1) & isnull(arr2))).all())
Copy link
Member

Choose a reason for hiding this comment

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

remove commented out lines



def _call_possibly_missing_method(arg, name, args, kwargs):
Expand Down
66 changes: 65 additions & 1 deletion xarray/test/test_dataarray.py
Expand Up @@ -8,7 +8,10 @@
Coordinate, Variable)
from xarray.core.pycompat import iteritems, OrderedDict
from xarray.core.common import _full_like
from . import (TestCase, ReturnItem, source_ndarray, unittest, requires_dask,
# from . import (TestCase, ReturnItem, source_ndarray, unittest, requires_dask,
# requires_bottleneck)

Copy link
Member

Choose a reason for hiding this comment

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

remove this commented out code.

from xarray.test import (TestCase, ReturnItem, source_ndarray, unittest, requires_dask,
requires_bottleneck)


Expand Down Expand Up @@ -63,6 +66,67 @@ def test_data_property(self):
self.assertArrayEqual(2 * np.ones((3, 4)), actual.data)
self.assertArrayEqual(actual.data, actual.values)

def test_struct_array_dims(self):
# -----------------test 1
p_data = np.array([('John', 180), ('Stacy', 150), ('Dick', 200)],
dtype=[('name', '|S256'), ('height', object)])
p_data_1 = np.array([('John', 180), ('Stacy', 150), ('Dick', 200)],
dtype=[('name', '|S256'), ('height', object)])

weights_0 = DataArray([80, 56, 120], dims=['participant'],
coords={'participant': p_data})
weights_1 = DataArray([81, 52, 115], dims=['participant'],
coords={'participant': p_data_1})

actual = weights_1 - weights_0

expected = DataArray([1, -4, -5], dims=['participant'],
coords={'participant': p_data})

self.assertDataArrayIdentical(actual, expected)

# -----------------test 2
p_data = np.array([('John', 180), ('Stacy', 150), ('Dick', 200)],
dtype=[('name', '|S256'), ('height', object)])
p_data_1 = np.array([('John', 180), ('Stacy', 151), ('Dick', 200)],
dtype=[('name', '|S256'), ('height', object)])

p_data_2 = np.array([('John', 180), ('Dick', 200)],
dtype=[('name', '|S256'), ('height', object)])

weights_0 = DataArray([80, 56, 120], dims=['participant'],
coords={'participant': p_data})
weights_1 = DataArray([81, 52, 115], dims=['participant'],
coords={'participant': p_data_1})

actual = weights_1 - weights_0

expected = DataArray([1, -5], dims=['participant'],
coords={'participant': p_data_2})

self.assertDataArrayIdentical(actual, expected)

# -----------------test 3 - np.nan
p_data = np.array([('John', 180), ('Stacy', 150), ('Dick', 200)],
dtype=[('name', '|S256'), ('height', object)])
p_data_1 = np.array([('John', 180), ('Stacy', np.nan), ('Dick', 200)],
dtype=[('name', '|S256'), ('height', object)])

p_data_2 = np.array([('John', 180), ('Dick', 200)],
dtype=[('name', '|S256'), ('height', object)])

weights_0 = DataArray([80, 56, 120], dims=['participant'],
coords={'participant': p_data})
weights_1 = DataArray([81, 52, 115], dims=['participant'],
coords={'participant': p_data_1})

actual = weights_1 - weights_0

expected = DataArray([1, -5], dims=['participant'],
coords={'participant': p_data_2})

self.assertDataArrayIdentical(actual, expected)

Copy link
Member

Choose a reason for hiding this comment

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

I think the tests you've written are overly verbose. I don't think you need to recreate p_data* and weights* for each of your tests. The way it is written now, I'm not sure what the differences between your three tests are (at least test1 and test2).

def test_name(self):
arr = self.dv
self.assertEqual(arr.name, 'foo')
Expand Down