Skip to main content
PYSPARK โ€ข LESSON 62

Extracting Calendar Month with month()

How do we extract the month number (1-12) from a date column for monthly partitioning?

Intermediateโฑ 2 Minutesโญ 350 XP
๐Ÿค” THE QUESTION

How do we extract the month number (1-12) from a date column for monthly partitioning?

๐Ÿ’ก WHAT IS IT?

month() extracts the month integer (1 to 12) from a date or timestamp Column.

๐ŸŽฏ WHAT IS IT USED FOR?

Monthly financial reporting, cohort retention tracking, and year-month hierarchical partitions.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, month

df = df.withColumn("order_month", month(col("order_date")))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Extracting Calendar Month with month().

  • โœ“ Import month function
  • โœ“ Extract month integer (1-12)
  • โœ“ Create monthly cohort partition key