Skip to content

Commit

Permalink
fix!: POSIXct without tzone -> Polars datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Mar 2, 2024
1 parent 7dab2d9 commit b3f6b4f
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/rust/src/conversion_r_to_s.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::robj_to;
use crate::series::RPolarsSeries;
use crate::utils::collect_hinted_result;
use extendr_api::prelude::*;
Expand All @@ -6,6 +7,8 @@ use extendr_api::prelude::*;
use polars::prelude as pl;
use polars::prelude::IntoSeries;
use polars::prelude::NamedFrom;
use polars_lazy::dsl::col;
use polars_lazy::frame::IntoLazy;
// Internal tree structure to contain Series of fully parsed nested Robject.
// It is easier to resolve concatenated datatype after all elements have been parsed
// because empty lists have no type in R, but the corrosponding polars type must be known before
Expand Down Expand Up @@ -210,10 +213,41 @@ fn recursive_robjname2series_tree(x: &Robj, name: &str) -> pl::PolarsResult<Seri
})
.flatten();
//todo this could probably in fewer allocations
let dt = pl::DataType::Datetime(pl::TimeUnit::Milliseconds, tz);
Ok(SeriesTree::Series(
((s * 1000f64).cast(&pl::DataType::Int64)?).cast(&dt)?,
))
match tz {
// zoned time
Some(tz) => {
Ok(SeriesTree::Series(
(s * 1_000f64).cast(&pl::DataType::Int64)?.cast(
&pl::DataType::Datetime(pl::TimeUnit::Milliseconds, Some(tz)),
)?,
))
}
// sys time
None => {
let sys_tz_robj = R!("Sys.timezone()")
.map_err(|err| pl::PolarsError::ComputeError(err.to_string().into()))?;
let sys_tz = robj_to!(String, sys_tz_robj)
.map_err(|err| pl::PolarsError::ComputeError(err.to_string().into()))?;
let utc_s = (s * 1_000f64).cast(&pl::DataType::Int64)?.cast(
&pl::DataType::Datetime(
pl::TimeUnit::Milliseconds,
Some("UTC".to_string()),
),
)?;
Ok(SeriesTree::Series(
pl::DataFrame::new(vec![utc_s.clone()])?
.lazy()
.select([col("")
.dt()
.convert_time_zone(sys_tz)
.dt()
.replace_time_zone(None, pl::lit("raise"))])
.collect()?
.column("")?
.clone(),
))
}
}
}
Ok(SeriesTree::Series(s)) if x.inherits("Date") => {
Ok(SeriesTree::Series(s.cast(&pl::DataType::Date)?))
Expand Down

0 comments on commit b3f6b4f

Please sign in to comment.