Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Pola rs Polars Str to Date Datetime

From Leeroopedia


Knowledge Sources
Domains Data Engineering, Time Series
Last Updated 2026-02-09 10:00 GMT

Overview

Concrete APIs for parsing string columns into Polars temporal types (Date, Datetime) and extracting date components using temporal accessors.

Description

Polars provides multiple entry points for temporal parsing. At the CSV reader level, pl.read_csv(source, try_parse_dates=True) enables automatic format inference for all string columns. At the expression level, Expr.str.to_date(format) and Expr.str.to_datetime(format) parse individual columns using explicit format specifiers. Once parsed, the dt accessor namespace exposes component extraction methods such as dt.year() and dt.month().

Usage

Use these APIs whenever you need to:

  • Read CSV files with automatic date detection enabled.
  • Convert string columns to Date or Datetime types using known format patterns.
  • Extract year, month, day, or other temporal components for downstream analysis.

Code Reference

Source Location

  • Repository: Polars
  • File: docs/source/src/python/user-guide/transformations/time-series/parsing.py (lines 1-43)

Signature

# CSV reader with auto date parsing
pl.read_csv(
    source: str | Path | IO[bytes],
    try_parse_dates: bool = False,
    ...
) -> DataFrame

# String-to-Date parsing
Expr.str.to_date(
    format: str | None = None,
    strict: bool = True,
    exact: bool = True,
    cache: bool = True,
) -> Expr

# String-to-Datetime parsing
Expr.str.to_datetime(
    format: str | None = None,
    time_unit: str = "us",
    time_zone: str | None = None,
    strict: bool = True,
    exact: bool = True,
    cache: bool = True,
) -> Expr

# Temporal component accessors
Expr.dt.year() -> Expr
Expr.dt.month() -> Expr
Expr.dt.day() -> Expr
Expr.dt.hour() -> Expr
Expr.dt.minute() -> Expr
Expr.dt.second() -> Expr

Import

import polars as pl

I/O Contract

Inputs

Name Type Required Description
source Path | IO[bytes] Yes File path or buffer for CSV reading
try_parse_dates bool No If True, attempt to auto-detect and parse date columns during CSV read (default: False)
format None No strftime format string (e.g., "%Y-%m-%d", "%Y-%m-%dT%H:%M:%S%z"); if None, Polars infers the format
strict bool No If True, raise on parse failure; if False, return null (default: True)

Outputs

Name Type Description
DataFrame pl.DataFrame DataFrame with string columns converted to temporal types (from read_csv)
Expr pl.Expr Expression yielding Date, Datetime, or Int32/UInt32 columns (from str.to_date, str.to_datetime, or dt accessors)

Usage Examples

Auto-Detect Dates During CSV Read

import polars as pl

# Auto-detect dates during CSV read
df = pl.read_csv("data.csv", try_parse_dates=True)
print(df.dtypes)  # Date/Datetime columns are automatically detected

Explicit Date Parsing

import polars as pl

# Explicit date parsing with format specifier
df = df.with_columns(
    pl.col("date_str").str.to_date("%Y-%m-%d")
)

Explicit Datetime Parsing

import polars as pl

# Explicit datetime parsing with timezone
df = df.with_columns(
    pl.col("ts_str").str.to_datetime("%Y-%m-%dT%H:%M:%S%z")
)

Extract Temporal Components

import polars as pl

# Extract year and month components
df = df.with_columns(
    pl.col("date").dt.year().alias("year"),
    pl.col("date").dt.month().alias("month"),
)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment