Package short description.
Project description
Welcome to jsonpolars Documentation
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jsonpolars-0.4.1.tar.gz
.
File metadata
- Download URL: jsonpolars-0.4.1.tar.gz
- Upload date:
- Size: 34.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f11976dbd72dcac46671e7fe6a6773ebf911d28f13042497a881cd8244668be2 |
|
MD5 | 481da65ad867fe284564da9d47ab8a9d |
|
BLAKE2b-256 | 3ff11bde786db24c4cd9425041cd42d10b58210ee22c6dd776fe26d1171f75c4 |
File details
Details for the file jsonpolars-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: jsonpolars-0.4.1-py3-none-any.whl
- Upload date:
- Size: 38.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 479409211b137817cf4d14eed73a769884b92621e0ece48a666ee609ecee7505 |
|
MD5 | b1fd9534be0bd621cc8043dcc136dfc3 |
|
BLAKE2b-256 | 3aa5b76896d42e8b3f5c5bbff816d53e9cf2fbb4832349583947addd8b682e7f |