pyspark.sql.functions.datepart#

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

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

Added in version 3.5.0.

Parameters:
fieldColumn

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 column name

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

Returns:
Column

a part of the date/timestamp or interval source. Returns a column whose type depends on the field to extract, e.g. an integer for YEAR and a decimal for SECOND.

Examples

>>> import datetime
>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([(datetime.datetime(2015, 4, 8, 13, 8, 15),)], ['ts'])
>>> df.select(
...     '*',
...     sf.datepart(sf.lit('YEAR'), 'ts').alias('year'),
...     sf.datepart(sf.lit('month'), 'ts').alias('month'),
...     sf.datepart(sf.lit('WEEK'), 'ts').alias('week'),
...     sf.datepart(sf.lit('D'), df.ts).alias('day'),
...     sf.datepart(sf.lit('M'), df.ts).alias('minute'),
...     sf.datepart(sf.lit('S'), df.ts).alias('second')
... ).show()
+-------------------+----+-----+----+---+------+---------+
|                 ts|year|month|week|day|minute|   second|
+-------------------+----+-----+----+---+------+---------+
|2015-04-08 13:08:15|2015|    4|  15|  8|     8|15.000000|
+-------------------+----+-----+----+---+------+---------+