pyspark.sql.functions.trunc#

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

Returns date truncated to the unit specified by the format.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
dateColumn or str

input column of values to truncate.

formatstr

‘year’, ‘yyyy’, ‘yy’ to truncate by year, or ‘month’, ‘mon’, ‘mm’ to truncate by month Other options are: ‘week’, ‘quarter’

Returns
Column

truncated date.

Examples

>>> df = spark.createDataFrame([('1997-02-28',)], ['d'])
>>> df.select(trunc(df.d, 'year').alias('year')).collect()
[Row(year=datetime.date(1997, 1, 1))]
>>> df.select(trunc(df.d, 'mon').alias('month')).collect()
[Row(month=datetime.date(1997, 2, 1))]