Skip to main content
Documentation Status https://github.com/MacHu-GWU/jsonpolars-project/actions/workflows/main.yml/badge.svg https://codecov.io/gh/MacHu-GWU/jsonpolars-project/branch/main/graph/badge.svg https://img.shields.io/pypi/v/jsonpolars.svg https://img.shields.io/pypi/l/jsonpolars.svg https://img.shields.io/pypi/pyversions/jsonpolars.svg https://img.shields.io/badge/Release_History!--None.svg?style=social https://img.shields.io/badge/STAR_Me_on_GitHub!--None.svg?style=social
https://img.shields.io/badge/Link-Document-blue.svg https://img.shields.io/badge/Link-API-blue.svg https://img.shields.io/badge/Link-Install-blue.svg https://img.shields.io/badge/Link-GitHub-blue.svg https://img.shields.io/badge/Link-Submit_Issue-blue.svg https://img.shields.io/badge/Link-Request_Feature-blue.svg https://img.shields.io/badge/Link-Download-blue.svg

Welcome to jsonpolars Documentation

https://jsonpolars.readthedocs.io/en/latest/_static/jsonpolars-logo.png

jsonpolars is an innovative Python library designed to bridge the gap between JSON-based data manipulation syntax and the powerful Polars data processing library. This project aims to provide a flexible and intuitive way to express Polars operations using JSON structures, making it easier for developers to work with Polars in various contexts. The library allows users to define complex data transformations using JSON syntax, which can then be translated into native Polars operations.

Here’s a simple example of how to use jsonpolars:

import polars as pl
from jsonpolars.api import parse_dfop

# Create a sample DataFrame
df = pl.DataFrame(
    [
        {"id": 1, "firstname": "Alice", "lastname": "Smith"},
        {"id": 2, "firstname": "Bob", "lastname": "Johnson"},
        {"id": 3, "firstname": "Cathy", "lastname": "Williams"},
    ]
)

# Define the operation using JSON structure
dfop_data = {
    "type": "with_columns",
    "exprs": [
        {
            "type": "alias",
            "name": "fullname",
            "expr": {
                "type": "plus",
                "left": {"type": "column", "name": "firstname"},
                "right": {
                    "type": "plus",
                    "left": {
                        "type": "lit",
                        "value": " ",
                    },
                    "right": {"type": "column", "name": "lastname"},
                },
            },
        }
    ],
}

# Parse and apply the operation
op = parse_dfop(dfop_data)
df1 = op.to_polars(df)
print(df1)

Output:

shape: (3, 4)
┌─────┬───────────┬──────────┬────────────────┐
│ id  ┆ firstname ┆ lastname ┆ fullname       │
│ --- ┆ ---       ┆ ---      ┆ ---            │
│ i64 ┆ str       ┆ str      ┆ str            │
╞═════╪═══════════╪══════════╪════════════════╡
│ 1   ┆ Alice     ┆ Smith    ┆ Alice Smith    │
│ 2   ┆ Bob       ┆ Johnson  ┆ Bob Johnson    │
│ 3   ┆ Cathy     ┆ Williams ┆ Cathy Williams │
└─────┴───────────┴──────────┴────────────────┘

In addition to JSON-based syntax, jsonpolars allows you to define operations using Python objects for a more Pythonic approach. Here’s how you can use this feature:

import json

# Define the operation using Python objects
op = dfop.WithColumns(
    exprs=[
        expr.Alias(
            name="fullname",
            expr=expr.Plus(
                left=expr.Column(name="firstname"),
                right=expr.Plus(
                    left=expr.Lit(value=" "),
                    right=expr.Column(name="lastname"),
                ),
            ),
        )
    ]
)

# Convert the operation to JSON (optional, for visualization)
print(json.dumps(op.to_dict(), indent=4))

Output:

{
    "type": "with_columns",
    "exprs": [
        {
            "type": "alias",
            "name": "fullname",
            "expr": {
                "type": "add",
                "left": {
                    "type": "column",
                    "name": "firstname"
                },
                "right": {
                    "type": "add",
                    "left": {
                        "type": "func_lit",
                        "value": " ",
                        "dtype": null,
                        "allow_object": false
                    },
                    "right": {
                        "type": "column",
                        "name": "lastname"
                    }
                }
            }
        }
    ],
    "named_exprs": {}
}

The to_polars() method seamlessly translates your Python object-based operation into Polars code, allowing you to apply complex transformations with ease.

# Apply the operation to a Polars DataFrame
df1 = op.to_polars(df)
print(df1)

Output:

shape: (3, 4)
┌─────┬───────────┬──────────┬────────────────┐
│ id  ┆ firstname ┆ lastname ┆ fullname       │
│ --- ┆ ---       ┆ ---      ┆ ---            │
│ i64 ┆ str       ┆ str      ┆ str            │
╞═════╪═══════════╪══════════╪════════════════╡
│ 1   ┆ Alice     ┆ Smith    ┆ Alice Smith    │
│ 2   ┆ Bob       ┆ Johnson  ┆ Bob Johnson    │
│ 3   ┆ Cathy     ┆ Williams ┆ Cathy Williams │
└─────┴───────────┴──────────┴────────────────┘

Install

jsonpolars is released on PyPI, so all you need is to:

$ pip install jsonpolars

To upgrade to latest version:

$ pip install --upgrade jsonpolars

Metadata

Release files for jsonpolars 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonpolars 0.4.2
File Size Uploaded
jsonpolars-0.4.2.tar.gz 35.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonpolars 0.4.2
File Interpreter ABI Platform
jsonpolars-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 74.3 kB

Release files / jsonpolars-0.4.2.tar.gz

Download URL jsonpolars-0.4.2.tar.gz
Size 35.0 kB
Tags Source
SHA-256 checksum
How to use checksums
12f2ebcb7504b61b97ccfd8d70f349554314c9b89825c772936c1b30f4e3f526
BLAKE2b-256 checksum
How to use checksums
d0935dde3b98dd4804fb33026908edc6325b95b1f1924e7978c63dfa20e77da4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.10

Release files / jsonpolars-0.4.2-py3-none-any.whl

Download URL jsonpolars-0.4.2-py3-none-any.whl
Size 39.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc1cb53b3552d87306fbeae8240ce980a4b2a7af1abf17fcacb74424ac0f6a05
BLAKE2b-256 checksum
How to use checksums
876320557bbc866386a8fd9fbbce726a3e8c0c18f297c7d3c14ab009d0b07c90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.10

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page