Skip to content

Commit 29a9825

Browse files
zhengruifenghuangxiaopingRD
authored andcommitted
[MINOR][PYTHON][DOCS] Refine the examples in window.py
### What changes were proposed in this pull request? Refine the examples in window.py ### Why are the changes needed? use the recommended way to import functions ### Does this PR introduce _any_ user-facing change? yes, doc-only changes ### How was this patch tested? CI ### Was this patch authored or co-authored using generative AI tooling? No Closes apache#53112 from zhengruifeng/doc_win_fun_import. Authored-by: Ruifeng Zheng <ruifengz@apache.org> Signed-off-by: Ruifeng Zheng <ruifengz@apache.org>
1 parent 8859e37 commit 29a9825

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

python/pyspark/sql/window.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ def partitionBy(*cols: Union["ColumnOrName", Sequence["ColumnOrName"]]) -> "Wind
8686
8787
Examples
8888
--------
89-
>>> from pyspark.sql import Window
90-
>>> from pyspark.sql.functions import row_number
89+
>>> from pyspark.sql import Window, functions as sf
9190
>>> df = spark.createDataFrame(
9291
... [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"])
9392
>>> df.show()
@@ -105,7 +104,7 @@ def partitionBy(*cols: Union["ColumnOrName", Sequence["ColumnOrName"]]) -> "Wind
105104
Show row number order by ``id`` in partition ``category``.
106105
107106
>>> window = Window.partitionBy("category").orderBy("id")
108-
>>> df.withColumn("row_number", row_number().over(window)).show()
107+
>>> df.withColumn("row_number", sf.row_number().over(window)).show()
109108
+---+--------+----------+
110109
| id|category|row_number|
111110
+---+--------+----------+
@@ -139,8 +138,7 @@ def orderBy(*cols: Union["ColumnOrName", Sequence["ColumnOrName"]]) -> "WindowSp
139138
140139
Examples
141140
--------
142-
>>> from pyspark.sql import Window
143-
>>> from pyspark.sql.functions import row_number
141+
>>> from pyspark.sql import Window, functions as sf
144142
>>> df = spark.createDataFrame(
145143
... [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"])
146144
>>> df.show()
@@ -158,7 +156,7 @@ def orderBy(*cols: Union["ColumnOrName", Sequence["ColumnOrName"]]) -> "WindowSp
158156
Show row number order by ``category`` in partition ``id``.
159157
160158
>>> window = Window.partitionBy("id").orderBy("category")
161-
>>> df.withColumn("row_number", row_number().over(window)).show()
159+
>>> df.withColumn("row_number", sf.row_number().over(window)).show()
162160
+---+--------+----------+
163161
| id|category|row_number|
164162
+---+--------+----------+
@@ -214,8 +212,7 @@ def rowsBetween(start: int, end: int) -> "WindowSpec":
214212
215213
Examples
216214
--------
217-
>>> from pyspark.sql import Window
218-
>>> from pyspark.sql import functions as func
215+
>>> from pyspark.sql import Window, functions as sf
219216
>>> df = spark.createDataFrame(
220217
... [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"])
221218
>>> df.show()
@@ -234,7 +231,7 @@ def rowsBetween(start: int, end: int) -> "WindowSpec":
234231
in partition ``category``
235232
236233
>>> window = Window.partitionBy("category").orderBy("id").rowsBetween(Window.currentRow, 1)
237-
>>> df.withColumn("sum", func.sum("id").over(window)).sort("id", "category", "sum").show()
234+
>>> df.withColumn("sum", sf.sum("id").over(window)).sort("id", "category", "sum").show()
238235
+---+--------+---+
239236
| id|category|sum|
240237
+---+--------+---+
@@ -294,8 +291,7 @@ def rangeBetween(start: int, end: int) -> "WindowSpec":
294291
295292
Examples
296293
--------
297-
>>> from pyspark.sql import Window
298-
>>> from pyspark.sql import functions as func
294+
>>> from pyspark.sql import Window, functions as sf
299295
>>> df = spark.createDataFrame(
300296
... [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"])
301297
>>> df.show()
@@ -314,7 +310,7 @@ def rangeBetween(start: int, end: int) -> "WindowSpec":
314310
in partition ``category``
315311
316312
>>> window = Window.partitionBy("category").orderBy("id").rangeBetween(Window.currentRow, 1)
317-
>>> df.withColumn("sum", func.sum("id").over(window)).sort("id", "category").show()
313+
>>> df.withColumn("sum", sf.sum("id").over(window)).sort("id", "category").show()
318314
+---+--------+---+
319315
| id|category|sum|
320316
+---+--------+---+

0 commit comments

Comments
 (0)