diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29b..244bdde6 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,4 @@ +- bump: patch + changes: + changed: + - Replaced unsafe numpy-Python comparison with use of numpy dtype to convert byte-string arrays to Unicode ones within enums \ No newline at end of file diff --git a/policyengine_core/enums/enum.py b/policyengine_core/enums/enum.py index 584b1bd4..b55ec347 100644 --- a/policyengine_core/enums/enum.py +++ b/policyengine_core/enums/enum.py @@ -49,8 +49,11 @@ def encode(cls, array: Union[EnumArray, np.ndarray]) -> EnumArray: if isinstance(array, EnumArray): return array - # if array.dtype.kind == "b": - if isinstance(array == 0, bool): + # First, convert byte-string arrays to Unicode-string arrays + # Confusingly, Numpy uses "S" to refer to byte-string arrays + # and "U" to refer to Unicode-string arrays, which are also + # referred to as the "str" type + if array.dtype.kind == "S": # Convert boolean array to string array array = array.astype(str)