Skip to content

feat(python): Allow custom JSONEncoder for the json_normalize function, minor speedup #20966

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

Merged
merged 1 commit into from
Jan 29, 2025

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 29, 2025

Closes #20950.

  • Allows a custom json encoder function to be passed to json_normalize, enabling the caller to target specific performance/behaviour.
  • Minor speedup (~6% faster on test data) from optimising Python loops 🚀
  • No change to default behaviour (which uses json.dumps).
  • Additional JSON test coverage.

Example

import polars as pl

data = [
    {
        "id": n,
        "name": {"first": "Some", "middle": None, "last": "Name"},
        "attrs": {"height": 185, "weight": 90},
        "lifts": {
            "max_deadlift": 190, 
            "max_squat": 140, 
            "max_bench": 100,
        },
    } for n in range(5)
]

Default:

(if not specified, uses json.dumps)

pl.json_normalize(data, max_level=0)
# shape: (5, 4)
# ┌─────┬─────────────────────────────────┬───────────────────────────────┬─────────────────────────────────┐
# │ id  ┆ name                            ┆ attrs                         ┆ lifts                           │
# │ --- ┆ ---                             ┆ ---                           ┆ ---                             │
# │ i64 ┆ str                             ┆ str                           ┆ str                             │
# ╞═════╪═════════════════════════════════╪═══════════════════════════════╪═════════════════════════════════╡
# │ 0   ┆ {"first": "Some", "middle": nu… ┆ {"height": 185, "weight": 90} ┆ {"max_deadlift": 190, "max_squ… │
# │ 1   ┆ {"first": "Some", "middle": nu… ┆ {"height": 185, "weight": 90} ┆ {"max_deadlift": 190, "max_squ… │
# │ 2   ┆ {"first": "Some", "middle": nu… ┆ {"height": 185, "weight": 90} ┆ {"max_deadlift": 190, "max_squ… │
# │ 3   ┆ {"first": "Some", "middle": nu… ┆ {"height": 185, "weight": 90} ┆ {"max_deadlift": 190, "max_squ… │
# │ 4   ┆ {"first": "Some", "middle": nu… ┆ {"height": 185, "weight": 90} ┆ {"max_deadlift": 190, "max_squ… │
# └─────┴─────────────────────────────────┴───────────────────────────────┴─────────────────────────────────┘

Custom encoder:

(use orjson.dumps as the encoder instead; note that output is now binary)

import orjson
pl.json_normalize(data, max_level=0, encoder=orjson.dumps)
# shape: (5, 4)
# ┌─────┬─────────────────────────────────┬───────────────────────────────┬─────────────────────────────────┐
# │ id  ┆ name                            ┆ attrs                         ┆ lifts                           │
# │ --- ┆ ---                             ┆ ---                           ┆ ---                             │
# │ i64 ┆ binary                          ┆ binary                        ┆ binary                          │
# ╞═════╪═════════════════════════════════╪═══════════════════════════════╪═════════════════════════════════╡
# │ 0   ┆ b"{"first":"Some","middle":nul… ┆ b"{"height":185,"weight":90}" ┆ b"{"max_deadlift":190,"max_squ… │
# │ 1   ┆ b"{"first":"Some","middle":nul… ┆ b"{"height":185,"weight":90}" ┆ b"{"max_deadlift":190,"max_squ… │
# │ 2   ┆ b"{"first":"Some","middle":nul… ┆ b"{"height":185,"weight":90}" ┆ b"{"max_deadlift":190,"max_squ… │
# │ 3   ┆ b"{"first":"Some","middle":nul… ┆ b"{"height":185,"weight":90}" ┆ b"{"max_deadlift":190,"max_squ… │
# │ 4   ┆ b"{"first":"Some","middle":nul… ┆ b"{"height":185,"weight":90}" ┆ b"{"max_deadlift":190,"max_squ… │
# └─────┴─────────────────────────────────┴───────────────────────────────┴─────────────────────────────────┘

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Jan 29, 2025
@alexander-beedie alexander-beedie changed the title feat(python): Allow custom JSONEncoder for the json_normalize function feat(python): Allow custom JSONEncoder for the json_normalize function, minor speedup Jan 29, 2025
@ritchie46 ritchie46 merged commit 527b9cf into pola-rs:main Jan 29, 2025
20 of 21 checks passed
@alexander-beedie alexander-beedie deleted the custom-json-encoder branch January 29, 2025 12:00
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 python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow other json callable (like orjson.json) used for loading JSON data
2 participants