Skip to content

Commit 66b466e

Browse files
authored
Merge pull request #224 from OpenUpSA/elastic-debugging
Dealing with Nones
2 parents 2e244bb + 41911b9 commit 66b466e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

helpers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,18 @@ def tsv_to_json_pandas(tsv_string, project_id):
585585
# Other types: leave as is
586586

587587
# Replace all NaN with None for JSON compatibility
588+
import math
589+
def clean_nans(obj):
590+
if isinstance(obj, float) and math.isnan(obj):
591+
return None
592+
if isinstance(obj, dict):
593+
return {k: clean_nans(v) for k, v in obj.items()}
594+
if isinstance(obj, list):
595+
return [clean_nans(x) for x in obj]
596+
return obj
597+
588598
df = df.where(pd.notnull(df), None)
589-
json_list = df.to_dict('records')
599+
json_list = [clean_nans(row) for row in df.to_dict('records')]
590600
return json_list
591601

592602

0 commit comments

Comments
 (0)