Skip to main content

datepart

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

Syntax​

Python
from pyspark.sql import functions as dbf

dbf.datepart(field=<field>, source=<source>)

Parameters​

Parameter

Type

Description

field

pyspark.sql.Column

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

source

pyspark.sql.Column or str

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

Parameter

Type

Description

field

pyspark.sql.Column

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

source

pyspark.sql.Column or str

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

Returns​

pyspark.sql.Column: a part of the date/timestamp or interval source.

Examples​

Python
import datetime
from pyspark.sql import functions as dbf
df = spark.createDataFrame([(datetime.datetime(2015, 4, 8, 13, 8, 15),)], ['ts'])
df.select(
'*',
dbf.datepart(dbf.lit('YEAR'), 'ts').alias('year'),
dbf.datepart(dbf.lit('month'), 'ts').alias('month'),
dbf.datepart(dbf.lit('WEEK'), 'ts').alias('week'),
dbf.datepart(dbf.lit('D'), df.ts).alias('day'),
dbf.datepart(dbf.lit('M'), df.ts).alias('minute'),
dbf.datepart(dbf.lit('S'), df.ts).alias('second')
).show()