27 lines
696 B
Python
27 lines
696 B
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
import polars as pl
|
|
|
|
|
|
def filter_time_range(
|
|
df: pl.DataFrame, column: str, start: datetime, end: datetime
|
|
) -> pl.DataFrame:
|
|
return df.filter(
|
|
(pl.col(column) >= start) & (pl.col(column) <= end)
|
|
)
|
|
|
|
|
|
def resample(df: pl.DataFrame, time_column: str, interval: str) -> pl.DataFrame:
|
|
numeric_cols = [
|
|
c for c in df.columns if c != time_column and df[c].dtype.is_numeric()
|
|
]
|
|
return df.sort(time_column).group_by_dynamic(time_column, every=interval).agg(
|
|
[pl.col(c).mean().alias(c) for c in numeric_cols]
|
|
)
|
|
|
|
|
|
def to_records(df: pl.DataFrame) -> list[dict]:
|
|
return df.to_dicts()
|