Skip to content

Commit c6c458b

Browse files
committed
ref: correct nan->None replacement
1 parent dbb3a34 commit c6c458b

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

ckanext/dc_serve/serve.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,9 @@ def get_dc_tables(ds, from_basins: bool = False) -> dict:
4545
tables.update(get_dc_tables(bn.ds))
4646
else:
4747
for tab in ds.tables:
48-
# A table is a 2D array
49-
tab_array = ds.tables[tab][:]
50-
tab_list = tab_array.tolist()
51-
if np.any(np.isnan(tab_array)):
52-
# NaN is not part of the JSON specification, so we convert it
53-
# to None before sending the response.
54-
tab_list = [[v if not np.isnan(v) else None for v in column]
55-
for column in tab_list]
56-
48+
tab_list = ds.tables[tab][:].tolist()
49+
# find any NaNs and replace them with None (JSON compatibility)
50+
tab_list = replace_nan_with_none_recursive(tab_list)
5751
tables[tab] = (ds.tables[tab].keys(),
5852
tab_list
5953
)
@@ -305,5 +299,13 @@ def get_resource_kernel_complement_condensed(r_data):
305299
r_data["complemented-condensed"] = True
306300

307301

302+
def replace_nan_with_none_recursive(data):
303+
if isinstance(data, (list, tuple)):
304+
return [replace_nan_with_none_recursive(item) for item in data]
305+
else:
306+
return data if not np.isnan(data) else None
307+
308+
309+
308310
atexit.register(is_dc_resource.cache_clear)
309311
atexit.register(get_resource_kernel_base.cache_clear)

0 commit comments

Comments
 (0)