From 457127bf8cd1171305119a152cf217a6b0e0f8a9 Mon Sep 17 00:00:00 2001 From: Ned Western Date: Wed, 14 Feb 2024 20:10:11 +1000 Subject: [PATCH 1/2] docs(python): Add docstring examples for reading json --- py-polars/polars/io/json.py | 30 ++++++++++++++++++++++++++++++ py-polars/polars/io/ndjson.py | 16 ++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/py-polars/polars/io/json.py b/py-polars/polars/io/json.py index 3f083ed6bfa1..adb0001728fb 100644 --- a/py-polars/polars/io/json.py +++ b/py-polars/polars/io/json.py @@ -48,6 +48,36 @@ def read_json( See Also -------- read_ndjson + + Examples + -------- + >>> from io import StringIO + >>> json_str = '[{"foo":1,"bar":6},{"foo":2,"bar":7},{"foo":3,"bar":8}]' + >>> pl.read_json(StringIO(json_str)) + shape: (3, 2) + ┌─────┬─────┐ + │ foo ┆ bar │ + │ --- ┆ --- │ + │ i64 ┆ i64 │ + ╞═════╪═════╡ + │ 1 ┆ 6 │ + │ 2 ┆ 7 │ + │ 3 ┆ 8 │ + └─────┴─────┘ + + With the schema defined. + + >>> pl.read_json(StringIO(json_str), schema={"foo": pl.Int64, "bar": pl.Float64}) + shape: (3, 2) + ┌─────┬─────┐ + │ foo ┆ bar │ + │ --- ┆ --- │ + │ i64 ┆ f64 │ + ╞═════╪═════╡ + │ 1 ┆ 6.0 │ + │ 2 ┆ 7.0 │ + │ 3 ┆ 8.0 │ + └─────┴─────┘ """ return pl.DataFrame._read_json( source, diff --git a/py-polars/polars/io/ndjson.py b/py-polars/polars/io/ndjson.py index 4f5dc87e61fb..7d891e4250f1 100644 --- a/py-polars/polars/io/ndjson.py +++ b/py-polars/polars/io/ndjson.py @@ -45,6 +45,22 @@ def read_ndjson( any dtypes inferred from the schema param will be overridden. ignore_errors Return `Null` if parsing fails because of schema mismatches. + + Examples + -------- + >>> from io import StringIO + >>> json_str = '{"foo":1,"bar":6}\n{"foo":2,"bar":7}\n{"foo":3,"bar":8}\n' + >>> pl.read_ndjson(StringIO(json_str)) + shape: (3, 2) + ┌─────┬─────┐ + │ foo ┆ bar │ + │ --- ┆ --- │ + │ i64 ┆ i64 │ + ╞═════╪═════╡ + │ 1 ┆ 6 │ + │ 2 ┆ 7 │ + │ 3 ┆ 8 │ + └─────┴─────┘ """ return pl.DataFrame._read_ndjson( source, From 087b745f25a84ef1d4e678af64600d3232b046ae Mon Sep 17 00:00:00 2001 From: Ned Western Date: Wed, 14 Feb 2024 20:20:02 +1000 Subject: [PATCH 2/2] Raw string for backslash in docstring --- py-polars/polars/io/ndjson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py-polars/polars/io/ndjson.py b/py-polars/polars/io/ndjson.py index 7d891e4250f1..43cad7e90bc9 100644 --- a/py-polars/polars/io/ndjson.py +++ b/py-polars/polars/io/ndjson.py @@ -21,7 +21,7 @@ def read_ndjson( schema_overrides: SchemaDefinition | None = None, ignore_errors: bool = False, ) -> DataFrame: - """ + r""" Read into a DataFrame from a newline delimited JSON file. Parameters