So I have 2 structured Numpy Arrays:
a = numpy.array([('2020-01-04', 'Test', 1, 1.0),
('2020-01-05', 'Test2', 2, NaN)],
dtype=[('Date', 'M8[D]'), ('Name', 'S8'), ('idx', 'i8'), ('value', 'f8')])
b = numpy.array([('2020-01-04', 'Test', 2, 1.0),
('2020-01-05', 'Test2', 2, NaN)],
dtype=[('Date', 'M8[D]'), ('Name', 'S8'), ('idx', 'i8'), ('value', 'f8')])
I need to compare the 2 arrays and get an array of True/False
values that will indicate which indices in the array are different.
Doing something like:
not_same = np.full(shape=a.shape, dtype=bool, fill_value=False)
for field in a.dtype.names:
not_same = np.logical_or(not_same,
a[field] != b[field])
works to a point but comparison of NaN != NaN
is actually True
, so I would need to use something like np.allclose
but you can only do this if the values you're comparing are floating point (Strings blow up).
So I need either one of 2 things either:
- Determine that values in the
a[field]
are floating point or not
or
- A method of comparing 2 arrays which will allow comparison of 2 NaN values that will be give you True
Per Request below regarding the error:
dt = np.dtype([('string', 'S10'), ('val', 'f8')])
arr = np.array([('test', 1.0)], dtype=dt)
np.isreal(arr['string'])
Ran on Ubuntu 20.04 with Python 3.8.5