pyspark.sql.functions.date_part#

pyspark.sql.functions.date_part(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(
...     date_part(lit('YEAR'), 'ts').alias('year'),
...     date_part(lit('month'), 'ts').alias('month'),
...     date_part(lit('WEEK'), 'ts').alias('week'),
...     date_part(lit('D'), 'ts').alias('day'),
...     date_part(lit('M'), 'ts').alias('minute'),
...     date_part(lit('S'), 'ts').alias('second')
... ).collect()
[Row(year=2015, month=4, week=15, day=8, minute=8, second=Decimal('15.000000'))]