
dayofweek()
The dayofweek()
function is used to extract the day of the week from a date or timestamp column. This function returns an integer value representing the day, where Sunday is 1, Monday is 2, and so forth, up to Saturday, which is 7.
Other similar functions
weekofyear()
: it returns an integer value representing the week of the year.dayofyear()
: it returns an integer value representing the day of the year.year()
: it returns an integer value representing the yearquarter()
: it returns an integer value representing the quarter of the year.month()
: it returns an integer value representing the month of the year.minute()
: it returns an integer value representing the minute of a datetime.second()
: it returns an integer value representing the second of a datetime.
Create Spark Session and sample DataFrame
from pyspark.sql import SparkSessionfrom pyspark.sql.functions import dayofweek, to_date
# Initialize Spark Sessionspark = SparkSession.builder.appName("dayofweekExample").getOrCreate()
# Sample DataFrame with Date Stringsdata = [("2023-07-01",), ("2023-08-24",), ("2023-10-11",)]columns = ["Date"]df = spark.createDataFrame(data, columns)df.show()
Output:
+----------+
| Date|
+----------+
|2023-07-01|
|2023-08-24|
|2023-10-11|
+----------+
Example: Use dayofweek
to get day of a week
to_date("Date")
: it converts the Date column from string type to date type.dayofweek("Date")
: it returns the day of a week for the Date Column.
# Convert String to Date Typedf = df.withColumn("Date", to_date("Date"))
# Extracting Day of Weekday_of_week_df = df.withColumn("Day of Week", dayofweek("Date"))day_of_week_df.show()
Output:
+----------+-----------+
| Date|Day of Week|
+----------+-----------+
|2023-07-01| 7|
|2023-08-24| 5|
|2023-10-11| 4|
+----------+-----------+
# Stop the Spark Sessionspark.stop()