Skip to main content
PYSPARK • LESSON 61

Extracting Calendar Year with year()

How do we extract the 4-digit calendar year integer from an order date Column?

Intermediate2 Minutes340 XP
🤔 THE QUESTION

How do we extract the 4-digit calendar year integer from an order date Column?

💡 WHAT IS IT?

year() extracts the calendar year component as an IntegerType from a date or timestamp Column.

🎯 WHAT IS IT USED FOR?

Creating annual partition directories in lakehouses and grouping financial transactions by year.

💻 EXAMPLE
from pyspark.sql.functions import col, year

df = df.withColumn("order_year", year(col("order_date")))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Extracting Calendar Year with year().

  • Import year function
  • Extract integer year value
  • Prepare annual partition key