Skip to main content

date_trunc

Returns timestamp truncated to the unit specified by the format.

For the corresponding Databricks SQL function, see date_trunc function.

Syntax

Python
from pyspark.sql import functions as dbf

dbf.date_trunc(format=<format>, timestamp=<timestamp>)

Parameters

Parameter

Type

Description

format

literal 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

pyspark.sql.Column or str

input column of values to truncate.

Parameter

Type

Description

format

literal 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

pyspark.sql.Column or str

input column of values to truncate.

Returns

pyspark.sql.Column: truncated timestamp.

Examples

Python
from pyspark.sql import functions as dbf
df = spark.createDataFrame([('1997-02-28 05:02:11',)], ['ts'])
df.select('*', dbf.date_trunc('year', df.ts)).show()
df.select('*', dbf.date_trunc('mon', 'ts')).show()