Skip to content

Commit

Permalink
Run doctests (#1950)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj committed Dec 5, 2021
1 parent 9fece9a commit 334643c
Show file tree
Hide file tree
Showing 12 changed files with 980 additions and 693 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ jobs:
export RUSTFLAGS="-C debuginfo=0"
cd py-polars && rustup override set nightly-2021-12-02 && make build-and-test-with-cov
cargo clippy
- name: Check doc examples
run: |
cd py-polars && make doctest
# test if we can import polars without any requirements
- name: Import polars
run: |
Expand Down
3 changes: 3 additions & 0 deletions py-polars/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ build-and-test: test-install

build-and-test-with-cov: test-install
cd tests && pytest --cov=polars --import-mode=importlib

doctest:
cd tests && python run_doc_examples.py
45 changes: 27 additions & 18 deletions py-polars/polars/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ def from_dict(
>>> df = pl.from_dict(data)
>>> df
shape: (2, 2)
─────┬─────
─────┬─────
│ a ┆ b │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1 ┆ 3 │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 2 ┆ 4 │
╰─────┴─────╯
└─────┴─────┘
"""
return DataFrame._from_dict(data=data, columns=columns)

Expand Down Expand Up @@ -88,8 +89,8 @@ def from_records(
>>> data = [[1, 2, 3], [4, 5, 6]]
>>> df = pl.from_records(data, columns=["a", "b"])
>>> df
shape: (3, 2)
─────┬─────
shape: (3, 2)
─────┬─────
│ a ┆ b │
│ --- ┆ --- │
│ i64 ┆ i64 │
Expand All @@ -99,7 +100,8 @@ def from_records(
│ 2 ┆ 5 │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 3 ┆ 6 │
╰─────┴─────╯
└─────┴─────┘
"""
return DataFrame._from_records(data, columns=columns, orient=orient)

Expand All @@ -124,7 +126,7 @@ def from_dicts(dicts: Sequence[Dict[str, Any]]) -> DataFrame:
>>> df = pl.from_dicts(data)
>>> df
shape: (3, 2)
─────┬─────
─────┬─────
│ a ┆ b │
│ --- ┆ --- │
│ i64 ┆ i64 │
Expand All @@ -134,7 +136,8 @@ def from_dicts(dicts: Sequence[Dict[str, Any]]) -> DataFrame:
│ 2 ┆ 5 │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 3 ┆ 6 │
╰─────┴─────╯
└─────┴─────┘
"""
return DataFrame._from_dicts(dicts)

Expand Down Expand Up @@ -163,11 +166,12 @@ def from_arrow(
--------
Constructing a DataFrame from an Arrow Table:
>>> import pyarrow as pa
>>> data = pa.table({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> df = pl.from_arrow(data)
>>> df
shape: (3, 2)
─────┬─────
─────┬─────
│ a ┆ b │
│ --- ┆ --- │
│ i64 ┆ i64 │
Expand All @@ -177,20 +181,22 @@ def from_arrow(
│ 2 ┆ 5 │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 3 ┆ 6 │
─────┴─────
─────┴─────
Constructing a Series from an Arrow Array:
>>> import pyarrow as pa
>>> data = pa.array([1, 2, 3])
>>> series = pl.from_arrow(data)
>>> series
shape: (3,)
Series: '' [i64]
[
1
2
3
1
2
3
]
"""
if not _PYARROW_AVAILABLE:
raise ImportError(
Expand Down Expand Up @@ -231,32 +237,35 @@ def from_pandas(
--------
Constructing a DataFrame from a pandas DataFrame:
>>> import pandas as pd
>>> pd_df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"])
>>> df = pl.from_pandas(pd_df)
>>> df
shape: (2, 3)
─────┬─────┬─────
shape: (2, 3)
─────┬─────┬─────
│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1 ┆ 2 ┆ 3 │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 4 ┆ 5 ┆ 6 │
─────┴─────┴─────
─────┴─────┴─────
Constructing a Series from a pandas Series:
>>> import pandas as pd
>>> pd_series = pd.Series([1, 2, 3], name="pd")
>>> df = pl.from_pandas(pd_series)
>>> df
shape: (3,)
Series: 'pd' [i64]
[
1
2
3
1
2
3
]
"""
try:
import pandas as pd
Expand Down

0 comments on commit 334643c

Please sign in to comment.