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
- formatliteral string
‘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’
- timestamp
Column
or column name input column of values to truncate.
- Returns
Column
truncated timestamp.
See also
Examples
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([('1997-02-28 05:02:11',)], ['ts']) >>> df.select('*', sf.date_trunc('year', df.ts)).show() +-------------------+--------------------+ | ts|date_trunc(year, ts)| +-------------------+--------------------+ |1997-02-28 05:02:11| 1997-01-01 00:00:00| +-------------------+--------------------+
>>> df.select('*', sf.date_trunc('mon', 'ts')).show() +-------------------+-------------------+ | ts|date_trunc(mon, ts)| +-------------------+-------------------+ |1997-02-28 05:02:11|1997-02-01 00:00:00| +-------------------+-------------------+