Skip to content

Commit

Permalink
Miscellaneous improvements (#4203)
Browse files Browse the repository at this point in the history
Co-authored-by: Matteo Santamaria <msantama@gmail.com>
  • Loading branch information
matteosantama and Matteo Santamaria committed Aug 1, 2022
1 parent 8f686b1 commit 7092c11
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ venv/
coverage.lcov
coverage.xml
.DS_Store
.python-version
5 changes: 4 additions & 1 deletion py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,14 +1164,17 @@ def write_csv(
Examples
--------
>>> import pathlib
>>>
>>> df = pl.DataFrame(
... {
... "foo": [1, 2, 3, 4, 5],
... "bar": [6, 7, 8, 9, 10],
... "ham": ["a", "b", "c", "d", "e"],
... }
... )
>>> df.write_csv("new_file.csv", sep=",")
>>> path: pathlib.Path = dirpath / "new_file.csv"
>>> df.write_csv(path, sep=",")
"""
if len(sep) > 1:
Expand Down
7 changes: 5 additions & 2 deletions py-polars/polars/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ def scan_csv(
Examples
--------
>>> import pathlib
>>>
>>> (
... pl.scan_csv("my_long_file.csv") # lazy, doesn't do a thing
... .select(
Expand All @@ -521,9 +523,10 @@ def scan_csv(
>>> df = pl.DataFrame(
... {"BrEeZaH": [1, 2, 3, 4], "LaNgUaGe": ["is", "terrible", "to", "read"]}
... )
>>> df.to_csv("mydf.csv")
>>> path: pathlib.Path = dirpath / "mydf.csv"
>>> df.to_csv(path)
>>> pl.scan_csv(
... "mydf.csv", with_column_names=lambda cols: [col.lower() for col in cols]
... path, with_column_names=lambda cols: [col.lower() for col in cols]
... ).fetch()
shape: (4, 2)
┌─────────┬──────────┐
Expand Down
6 changes: 2 additions & 4 deletions py-polars/polars/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ def is_categorical_dtype(data_type: Any) -> bool:
UInt32: integers(min_value=0, max_value=(2**32) - 1),
UInt64: integers(min_value=0, max_value=(2**64) - 1),
# TODO: when generating text for categorical, ensure there are repeats -
# don't want all to be unique.
# don't want all to be unique.
Categorical: text(),
Utf8: text(),
# TODO: generate arrow temporal types with different resolution (32/64) to
# validate compatibility.
# validate compatibility.
Time: times(),
Date: dates(),
Duration: timedeltas(),
Expand Down Expand Up @@ -592,7 +592,6 @@ def series(
>>> @given(df=series())
... def test_repr(s: pl.Series) -> None:
... assert isinstance(repr(s), str)
... # print(s)
>>>
>>> s = series(dtype=pl.Int32, max_size=5)
>>> s.example() # doctest: +SKIP
Expand Down Expand Up @@ -748,7 +747,6 @@ def dataframes(
>>> @given(df=dataframes())
... def test_repr(df: pl.DataFrame) -> None:
... assert isinstance(repr(df), str)
... # print(df)
>>>
>>> # generate LazyFrames with at least 1 column, random dtypes, and specific size:
>>> df = dataframes(min_cols=1, lazy=True, max_size=5)
Expand Down
28 changes: 17 additions & 11 deletions py-polars/tests/run_doc_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import sys
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any, Generator

import polars
Expand Down Expand Up @@ -85,14 +86,19 @@ def check_output(self, want: str, got: str, optionflags: Any) -> bool:
# __file__ returns the __init__.py, so grab the parent
src_dir = Path(polars.__file__).parent

# collect all tests
tests = [
doctest.DocTestSuite(m, extraglobs={"pl": polars}, optionflags=1)
for m in modules_in_path(src_dir)
]
test_suite = unittest.TestSuite(tests)

# run doctests and report
result = unittest.TextTestRunner().run(test_suite)
success_flag = (result.testsRun > 0) & (len(result.failures) == 0)
sys.exit(int(not success_flag))
with TemporaryDirectory() as tmpdir:
# collect all tests
tests = [
doctest.DocTestSuite(
m, extraglobs={"pl": polars, "dirpath": Path(tmpdir)}, optionflags=1
)
for m in modules_in_path(src_dir)
]
test_suite = unittest.TestSuite(tests)

# Ensure that we clean up any artifacts produced by the doctests
# with patch(polars.DataFrame.to_csv, polars.DataFrame.write_csv):
# run doctests and report
result = unittest.TextTestRunner().run(test_suite)
success_flag = (result.testsRun > 0) & (len(result.failures) == 0)
sys.exit(int(not success_flag))
4 changes: 1 addition & 3 deletions py-polars/tests_parametric/test_dataframe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ----------------------------------------------------
# Validate DataFrame behaviour with parameteric tests
# Validate DataFrame behaviour with parametric tests
# ----------------------------------------------------
from __future__ import annotations

Expand All @@ -14,7 +14,6 @@
def test_repr(df: pl.DataFrame) -> None:
assert isinstance(repr(df), str)
assert_frame_equal(df, df, check_exact=True)
# print(df)


@given(df=dataframes(min_size=1, min_cols=1, max_size=10, null_probability=0.25))
Expand All @@ -29,7 +28,6 @@ def test_null_count(df: pl.DataFrame) -> None:
assert null_count.shape == (1, ncols)
for idx, count in enumerate(null_count.rows()[0]):
assert count == sum(v is None for v in df.select_at_idx(idx).to_list())
print(null_count.rows())


@given(
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests_parametric/test_series.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -------------------------------------------------
# Validate Series behaviour with parameteric tests
# Validate Series behaviour with parametric tests
# -------------------------------------------------
from __future__ import annotations

Expand Down

0 comments on commit 7092c11

Please sign in to comment.