Search code examples
pythonpandaspython-polars

Polars gives exception for empty json, but Pandas works


While reading json from files, using polars give exception when it has empty json. But it is not the same in pandas. I have some files with only key and not value.

Is polars still primitive and not matured like pandas? which package to use?

Polars

from io import StringIO
import polars as pl

print(pl.read_json(StringIO('{"key":{}}')))

gives exception

pyo3_runtime.PanicException: called `Result::unwrap()` on an `Err` value: ComputeError(ErrString("a StructArray must contain at least one field"))

Pandas

from io import StringIO
import pandas as pd

print(pd.read_json(StringIO('{"key":{}}')))

works without exception, gives output as

Empty DataFrame
Columns: [key]
Index: []

Solution

  • Pandas is mature and Polars is comparatively a new library. There is an issue exists, which is same as yours, refer to this bug link, Polars Issue Link

    Either you can use exception handling with polars for this scenario or go with pandas.

    Update: The issue is closed now, you it is working in Polars