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

add an ignore_nulls option to json_encode for the pl.Struct column #17865

Open
nikhilramolla opened this issue Jul 25, 2024 · 0 comments
Open
Labels
enhancement New feature or an improvement of an existing feature

Comments

@nikhilramolla
Copy link

Description

pl.Struct contains a json_encode method which dumps a struct to a json string.

It would to useful to skip null values in the struct's items when dumping.

Example:

df = pl.DataFrame(
    {
        "struct": [
            { "a": "p", "b": None },
            { "a": None, "b": "q" },
        ],
    },
    schema=[
        ("struct", pl.Struct({
            "a": pl.String,
            "b": pl.String,
        }))
    ],
)
print(df)
print(df.select(pl.col("struct").struct.json_encode()))
print(df.select(pl.col("struct").struct.json_encode(ignore_nulls=True)))
shape: (2, 1)
┌────────────┐
│ struct     │
│ ---        │
│ struct[2]  │
╞════════════╡
│ {"p",null} │
│ {null,"q"} │
└────────────┘
shape: (2, 1)
┌────────────────────┐
│ struct             │
│ ---                │
│ str                │
╞════════════════════╡
│ {"a":"p","b":null} │
│ {"a":null,"b":"q"} │
└────────────────────┘
shape: (2, 1)
┌───────────┐
│ struct    │
│ ---       │
│ str       │
╞═══════════╡
│ {"a":"p"} │
│ {"b":"q"} │
└───────────┘
@nikhilramolla nikhilramolla added the enhancement New feature or an improvement of an existing feature label Jul 25, 2024
@nikhilramolla nikhilramolla changed the title add a ignore_nulls option to json_encode for the pl.Struct column add an ignore_nulls option to json_encode for the pl.Struct column Jul 25, 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