pyspark.sql.functions.time_trunc#

pyspark.sql.functions.time_trunc(unit, time)[source]#

Returns time truncated to the unit.

Added in version 4.1.0.

Parameters:
unitColumn or column name

The unit to truncate the time to. Supported units are: “HOUR”, “MINUTE”, “SECOND”, “MILLISECOND”, and “MICROSECOND”. The unit is case-insensitive. A column that evaluates to a string.

timeColumn or column name

A time to truncate. A column that evaluates to a time.

Returns:
Column

A time truncated to the specified unit. Returns a column that evaluates to a time.

Examples

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame(
...     [("HOUR", "13:08:15")],
...     ['unit', 'time']).withColumn("time", sf.col("time").cast("time"))
>>> df.select('*', sf.time_trunc('unit', 'time')).show()
+----+--------+----------------------+
|unit|    time|time_trunc(unit, time)|
+----+--------+----------------------+
|HOUR|13:08:15|              13:00:00|
+----+--------+----------------------+