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
- date
Column
or column name input column of values to truncate.
- formatliteral string
‘year’, ‘yyyy’, ‘yy’ to truncate by year, or ‘month’, ‘mon’, ‘mm’ to truncate by month Other options are: ‘week’, ‘quarter’
- date
- Returns
Column
truncated date.
See also
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| +----------+--------------+