pyspark.sql.functions.trunc#

pyspark.sql.functions.trunc(date, format)[source]#

Returns date truncated to the unit specified by the format.

Added in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters:
dateColumn or column name

input column of values to truncate. A column that evaluates to a date.

formatliteral string

‘year’, ‘yyyy’, ‘yy’ to truncate by year, or ‘month’, ‘mon’, ‘mm’ to truncate by month Other options are: ‘week’, ‘quarter’. A column that evaluates to a string.

Returns:
Column

truncated date. Returns a column that evaluates to a date.

Examples

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([('1997-02-28',)], ['dt'])
>>> df.select('*', sf.trunc(df.dt, 'year')).show()
+----------+---------------+
|        dt|trunc(dt, year)|
+----------+---------------+
|1997-02-28|     1997-01-01|
+----------+---------------+
>>> df.select('*', sf.trunc('dt', 'mon')).show()
+----------+--------------+
|        dt|trunc(dt, mon)|
+----------+--------------+
|1997-02-28|    1997-02-01|
+----------+--------------+