Blazingly fast DataFrame library for Python with lazy and eager evaluation modes
—
Global configuration system for controlling formatting, streaming behavior, optimization settings, and execution engines with context managers and persistent configuration options.
Global configuration class for controlling Polars behavior across all operations.
class Config:
# Display and Formatting
@classmethod
def set_fmt_str_lengths(cls, n: int) -> type[Config]:
"""Set maximum string length in display."""
@classmethod
def set_fmt_float(cls, fmt: str) -> type[Config]:
"""Set float number formatting."""
@classmethod
def set_tbl_cols(cls, n: int) -> type[Config]:
"""Set maximum columns in table display."""
@classmethod
def set_tbl_rows(cls, n: int) -> type[Config]:
"""Set maximum rows in table display."""
@classmethod
def set_tbl_width_chars(cls, width: int) -> type[Config]:
"""Set table width in characters."""
# Streaming Configuration
@classmethod
def set_streaming_chunk_size(cls, size: int) -> type[Config]:
"""Set streaming chunk size."""
# Global Settings
@classmethod
def set_auto_structify(cls, active: bool) -> type[Config]:
"""Enable/disable automatic struct creation."""
@classmethod
def set_thousands_separator(cls, separator: str) -> type[Config]:
"""Set thousands separator in display."""
@classmethod
def set_decimal_separator(cls, separator: str) -> type[Config]:
"""Set decimal separator in display."""
# Configuration Management
@classmethod
def restore_defaults(cls) -> type[Config]:
"""Restore all settings to defaults."""
@classmethod
def save(cls, file: str) -> None:
"""Save current configuration to file."""
@classmethod
def load(cls, file: str) -> type[Config]:
"""Load configuration from file."""
# Context Manager Support
def __enter__(self):
"""Enter configuration context."""
def __exit__(self, exc_type, exc_val, exc_tb):
"""Exit configuration context."""Configuration for query optimization flags and execution strategies.
class QueryOptFlags:
def __init__(
self,
*,
type_coercion=True,
predicate_pushdown=True,
projection_pushdown=True,
simplify_expression=True,
slice_pushdown=True,
comm_subplan_elim=True,
comm_subexpr_elim=True,
cluster_with_columns=True,
streaming=False
):
"""
Query optimization flags.
Parameters:
- type_coercion: Enable type coercion optimization
- predicate_pushdown: Push predicates down to data sources
- projection_pushdown: Push column projections down
- simplify_expression: Simplify expressions
- slice_pushdown: Push slice operations down
- comm_subplan_elim: Eliminate common subplans
- comm_subexpr_elim: Eliminate common subexpressions
- cluster_with_columns: Cluster with_columns operations
- streaming: Enable streaming execution
"""GPU acceleration configuration for supported operations.
class GPUEngine:
def __init__(
self,
*,
device=None,
memory_resource=None,
raise_on_fail=False
):
"""
GPU execution engine configuration.
Parameters:
- device: GPU device ID
- memory_resource: GPU memory resource
- raise_on_fail: Raise error if GPU execution fails
"""import polars as pl
# Set display options
pl.Config.set_tbl_rows(20)
pl.Config.set_tbl_cols(10)
pl.Config.set_fmt_str_lengths(50)
# Create DataFrame to see formatting
df = pl.DataFrame({
"long_column_name": ["very long string value"] * 100,
"numbers": range(100)
})
print(df) # Will use new display settings# Temporary configuration changes
with pl.Config() as cfg:
cfg.set_tbl_rows(5)
cfg.set_fmt_str_lengths(10)
# DataFrame display uses temporary settings
print(df.head(10))
# Settings restored after context
print(df.head(10)) # Uses default settings# Custom optimization flags
opt_flags = pl.QueryOptFlags(
predicate_pushdown=True,
projection_pushdown=True,
streaming=True
)
# Use with LazyFrame collection
result = (
pl.scan_csv("large_file.csv")
.filter(pl.col("amount") > 1000)
.select(["id", "amount", "date"])
.collect(**opt_flags.__dict__)
)# Save current configuration
pl.Config.save("my_polars_config.json")
# Modify settings
pl.Config.set_tbl_rows(50)
pl.Config.set_streaming_chunk_size(1000000)
# Later, restore saved configuration
pl.Config.load("my_polars_config.json")
# Or restore defaults
pl.Config.restore_defaults()Install with Tessl CLI
npx tessl i tessl/pypi-polars