Skip to content

Commit 4cb8400

Browse files
author
Noam Gottlieb
committed
Fixed a corner case where numpy's np.float32 nans are not ignored when using ignore_nan_equality
1 parent 8ab1c8d commit 4cb8400

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

deepdiff/diff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
convert_item_or_items_into_compiled_regexes_else_none,
2222
type_is_subclass_of_type_group, type_in_type_group, get_doc,
2323
number_to_string, datetime_normalize, KEY_TO_VAL_STR, booleans,
24-
np_ndarray, get_numpy_ndarray_rows, OrderedSetPlus, RepeatedTimer,
24+
np_ndarray, np_floating, get_numpy_ndarray_rows, OrderedSetPlus, RepeatedTimer,
2525
TEXT_VIEW, TREE_VIEW, DELTA_VIEW, detailed__dict__, add_root_to_paths,
2626
np, get_truncate_datetime, dict_, CannotCompare, ENUM_INCLUDE_KEYS)
2727
from deepdiff.serialization import SerializationMixin
@@ -1503,7 +1503,7 @@ def _diff(self, level, parents_ids=frozenset(), _original_type=None, local_tree=
15031503
self._report_result('values_changed', level, local_tree=local_tree)
15041504
return
15051505

1506-
if self.ignore_nan_inequality and isinstance(level.t1, float) and str(level.t1) == str(level.t2) == 'nan':
1506+
if self.ignore_nan_inequality and isinstance(level.t1, (float, np_floating)) and str(level.t1) == str(level.t2) == 'nan':
15071507
return
15081508

15091509
if isinstance(level.t1, booleans):

deepdiff/helper.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class np_type:
3939
np_float32 = np_type # pragma: no cover.
4040
np_float64 = np_type # pragma: no cover.
4141
np_float_ = np_type # pragma: no cover.
42+
np_floating = np_type # pragma: no cover.
4243
np_complex64 = np_type # pragma: no cover.
4344
np_complex128 = np_type # pragma: no cover.
4445
np_complex_ = np_type # pragma: no cover.
@@ -60,6 +61,7 @@ class np_type:
6061
np_float32 = np.float32
6162
np_float64 = np.float64
6263
np_float_ = np.float_
64+
np_floating = np.floating
6365
np_complex64 = np.complex64
6466
np_complex128 = np.complex128
6567
np_complex_ = np.complex_
@@ -68,7 +70,7 @@ class np_type:
6870
numpy_numbers = (
6971
np_int8, np_int16, np_int32, np_int64, np_uint8,
7072
np_uint16, np_uint32, np_uint64, np_intp, np_uintp,
71-
np_float32, np_float64, np_float_, np_complex64,
73+
np_float32, np_float64, np_float_, np_floating, np_complex64,
7274
np_complex128, np_complex_,)
7375

7476
numpy_complex_numbers = (
@@ -336,7 +338,7 @@ def number_to_string(number, significant_digits, number_format_notation="f"):
336338
using = number_formatting[number_format_notation]
337339
except KeyError:
338340
raise ValueError("number_format_notation got invalid value of {}. The valid values are 'f' and 'e'".format(number_format_notation)) from None
339-
341+
340342
if not isinstance(number, numbers):
341343
return number
342344
elif isinstance(number, Decimal):

tests/test_diff_numpy.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@
105105
}
106106
},
107107
},
108+
'numpy_array9_ignore_nan_inequality_float32': {
109+
't1': np.array([1, 2, 3, np.nan], np.float32),
110+
't2': np.array([1, 2, 4, np.nan], np.float32),
111+
'deepdiff_kwargs': {
112+
'ignore_nan_inequality': True,
113+
},
114+
'expected_result': {'values_changed': {'root[2]': {'new_value': 4.0, 'old_value': 3.0}}}
115+
},
108116
'numpy_almost_equal': {
109117
't1': np.array([1.0, 2.3333333333333]),
110118
't2': np.array([1.0, 2.33333334]),

0 commit comments

Comments
 (0)