Skip to content

Commit 7254133

Browse files
committed
fix: show results in the nine silent top-level examples
Nine examples (`export.py`, `import.py`, `python-udaf.py`, `python-udf.py`, `query-pyarrow-data.py`, `sql-to-pandas.py`, `sql-using-python-udaf.py`, `sql-using-python-udf.py`, `substrait.py`) ended in bare asserts and printed nothing, so a reader running them sees no output and cannot tell a working script from a silent one (#1728). Each now shows its final result with `df.show()` or a terminal `print`, keeping the asserts.
1 parent 7022baa commit 7254133

9 files changed

Lines changed: 18 additions & 0 deletions

examples/export.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@
5050
# export to Python dictionary of columns
5151
pydict = df.to_pydict()
5252
assert pydict == {"a": [1, 2, 3], "b": [4, 5, 6]}
53+
54+
print(f"Exported as list of rows: {pylist}")
55+
print(f"Exported as dict of columns: {pydict}")

examples/import.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@
5555
arrow_table = pa.Table.from_pydict({"a": [1, 2, 3], "b": [4, 5, 6]})
5656
df = ctx.from_arrow(arrow_table)
5757
assert type(df) is datafusion.DataFrame
58+
59+
# Display the last converted DataFrame
60+
df.show()

examples/python-udaf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ def evaluate(self) -> pa.Scalar:
6767
result = df.collect()[0]
6868

6969
assert result.column(0) == pa.array([6.0])
70+
71+
print(f"Sum of column 'a': {result.column(0)[0].as_py()}")

examples/python-udf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def is_null(array: pa.Array) -> pa.Array:
3838

3939
df = df.select(is_null_arr(f.col("a")))
4040

41+
df.show()
42+
4143
result = df.collect()[0]
4244

4345
assert result.column(0) == pa.array([False] * 3)

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@
4040
kind="bar", title="Trip Count by Number of Passengers"
4141
).get_figure()
4242
fig.savefig("chart.png")
43+
44+
print("Saved chart to chart.png")

examples/sql-using-python-udaf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def evaluate(self) -> pa.Scalar:
7575
result_df = ctx.sql(
7676
"select a, my_accumulator(b) as b_aggregated from t group by a order by a"
7777
)
78+
result_df.show()
7879
# Dataframe:
7980
# +---+--------------+
8081
# | a | b_aggregated |

examples/sql-using-python-udf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def is_null(array: pa.Array) -> pa.Array:
5353

5454
# Query the DataFrame using SQL
5555
result_df = ctx.sql("select a, is_null(b) as b_is_null from t")
56+
result_df.show()
5657
# Dataframe:
5758
# +---+-----------+
5859
# | a | b_is_null |

examples/substrait.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@
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+
print(f"Substrait plan round-trip complete: {len(substrait_bytes)} encoded bytes")

0 commit comments

Comments
 (0)