pyspark.sql.functions.date_trunc#

pyspark.sql.functions.date_trunc(format, timestamp)[source]#

Returns timestamp truncated to the unit specified by the format.

New in version 2.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
formatstr

‘year’, ‘yyyy’, ‘yy’ to truncate by year, ‘month’, ‘mon’, ‘mm’ to truncate by month, ‘day’, ‘dd’ to truncate by day, Other options are: ‘microsecond’, ‘millisecond’, ‘second’, ‘minute’, ‘hour’, ‘week’, ‘quarter’

timestampColumn or str

input column of values to truncate.

Returns
Column

truncated timestamp.

Examples

>>> df = spark.createDataFrame([('1997-02-28 05:02:11',)], ['t'])
>>> df.select(date_trunc('year', df.t).alias('year')).collect()
[Row(year=datetime.datetime(1997, 1, 1, 0, 0))]
>>> df.select(date_trunc('mon', df.t).alias('month')).collect()
[Row(month=datetime.datetime(1997, 2, 1, 0, 0))]