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?

Intermediateโฑ 2 Minutesโญ 340 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