pyspark.sql.functions.extract

pyspark.sql.functions.extract(field: ColumnOrName, source: ColumnOrName) → pyspark.sql.column.Column[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.

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