pyspark.sql.functions.time_trunc#

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

Returns time truncated to the unit.

New 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.

timeColumn or column name

A time to truncate.

Returns
Column

A time truncated to the specified unit.

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|
+----+--------+----------------------+