Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
Extracts a part of the date/timestamp or interval source.
Syntax
from pyspark.databricks.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. |
Returns
pyspark.sql.Column: a part of the date/timestamp or interval source.
Examples
import datetime
from pyspark.databricks.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()