
Just loading dates isn’t enough. For real analysis, you need to “engineer features” from them, like “What day of the week do most sales happen?” or “How do sales compare by month?” With the Polars dt namespace, extracting these kinds of features from dates becomes much more streamlined.
In Polars, once you have a datetime column, you can access all these features (and more) through the .dt namespace.
Step 1: Create the Data
First, let’s create a DataFrame and make sure our date column is the right type.
import polars as pl
df = pl.DataFrame({
"date_str": ["2025-01-01", "2025-01-02", "2025-01-03"],
"sales": [100, 150, 200]
})
# Convert the string column to a real datetime
df = df.with_columns(
pl.col("date_str").str.to_datetime().alias("date_obj")
)
print(df)Output:
shape: (3, 3) ┌────────────┬───────┬─────────────────────┐ │ date_str ┆ sales ┆ date_obj │ │ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ datetime[μs] │ ╞════════════╪═══════╪═════════════════════╡ │ 2025-01-01 ┆ 100 ┆ 2025-01-01 00:00:00 │ │ 2025-01-02 ┆ 150 ┆ 2025-01-02 00:00:00 │ │ 2025-01-03 ┆ 200 ┆ 2025-01-03 00:00:00 │ └────────────┴────────┴─────────────────────┘
Step 2: Use the .dt Namespace
Now, let’s use with_columns to extract new features. The syntax is pl.col("my_date_col").dt.method().
df_features = df.with_columns([
pl.col("date_obj").dt.year().alias("year"),
pl.col("date_obj").dt.month().alias("month"),
pl.col("date_obj").dt.day().alias("day"),
pl.col("date_obj").dt.weekday().alias("weekday"), # 1=Mon, 7=Sun
pl.col("date_obj").dt.strftime("%Y-%m").alias("year_month") # Custom format
])
print(df_features)Output:
shape: (3, 8) ┌────────────┬───────┬─────────────────────┬──────┬───────┬─────┬─────────┬────────────┐ │ date_str ┆ sales ┆ date_obj ┆ year ┆ month ┆ day ┆ weekday ┆ year_month │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ datetime[μs] ┆ i32 ┆ u32 ┆ u32 ┆ u32 ┆ str │ ╞════════════╪═══════╪═════════════════════╪══════╪═══════╪═════╪═════════╪════════════╡ │ 2025-01-01 ┆ 100 ┆ 2025-01-01 00:00:00 ┆ 2025 ┆ 1 ┆ 1 ┆ 3 ┆ 2025-01 │ │ 2025-01-02 ┆ 150 ┆ 2025-01-02 00:00:00 ┆ 2025 ┆ 1 ┆ 2 ┆ 4 ┆ 2025-01 │ │ 2025-01-03 ┆ 200 ┆ 2025-01-03 00:00:00 ┆ 2025 ┆ 1 ┆ 3 ┆ 5 ┆ 2025-01 │ └────────────┴───────┴─────────────────────┴──────┴───────┴─────┴─────────┴────────────┘
This is the fastest way to prepare time-series data for analysis or machine learning.
Key Takeaways
- Loading dates alone isn’t sufficient; you need to engineer features for proper analysis.
- In Polars, use the .dt namespace to access various datetime features easily.
- Create a DataFrame and ensure your date column is correctly formatted before using the .dt namespace.
- Use pl.col(‘my_date_col’).dt.method() to extract new features efficiently.
- This approach streamlines preparing time-series data for analysis or machine learning.





