Skip to content

Commit

Permalink
docs(python): More examples for read_csv
Browse files Browse the repository at this point in the history
Addition to pola-rs#13545 with unnecessary doctest skips removed.
Part of pola-rs#13161.
  • Loading branch information
MarcNuebel authored Jan 9, 2024
1 parent 130c48f commit e110863
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions py-polars/polars/io/csv/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,16 @@ def read_csv(
Reproducible example using BytesIO object, parsing dates.
>>> import io # doctest: +SKIP
>>> import io
>>> source = io.BytesIO(
... (
... "ID,Name,Birthday\n"
... "1,Alice,1995-07-12\n"
... "2,Bob,1990-09-20\n"
... "3,Charlie,2002-03-08"
... ).encode()
... ) # doctest: +SKIP
>>> pl.read_csv(source, try_parse_dates=True) # doctest: +SKIP
... )
>>> pl.read_csv(source, try_parse_dates=True)
shape: (3, 3)
┌─────┬─────────┬────────────┐
│ ID ┆ Name ┆ Birthday │
Expand All @@ -216,6 +216,46 @@ def read_csv(
│ 2 ┆ Bob ┆ 1990-09-20 │
│ 3 ┆ Charlie ┆ 2002-03-08 │
└─────┴─────────┴────────────┘
Consider specifying `dtypes` or `schema` to avoid wrong schema inference.
Alternatively, `infer_schema_length` can be adapted.
>>> pl.read_csv("sparse_data.csv") # doctest: +SKIP
shape: (102, 2)
┌────────┬────────────┐
│ row_nr ┆ sparse_int │
│ --- ┆ --- │
│ i64 ┆ str │
╞════════╪════════════╡
│ 0 ┆ null │
│ 1 ┆ null │
│ 2 ┆ null │
│ 3 ┆ null │
│ … ┆ … │
│ 98 ┆ null │
│ 99 ┆ null │
│ 100 ┆ null │
│ 101 ┆ 1 │
└────────┴────────────┘
>>> pl.read_csv(
... "sparse_data.csv", infer_schema_length=None, dtypes={"row_nr": pl.Int8}
... ) # doctest: +SKIP
shape: (102, 2)
┌────────┬────────────┐
│ row_nr ┆ sparse_int │
│ --- ┆ --- │
│ i8 ┆ i64 │
╞════════╪════════════╡
│ 0 ┆ null │
│ 1 ┆ null │
│ 2 ┆ null │
│ 3 ┆ null │
│ … ┆ … │
│ 98 ┆ null │
│ 99 ┆ null │
│ 100 ┆ null │
│ 101 ┆ 1 │
└────────┴────────────┘
"""
_check_arg_is_1byte("separator", separator, can_be_empty=False)
_check_arg_is_1byte("quote_char", quote_char, can_be_empty=True)
Expand Down

0 comments on commit e110863

Please sign in to comment.