
Today we’re covering two powerful Polars Expressions: shift and rank. These are essential for financial analysis, ranking, and finding trends. In this article you’ll learn more about Polars shift rank and how these functions work together in your data workflows.
Let’s look at each functions in Polar Shift Rank individually and how they work together.
1. shift() (Lagging Data)
The shift() function “shifts” a column down by n rows. This is incredibly useful for calculating percent change or comparing a value to the previous value.
import polars as pl
df = pl.DataFrame({"sales": [10, 20, 15, 25]})
# Let's create a new column 'sales_previous_day'
df.with_columns(
pl.col("sales").shift(1).alias("sales_previous_day")
)Output:
shape: (4, 2) ┌───────┬────────────────────┐ │ sales ┆ sales_previous_day │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═══════╪════════════════════╡ │ 10 ┆ null │ │ 20 ┆ 10 │ │ 15 ┆ 20 │ │ 25 ┆ 15 │ └───────┴────────────────────┘
The first row is null because there is no previous day. Now you can easily calculate day-over-day change!
2. rank() (Finding Top Performers)
The rank() function gives a rank to each item, with 1 being the highest.
df.with_columns(
pl.col("sales").rank(descending=True).alias("sales_rank")
)Output:
shape: (4, 2) ┌───────┬────────────┐ │ sales ┆ sales_rank │ │ --- ┆ --- │ │ i64 ┆ u32 │ ╞═══════╪════════════╡ │ 10 ┆ 4 │ │ 20 ┆ 2 │ │ 15 ┆ 3 │ │ 25 ┆ 1 │ └───────┴────────────┘
Combining with .over() (The Pro Move)
The real power is combining rank() with the .over() window function. Let’s rank employees within their department.
df = pl.DataFrame({
"dept": ["Sales", "Sales", "Eng", "Eng"],
"salary": [100, 150, 200, 180]
})
df.with_columns(
pl.col("salary").rank(descending=True).over("dept").alias("rank_in_dept")
)Output:
shape: (4, 3) ┌───────┬────────┬──────────────┐ │ dept ┆ salary ┆ rank_in_dept │ │ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ u32 │ ╞═══════╪════════╪══════════════╡ │ Sales ┆ 100 ┆ 2 │ │ Sales ┆ 150 ┆ 1 │ │ Eng ┆ 200 ┆ 1 │ │ Eng ┆ 180 ┆ 2 │ └───────┴────────┴──────────────┘
Key Takeaways
- This article explores two key Polars Expressions: shift and rank, crucial for financial analysis and identifying trends.
- The shift() function shifts data down by n rows, enabling calculations like percent change and comparisons with previous values.
- The rank() function assigns ranks to items, with 1 being the highest, allowing for easy identification of top performers.
- Combining rank() with the .over() function enhances its power, enabling rankings within specific groups like departments.





