Fixed bug: to_serializable_dtype() did not identify correctly dtype of array's entries with object dtype

This commit is contained in:
2024-10-28 18:49:22 +01:00
parent 74633adf7f
commit 3f7a089a28

View File

@ -326,17 +326,23 @@ def to_serializable_dtype(value):
if value.dtype.names:
value = {field: to_serializable_dtype(value[field]) for field in value.dtype.names}
else:
# Handling regular array NumPy types
if np.issubdtype(value.dtype, np.bytes_):
# Handling regular array NumPy types with assumption of unform dtype accross array elements
# TODO: evaluate a more general way to check for individual dtypes
if isinstance(value[0], bytes):
# Decode bytes
value = [item.decode('utf-8') for item in value] if len(value) > 1 else value[0].decode('utf-8')
elif np.issubdtype(value.dtype, np.unicode_):
elif isinstance(value[0], str):
# Already a string type
value = [str(item) for item in value] if len(value) > 1 else str(value[0])
elif np.issubdtype(value.dtype, np.integer):
elif isinstance(value[0], int):
# Integer type
value = [int(item) for item in value] if len(value) > 1 else int(value[0])
elif np.issubdtype(value.dtype, np.floating):
elif isinstance(value[0], float):
# Floating type
value = [float(item) for item in value] if len(value) > 1 else float(value[0])
else:
print('Yaml-compatible data-type was not found. Value has been set to NaN.')
print("Debug: value.dtype is", value.dtype)
value = np.nan
except Exception as e: