Skip to content

Commit d63c1a5

Browse files
committed
fix(examples): make csv-read-options self-contained, add output to silent examples (#1728)
1 parent 878e5d3 commit d63c1a5

10 files changed

Lines changed: 40 additions & 6 deletions

‎examples/csv-read-options.py‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,30 @@
1717

1818
"""Example demonstrating CsvReadOptions usage."""
1919

20+
import gzip
21+
import shutil
22+
import tempfile
23+
from pathlib import Path
24+
2025
from datafusion import CsvReadOptions, SessionContext
2126

27+
# This example is self-contained: it writes its own small CSV file, plus a
28+
# gzipped copy, into a temporary directory instead of expecting a
29+
# ``data.csv`` file that is not part of this repository.
30+
tmp_dir = Path(tempfile.mkdtemp())
31+
csv_path = tmp_dir / "data.csv"
32+
csv_path.write_text("a,b,c\n1,2,3\n4,5,6\n7,8,9\n")
33+
gz_path = tmp_dir / "data.csv.gz"
34+
with csv_path.open("rb") as f_in, gzip.open(gz_path, "wb") as f_out:
35+
shutil.copyfileobj(f_in, f_out)
36+
2237
# Create a SessionContext
2338
ctx = SessionContext()
2439

2540
# Example 1: Using CsvReadOptions with default values
2641
print("Example 1: Default CsvReadOptions")
2742
options = CsvReadOptions()
28-
df = ctx.read_csv("data.csv", options=options)
43+
df = ctx.read_csv(str(csv_path), options=options)
2944

3045
# Example 2: Using CsvReadOptions with custom parameters
3146
print("\nExample 2: Custom CsvReadOptions")
@@ -36,7 +51,7 @@
3651
schema_infer_max_records=1000,
3752
file_extension=".csv",
3853
)
39-
df = ctx.read_csv("data.csv", options=options)
54+
df = ctx.read_csv(str(csv_path), options=options)
4055

4156
# Example 3: Using the builder pattern (recommended for readability)
4257
print("\nExample 3: Builder pattern")
@@ -49,7 +64,7 @@
4964
.with_truncated_rows(False) # noqa: FBT003
5065
.with_newlines_in_values(True) # noqa: FBT003
5166
)
52-
df = ctx.read_csv("data.csv", options=options)
67+
df = ctx.read_csv(str(csv_path), options=options)
5368

5469
# Example 4: Advanced options
5570
print("\nExample 4: Advanced options")
@@ -64,18 +79,18 @@
6479
.with_file_compression_type("gzip") # Read gzipped CSV
6580
.with_file_extension(".gz")
6681
)
67-
df = ctx.read_csv("data.csv.gz", options=options)
82+
df = ctx.read_csv(str(gz_path), options=options)
6883

6984
# Example 5: Register CSV table with options
7085
print("\nExample 5: Register CSV table")
7186
options = CsvReadOptions().with_has_header(True).with_delimiter(",") # noqa: FBT003
72-
ctx.register_csv("my_table", "data.csv", options=options)
87+
ctx.register_csv("my_table", str(csv_path), options=options)
7388
df = ctx.sql("SELECT * FROM my_table")
7489

7590
# Example 6: Backward compatibility (without options)
7691
print("\nExample 6: Backward compatibility")
7792
# Still works the old way!
78-
df = ctx.read_csv("data.csv", has_header=True, delimiter=",")
93+
df = ctx.read_csv(str(csv_path), has_header=True, delimiter=",")
7994

8095
print("\nAll examples completed!")
8196
print("\nFor all available options, see the CsvReadOptions documentation:")

‎examples/export.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,24 @@
3434
# export to pandas dataframe
3535
pandas_df = df.to_pandas()
3636
assert pandas_df.shape == (3, 2)
37+
print(pandas_df)
3738

3839
# export to PyArrow table
3940
arrow_table = df.to_arrow_table()
4041
assert arrow_table.shape == (3, 2)
42+
print(arrow_table)
4143

4244
# export to Polars dataframe
4345
polars_df = df.to_polars()
4446
assert polars_df.shape == (3, 2)
47+
print(polars_df)
4548

4649
# export to Python list of rows
4750
pylist = df.to_pylist()
4851
assert pylist == [{"a": 1, "b": 4}, {"a": 2, "b": 5}, {"a": 3, "b": 6}]
52+
print(pylist)
4953

5054
# export to Python dictionary of columns
5155
pydict = df.to_pydict()
5256
assert pydict == {"a": [1, 2, 3], "b": [4, 5, 6]}
57+
print(pydict)

‎examples/import.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,22 @@
4040
# Create a datafusion DataFrame from a Python list of rows
4141
df = ctx.from_pylist([{"a": 1, "b": 4}, {"a": 2, "b": 5}, {"a": 3, "b": 6}])
4242
assert type(df) is datafusion.DataFrame
43+
df.show()
4344

4445
# Convert pandas DataFrame to datafusion DataFrame
4546
pandas_df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
4647
df = ctx.from_pandas(pandas_df)
4748
assert type(df) is datafusion.DataFrame
49+
df.show()
4850

4951
# Convert polars DataFrame to datafusion DataFrame
5052
polars_df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
5153
df = ctx.from_polars(polars_df)
5254
assert type(df) is datafusion.DataFrame
55+
df.show()
5356

5457
# Convert Arrow Table to datafusion DataFrame
5558
arrow_table = pa.Table.from_pydict({"a": [1, 2, 3], "b": [4, 5, 6]})
5659
df = ctx.from_arrow(arrow_table)
5760
assert type(df) is datafusion.DataFrame
61+
df.show()

‎examples/python-udaf.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def evaluate(self) -> pa.Scalar:
6363
)
6464

6565
df = df.aggregate([], [my_udaf(col("a"))])
66+
df.show()
6667

6768
result = df.collect()[0]
6869

‎examples/python-udf.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def is_null(array: pa.Array) -> pa.Array:
3737
df = ctx.create_dataframe([[batch]])
3838

3939
df = df.select(is_null_arr(f.col("a")))
40+
df.show()
4041

4142
result = df.collect()[0]
4243

‎examples/query-pyarrow-data.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
col("a") - col("b"),
3636
)
3737

38+
df.show()
39+
3840
# execute and collect the first (and only) batch
3941
result = df.collect()[0]
4042

‎examples/sql-to-pandas.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
# convert to Pandas
3636
pandas_df = df.to_pandas()
37+
print(pandas_df)
3738

3839
# create a chart
3940
fig = pandas_df.plot(

‎examples/sql-using-python-udaf.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,6 @@ def evaluate(self) -> pa.Scalar:
8282
# | 1 | 9 |
8383
# | 3 | 6 |
8484
# +---+--------------+
85+
result_df.show()
8586
assert result_df.to_pydict()["a"] == [1, 3]
8687
assert result_df.to_pydict()["b_aggregated"] == [9, 6]

‎examples/sql-using-python-udf.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ def is_null(array: pa.Array) -> pa.Array:
6161
# | 2 | true |
6262
# | 3 | false |
6363
# +---+-----------+
64+
result_df.show()
6465
assert result_df.to_pydict()["b_is_null"] == [False, True, False]

‎examples/substrait.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@
4747
# Back to Substrait Plan just for demonstration purposes
4848
# type(substrait_plan) -> <class 'datafusion.substrait.plan'>
4949
substrait_plan = ss.Producer.to_substrait_plan(df_logical_plan, ctx)
50+
51+
# Show the logical plan recovered from the Substrait round trip
52+
print(df_logical_plan)

0 commit comments

Comments
 (0)