Polars & Pandas
Working with Dates — Pandas vs Polars
Dates and times are everywhere in datasets. Let’s see how Pandas and Polars parse and handle them.
Published
Last updated
By
Jeferson Peter
1 min read
1 min
Datasets almost always include timestamps. Handling them correctly is essential for analysis.
Both Pandas and Polars provide tools to parse and format dates, but their syntax differs.
Example data
import pandas as pd
import polars as pl
data = {"date": ["2025-01-01", "2025-02-15"]}
df_pd = pd.DataFrame(data)
df_pl = pl.DataFrame(data)
Converting to datetime
# Pandas
df_pd["date"] = pd.to_datetime(df_pd["date"])
print(df_pd)
# date
# 0 2025-01-01
# 1 2025-02-15
# Polars
df_pl = df_pl.with_columns(pl.col("date").str.strptime(pl.Date, "%Y-%m-%d"))
print(df_pl)
# shape: (2, 1)
# ┌────────────┐
# │ date │
# │ --- │
# │ date │
# ╞════════════╡
# │ 2025-01-01 │
# │ 2025-02-15 │
# └────────────┘
Extracting parts of the date
# Pandas
print(df_pd["date"].dt.month)
# 0 1
# 1 2
# Name: date, dtype: int32
# Polars
print(df_pl.with_columns(df_pl["date"].dt.month().alias("month")))
# shape: (2, 2)
# ┌────────────┬───────┐
# │ date ┆ month │
# │ --- ┆ --- │
# │ date ┆ u32 │
# ╞════════════╪═══════╡
# │ 2025-01-01 ┆ 1 │
# │ 2025-02-15 ┆ 2 │
# └────────────┴───────┘
Conclusion
- Pandas:
pd.to_datetimeand.dtaccessor for parts of the date. - Polars:
.str.strptime()for parsing,.dtnamespace for components. - Both are powerful — Polars emphasizes performance and explicit parsing.