Created
November 28, 2023 16:35
-
-
Save mazzma12/7b2c46145b1eb1fe97f1927dd43dd400 to your computer and use it in GitHub Desktop.
handy json serializer for numpy and pandas type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
def default_json_encoder(value): | |
# This should be built-in ... | |
if isinstance(value, (datetime.datetime, pd.Timestamp)): | |
return value.isoformat() | |
elif isinstance(value, np.bool_): | |
return bool(value) | |
elif isinstance(value, np.floating): | |
return float(value) | |
elif isinstance(value, np.integer): | |
return int(value) | |
elif isinstance(value, np.ndarray): | |
return value.tolist() | |
elif isinstance(value, set): | |
return list(value) | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment