I have a Polars dataframe pydf
in Python with a datetime column time1
that I’m passing to Rust.
In Rust I am calling code that works with epoch format, but afterwards it should return another dataframe with a datetime column time2_epoch
in the same time unit and time zone as time1
.
Currently I perform the conversion to and from epoch on the Python side.
from datetime import datetime
from zoneinfo import ZoneInfo
import polars as pl
pydf = pl.DataFrame({'time1': datetime(2024, 1, 1, tzinfo=ZoneInfo("UTC"))})
time_zone = pydf.schema['time1'].time_zone
time_unit = pydf.schema['time1'].time_unit
pydf_epoch = pydf.with_columns(pl.col('time1').dt.epoch(time_unit))
On the Rust side I have this function accepting the Polars dataframe and the time unit
fn foo(pydf: PyDataFrame, time_unit: &str) -> PyResult<PyDataFrame> {
// Work on pydf and return a dataframe with column time2_epoch whose precision is time_unit
}
Back on the Python side I convert time2_epoch
to a datetime column
rustdf.with_columns(
pl.from_epoch(pl.col('time2_epoch'), time_unit).alias('time2')
).with_columns(
pl.col('time2').dt.convert_time_zone(time_zone)
)
Is it possible to pass pydf
directly to foo
and perform the time conversions in Rust?
I can get the type of the time1
column (an enum AFAIK), but I don’t know how to extract the time zone and time unit from this
let schema = pydf.schema();
let time1_type = schema.get("time1").unwrap();