Skip to content

Commit

Permalink
feat: R hms class conversion to Series and Series Time type conversio…
Browse files Browse the repository at this point in the history
…n to R hms vector
  • Loading branch information
eitsupi committed Jun 29, 2024
1 parent 83cde97 commit f7316d3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ RoxygenNote: 7.3.1
SystemRequirements: Cargo (Rust's package manager), rustc
Imports:
rlang
Suggests:
hms
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ S3method(as_polars_series,default)
S3method(as_polars_series,difftime)
S3method(as_polars_series,double)
S3method(as_polars_series,factor)
S3method(as_polars_series,hms)
S3method(as_polars_series,integer)
S3method(as_polars_series,list)
S3method(as_polars_series,logical)
Expand Down
11 changes: 11 additions & 0 deletions R/as_polars_series.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ as_polars_series.difftime <- function(x, name = NULL, ...) {
wrap()
}

#' @export
as_polars_series.hms <- function(x, name = NULL, ...) {
PlRSeries$new_f64(name %||% "", x)$mul(
PlRSeries$new_i32("", 1000000000L)
)$cast(
pl$Time$`_dt`,
strict = TRUE
) |>
wrap()
}

#' @export
as_polars_series.array <- function(x, name = NULL, ...) {
dims <- dim(x) |>
Expand Down
19 changes: 19 additions & 0 deletions src/rust/src/conversion/chunked_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,22 @@ impl From<Wrap<&DurationChunked>> for Sexp {
sexp.into()
}
}

impl From<Wrap<&TimeChunked>> for Sexp {
fn from(ca: Wrap<&TimeChunked>) -> Self {
let ca = ca.0;
let mut sexp = OwnedRealSexp::new(ca.len()).unwrap();
let _ = sexp.set_class(&["hms", "difftime"]);
let _ = sexp
.set_attrib("units", <OwnedStringSexp>::try_from("secs").unwrap().into())
.unwrap();
for (i, v) in ca.into_iter().enumerate() {
if let Some(v) = v {
let _ = sexp.set_elt(i, v as f64 / 1_000_000_000.0);
} else {
let _ = sexp.set_na(i);
}
}
sexp.into()
}
}
1 change: 1 addition & 0 deletions src/rust/src/series/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl PlRSeries {
Ok(list.into())
},
DataType::Date => Ok(<Sexp>::from(Wrap(series.date().unwrap()))),
DataType::Time => Ok(<Sexp>::from(Wrap(series.time().unwrap()))),
DataType::Duration(_) => Ok(<Sexp>::from(Wrap(series.duration().unwrap()))),
DataType::Struct(_) => {
let df = series.clone().into_frame().unnest([series.name()]).unwrap();
Expand Down

0 comments on commit f7316d3

Please sign in to comment.