Giter Site home page Giter Site logo

Comments (4)

asishm avatar asishm commented on July 22, 2024

You can achieve this using the default_handler in df.to_json.

pd.DataFrame({"uuid": [uuid.uuid4()]}).to_json(default_handler=str).

Also the stdlib json library (pandas uses a vendored version of ujson iirc) also doesn't serialize uuids natively.

import json
import uuid
json.dumps({"a": uuid.uuid4()}) # raises with TypeError: Object of type UUID is not JSON serializable
json.dumps({"a": uuid.uuid4()}, default=str) # works

from pandas.

Siddharth-Latthe-07 avatar Siddharth-Latthe-07 commented on July 22, 2024

@grieve54706 The issue you are encountering arises because pandas does not natively support serialization of uuid.UUID instances to JSON. When you attempt to serialize a DataFrame containing UUID objects using to_json(), it results in encoding errors.
to produce the expected behaviour
u can try out this:-
convert the UUID objects to their string representations before serializing the DataFrame to JSON.

import uuid
import pandas as pd

# Create a DataFrame with a UUID column
df = pd.DataFrame({"uuid": [uuid.uuid4()]})

# Convert UUID objects to strings
df['uuid'] = df['uuid'].astype(str)

# Serialize the DataFrame to JSON
json_data = df.to_json()
print(json_data)

Plz let me know if the above works
thanks

from pandas.

grieve54706 avatar grieve54706 commented on July 22, 2024

Thanks, guys. I think your suggestions all work.

I provide a tool to connect many databases and put the data into pandas for other people, so I will not know which column is UUID and databases have different data that could be dtype Object too.
I found the orjson serializes UUID to the string by default. Curious, pandas to JSON should follow RFC 4122 too?

from pandas.

grieve54706 avatar grieve54706 commented on July 22, 2024

Thank you everyone. I will use orjson to handle UUID.

import orjson
import uuid
import pandas as pd

df = pd.DataFrame({"uuid": [uuid.uuid4()]})
json_data  = orjson.loads(orjson.dumps(df.to_dict(orient="split"), option=orjson.OPT_SERIALIZE_UUID))

from pandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.