Partekatu honen bidez:


Clase Window

Funciones de utilidad para definir la ventana en DataFrames.

Admite Spark Connect

Atributos de clase

Atributo Descripción
unboundedPreceding Valor de límite que representa el inicio de un marco de ventana sin enlazar.
unboundedFollowing Valor de límite que representa el final de un marco de ventana sin enlazar.
currentRow Valor de límite que representa la fila actual en un marco de ventana.

Methods

Método Descripción
orderBy(*cols) Crea una WindowSpec con la ordenación definida.
partitionBy(*cols) Crea una WindowSpec con la creación de particiones definida.
rangeBetween(start, end) Crea una WindowSpec con los límites de marco definidos, de start (inclusive) a end (inclusive), mediante desplazamientos basados en intervalos del valor de ORDER BY la fila actual.
rowsBetween(start, end) Crea una WindowSpec con los límites de marco definidos, de start (inclusive) a end (inclusive), mediante desplazamientos basados en filas de la fila actual.

Notas

Cuando no se define el orden, se usa de forma predeterminada un marco de ventana sin enlazar (rowFrame, unboundedPreceding, unboundedFollowing). Cuando se define el orden, se usa de forma predeterminada un marco de ventana creciente (rangeFrame, unboundedPreceding, currentRow).

Ejemplos

Ventana básica con orden y marco de fila

from pyspark.sql import Window

# ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
window = Window.orderBy("date").rowsBetween(Window.unboundedPreceding, Window.currentRow)

Ventana con particiones con marco de intervalo

from pyspark.sql import Window

# PARTITION BY country ORDER BY date RANGE BETWEEN 3 PRECEDING AND 3 FOLLOWING
window = Window.orderBy("date").partitionBy("country").rangeBetween(-3, 3)

Número de fila dentro de la partición

from pyspark.sql import Window, functions as sf

df = spark.createDataFrame(
    [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"]
)

# Show row number ordered by id within each category partition
window = Window.partitionBy("category").orderBy("id")
df.withColumn("row_number", sf.row_number().over(window)).show()

Suma en ejecución con marco basado en filas

from pyspark.sql import Window, functions as sf

df = spark.createDataFrame(
    [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"]
)

# Sum id values from the current row to the next row within each partition
window = Window.partitionBy("category").orderBy("id").rowsBetween(Window.currentRow, 1)
df.withColumn("sum", sf.sum("id").over(window)).sort("id", "category", "sum").show()

Suma en ejecución con marco basado en intervalos

from pyspark.sql import Window, functions as sf

df = spark.createDataFrame(
    [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"]
)

# Sum id values from the current id value to id + 1 within each partition
window = Window.partitionBy("category").orderBy("id").rangeBetween(Window.currentRow, 1)
df.withColumn("sum", sf.sum("id").over(window)).sort("id", "category").show()