Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support serializing to json in a format similar to pandas "split" orientation #18784

Open
CaselIT opened this issue Sep 17, 2024 · 0 comments
Open
Labels
enhancement New feature or an improvement of an existing feature

Comments

@CaselIT
Copy link
Contributor

CaselIT commented Sep 17, 2024

Description

Pandas supports a json serialization that's quite compact compared to the classic list of dict one.
This may be useful when sending dataframe via api calls.

Example:

df = (
    pl.DataFrame({k: [i for i in range(4)] for k in "abc"})
    .select(pl.col.a / 3, pl.col.c.cast(pl.String))
    .to_pandas()
)
print(df.to_json(orient="split"))

The above prints

{"columns":["a","c"],"index":[0,1,2,3],"data":[[0.0,"0"],[0.3333333333,"1"],[0.6666666667,"2"],[1.0,"3"]]}

Since polars has no index, it could output just columns and data.
Note that data, a bit unfortunately, is row-major, not column-major.

While using to_pandas() to get a pandas dataframe is an option, it's usually quite slow, and seems a waste to do that just to then serialize it again.

@CaselIT CaselIT added the enhancement New feature or an improvement of an existing feature label Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

1 participant