Skip to content

Commit

Permalink
Fixed a corner case where numpy's np.float32 nans are not ignored whe…
Browse files Browse the repository at this point in the history
…n using ignore_nan_equality
  • Loading branch information
Noam Gottlieb committed Feb 12, 2023
1 parent 8ab1c8d commit 4cb8400
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
convert_item_or_items_into_compiled_regexes_else_none,
type_is_subclass_of_type_group, type_in_type_group, get_doc,
number_to_string, datetime_normalize, KEY_TO_VAL_STR, booleans,
np_ndarray, get_numpy_ndarray_rows, OrderedSetPlus, RepeatedTimer,
np_ndarray, np_floating, get_numpy_ndarray_rows, OrderedSetPlus, RepeatedTimer,
TEXT_VIEW, TREE_VIEW, DELTA_VIEW, detailed__dict__, add_root_to_paths,
np, get_truncate_datetime, dict_, CannotCompare, ENUM_INCLUDE_KEYS)
from deepdiff.serialization import SerializationMixin
Expand Down Expand Up @@ -1503,7 +1503,7 @@ def _diff(self, level, parents_ids=frozenset(), _original_type=None, local_tree=
self._report_result('values_changed', level, local_tree=local_tree)
return

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

if isinstance(level.t1, booleans):
Expand Down
6 changes: 4 additions & 2 deletions deepdiff/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class np_type:
np_float32 = np_type # pragma: no cover.
np_float64 = np_type # pragma: no cover.
np_float_ = np_type # pragma: no cover.
np_floating = np_type # pragma: no cover.
np_complex64 = np_type # pragma: no cover.
np_complex128 = np_type # pragma: no cover.
np_complex_ = np_type # pragma: no cover.
Expand All @@ -60,6 +61,7 @@ class np_type:
np_float32 = np.float32
np_float64 = np.float64
np_float_ = np.float_
np_floating = np.floating
np_complex64 = np.complex64
np_complex128 = np.complex128
np_complex_ = np.complex_
Expand All @@ -68,7 +70,7 @@ class np_type:
numpy_numbers = (
np_int8, np_int16, np_int32, np_int64, np_uint8,
np_uint16, np_uint32, np_uint64, np_intp, np_uintp,
np_float32, np_float64, np_float_, np_complex64,
np_float32, np_float64, np_float_, np_floating, np_complex64,
np_complex128, np_complex_,)

numpy_complex_numbers = (
Expand Down Expand Up @@ -336,7 +338,7 @@ def number_to_string(number, significant_digits, number_format_notation="f"):
using = number_formatting[number_format_notation]
except KeyError:
raise ValueError("number_format_notation got invalid value of {}. The valid values are 'f' and 'e'".format(number_format_notation)) from None

if not isinstance(number, numbers):
return number
elif isinstance(number, Decimal):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_diff_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@
}
},
},
'numpy_array9_ignore_nan_inequality_float32': {
't1': np.array([1, 2, 3, np.nan], np.float32),
't2': np.array([1, 2, 4, np.nan], np.float32),
'deepdiff_kwargs': {
'ignore_nan_inequality': True,
},
'expected_result': {'values_changed': {'root[2]': {'new_value': 4.0, 'old_value': 3.0}}}
},
'numpy_almost_equal': {
't1': np.array([1.0, 2.3333333333333]),
't2': np.array([1.0, 2.33333334]),
Expand Down

0 comments on commit 4cb8400

Please sign in to comment.