Skip to content

Commit

Permalink
empty_json_to_empty_df
Browse files Browse the repository at this point in the history
  • Loading branch information
deanm0000 committed Sep 19, 2024
1 parent 9384945 commit 7092f3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/polars-io/src/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,14 @@ where
} else {
simd_json::to_borrowed_value(owned).map_err(to_compute_err)?
};

match &json_value {
BorrowedValue::Array(array) => {
if array.is_empty() {
return Ok(DataFrame::empty());
}
},
_ => {},
}
// struct type
let dtype = if let Some(mut schema) = self.schema {
if let Some(overwrite) = self.schema_overwrite {
Expand Down
10 changes: 10 additions & 0 deletions py-polars/tests/unit/io/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,13 @@ def test_compressed_json() -> None:
uncompressed = io.BytesIO(json_bytes)
out = pl.read_json(uncompressed)
assert_frame_equal(out, expected)


def test_empty_list_json() -> None:
df = pl.read_json(io.StringIO("[]"))
assert df.shape == (0, 0)
assert isinstance(df, pl.DataFrame)

df = pl.read_json(b"[]")
assert df.shape == (0, 0)
assert isinstance(df, pl.DataFrame)

0 comments on commit 7092f3b

Please sign in to comment.