I'm trying to use this code to convert a parquet into a json file: import pandas as pd…
- User
I'm trying to use this code to convert a parquet into a json file: import pandas as pd import numpy as np import json
Convert numpy arrays to python lists
def ndarray_to_list(val): if isinstance(val, np.ndarray): return val.tolist() return val
Load the Parquet dataset into a pandas DataFrame
df = pd.read_parquet('train-00000-of-00002-17222c96b55e9377.parquet')
Apply the conversion function to every cell in the DataFrame
df = df.applymap(ndarray_to_list)
Convert the DataFrame to a list of dictionaries (JSON entries)
json_entries = df.to_dict(orient='records')
Save the list of JSON entries to a .json file with an indent of 2
with open('orca_best_1.json', 'w') as f: json.dump(json_entries, f, indent=2)
but I get this error: Traceback (most recent call last): File "c:\Teknium\Datasets\Hermes-v2\source-datasets\orca-best\data\convert_parquet_to_json.py", line 22, in json.dump(json_entries, f, indent=2) File "C:\program files\python311\Lib\json_init_.py", line 179, in dump for chunk in iterable: File "C:\program files\python311\Lib\json\encoder.py", line 430, in _iterencode yield from _iterencode_list(o, _current_indent_level) File "C:\program files\python311\Lib\json\encoder.py", line 326, in _iterencode_list yield from chunks File "C:\program files\python311\Lib\json\encoder.py", line 406, in _iterencode_dict yield from chunks File "C:\program files\python311\Lib\json\encoder.py", line 406, in _iterencode_dict yield from chunks File "C:\program files\python311\Lib\json\encoder.py", line 439, in _iterencode o = _default(o) ^^^^^^^^^^^ File "C:\program files\python311\Lib\json\encoder.py", line 180, in default raise TypeError(f'Object of type {o.class.name} ' TypeError: Object of type ndarray is not JSON serializable
- Assistant
The error you're seeing is due to the presence of
numpyndarrayobjects in yourDataFramethat have not been successfully converted to regular Python lists, which the module can't serialize.