Skip to content

Commit

Permalink
fix(rust, python): fix ndjson empty array parsing (#6785)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Feb 10, 2023
1 parent 456d704 commit 729dece
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions polars/polars-io/src/ndjson_core/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,13 @@ fn value_to_dtype(val: &Value) -> DataType {
Value::Static(StaticNode::F64(_)) => DataType::Float64,
Value::Static(StaticNode::Null) => DataType::Null,
Value::Array(arr) => {
let dtype = value_to_dtype(&arr[0]);
let inner_type = if let Some(val) = arr.first() {
value_to_dtype(val)
} else {
DataType::Unknown
};

DataType::List(Box::new(dtype))
DataType::List(Box::new(inner_type))
}
#[cfg(feature = "dtype-struct")]
Value::Object(doc) => {
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/io/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,9 @@ def test_write_ndjson_with_trailing_newline() -> None:

expected = pl.DataFrame({"Column1": ["Value1"]})
assert_frame_equal(df, expected)


def test_read_ndjson_empty_array() -> None:
assert pl.read_ndjson(io.StringIO("""{"foo": {"bar": []}}""")).to_dict(False) == {
"foo": [{"": None}]
}

0 comments on commit 729dece

Please sign in to comment.