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
- 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'))]