pyspark.sql.functions.datepart#

pyspark.sql.functions.datepart(field, source)[source]#

Extracts a part of the date/timestamp or interval source.

New in version 3.5.0.

Parameters
fieldColumn or str

selects which part of the source should be extracted, and supported string values are as same as the fields of the equivalent function extract.

sourceColumn or str

a date/timestamp or interval column from where field should be extracted.

Returns
Column

a part of the date/timestamp or interval source.

Examples

>>> import datetime
>>> df = spark.createDataFrame([(datetime.datetime(2015, 4, 8, 13, 8, 15),)], ['ts'])
>>> df.select(
...     datepart(lit('YEAR'), 'ts').alias('year'),
...     datepart(lit('month'), 'ts').alias('month'),
...     datepart(lit('WEEK'), 'ts').alias('week'),
...     datepart(lit('D'), 'ts').alias('day'),
...     datepart(lit('M'), 'ts').alias('minute'),
...     datepart(lit('S'), 'ts').alias('second')
... ).collect()
[Row(year=2015, month=4, week=15, day=8, minute=8, second=Decimal('15.000000'))]