Skip to main content
PYSPARK โ€ข LESSON 154

Scalar Pandas UDF with Python Type Hints

How do we define a vectorized scalar Pandas UDF with Python Series type hints to compute sales tax?

Expertโฑ 2 Minutesโญ 820 XP
๐Ÿค” THE QUESTION

How do we define a vectorized scalar Pandas UDF with Python Series type hints to compute sales tax?

๐Ÿ’ก WHAT IS IT?

Decorating a function taking pd.Series -> pd.Series with @pandas_udf('double') for vectorized batch operations.

๐ŸŽฏ WHAT IS IT USED FOR?

Vectorized arithmetic, NumPy mathematical functions, and high-speed financial calculations.

๐Ÿ’ป EXAMPLE
import pandas as pd
from pyspark.sql.functions import pandas_udf

@pandas_udf("double")
def add_sales_tax(amounts: pd.Series) -> pd.Series:
    return amounts * 1.0825

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Scalar Pandas UDF with Python Type Hints.

  • โœ“ Define vectorized scalar Pandas UDF
  • โœ“ Specify Python type hints (pd.Series -> pd.Series)
  • โœ“ Execute vectorized tax computation