Skip to content

Commit

Permalink
implement <Series>$n_chunks() method (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu authored Mar 31, 2024
1 parent bd35fc2 commit 0ac6f5d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 37 deletions.
2 changes: 2 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,8 @@ RPolarsSeries$shape <- function() .Call(wrap__RPolarsSeries__shape, self)

RPolarsSeries$len <- function() .Call(wrap__RPolarsSeries__len, self)

RPolarsSeries$n_chunks <- function() .Call(wrap__RPolarsSeries__n_chunks, self)

RPolarsSeries$chunk_lengths <- function() .Call(wrap__RPolarsSeries__chunk_lengths, self)

RPolarsSeries$alias <- function(name) .Call(wrap__RPolarsSeries__alias, self, name)
Expand Down
15 changes: 15 additions & 0 deletions R/series__series.R
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,21 @@ Series_len = use_extendr_wrapper
#' chunked_series$chunk_lengths()
Series_chunk_lengths = use_extendr_wrapper

#' Get the number of chunks that this Series contains.
#'
#' @return A numeric value
#' @examples
#' s = as_polars_series(1:3)
#' s$n_chunks()
#'
#' # Concatenate Series with rechunk = TRUE
#' s2 = as_polars_series(4:6)
#' pl$concat(s, s2, rechunk = TRUE)$n_chunks()
#'
#' # Concatenate Series with rechunk = FALSE
#' pl$concat(s, s2, rechunk = FALSE)$n_chunks()
Series_n_chunks = use_extendr_wrapper

#' Append two Series
#'
#' @param other Series to append.
Expand Down
25 changes: 25 additions & 0 deletions man/Series_n_chunks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/rust/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ impl RPolarsSeries {
self.0.chunk_lengths().map(|val| val as f64).collect()
}

pub fn n_chunks(&self) -> f64 {
self.0.n_chunks() as f64
}

pub fn alias(&self, name: &str) -> RPolarsSeries {
let mut s = self.0.clone();
s.rename(name);
Expand Down
75 changes: 38 additions & 37 deletions tests/testthat/_snaps/after-wrappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -660,32 +660,33 @@
[100] "lt" "lt_eq" "map_batches"
[103] "map_elements" "max" "mean"
[106] "median" "min" "mod"
[109] "mode" "mul" "n_unique"
[112] "name" "nan_max" "nan_min"
[115] "neq" "neq_missing" "not"
[118] "null_count" "or" "pct_change"
[121] "peak_max" "peak_min" "pow"
[124] "print" "product" "quantile"
[127] "rank" "rechunk" "reinterpret"
[130] "rename" "rep" "rep_extend"
[133] "repeat_by" "replace" "reshape"
[136] "reverse" "rle" "rle_id"
[139] "rolling_max" "rolling_mean" "rolling_median"
[142] "rolling_min" "rolling_quantile" "rolling_skew"
[145] "rolling_std" "rolling_sum" "rolling_var"
[148] "round" "sample" "search_sorted"
[151] "set_sorted" "shape" "shift"
[154] "shift_and_fill" "shrink_dtype" "shuffle"
[157] "sign" "sin" "sinh"
[160] "skew" "slice" "sort"
[163] "sort_by" "sqrt" "std"
[166] "str" "struct" "sub"
[169] "sum" "tail" "tan"
[172] "tanh" "to_frame" "to_list"
[175] "to_lit" "to_physical" "to_r"
[178] "to_struct" "to_vector" "top_k"
[181] "unique" "unique_counts" "upper_bound"
[184] "value_counts" "var" "xor"
[109] "mode" "mul" "n_chunks"
[112] "n_unique" "name" "nan_max"
[115] "nan_min" "neq" "neq_missing"
[118] "not" "null_count" "or"
[121] "pct_change" "peak_max" "peak_min"
[124] "pow" "print" "product"
[127] "quantile" "rank" "rechunk"
[130] "reinterpret" "rename" "rep"
[133] "rep_extend" "repeat_by" "replace"
[136] "reshape" "reverse" "rle"
[139] "rle_id" "rolling_max" "rolling_mean"
[142] "rolling_median" "rolling_min" "rolling_quantile"
[145] "rolling_skew" "rolling_std" "rolling_sum"
[148] "rolling_var" "round" "sample"
[151] "search_sorted" "set_sorted" "shape"
[154] "shift" "shift_and_fill" "shrink_dtype"
[157] "shuffle" "sign" "sin"
[160] "sinh" "skew" "slice"
[163] "sort" "sort_by" "sqrt"
[166] "std" "str" "struct"
[169] "sub" "sum" "tail"
[172] "tan" "tanh" "to_frame"
[175] "to_list" "to_lit" "to_physical"
[178] "to_r" "to_struct" "to_vector"
[181] "top_k" "unique" "unique_counts"
[184] "upper_bound" "value_counts" "var"
[187] "xor"

---

Expand All @@ -705,17 +706,17 @@
[21] "len" "map_elements"
[23] "max" "mean"
[25] "median" "min"
[27] "mul" "n_unique"
[29] "name" "new"
[31] "panic" "print"
[33] "rem" "rename_mut"
[35] "rep" "set_sorted_mut"
[37] "shape" "sleep"
[39] "sort_mut" "std"
[41] "sub" "sum"
[43] "to_fmt_char" "to_frame"
[45] "to_r" "value_counts"
[47] "var"
[27] "mul" "n_chunks"
[29] "n_unique" "name"
[31] "new" "panic"
[33] "print" "rem"
[35] "rename_mut" "rep"
[37] "set_sorted_mut" "shape"
[39] "sleep" "sort_mut"
[41] "std" "sub"
[43] "sum" "to_fmt_char"
[45] "to_frame" "to_r"
[47] "value_counts" "var"

# public and private methods of each class RThreadHandle

Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-series.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ test_that("shape and len", {
expect_identical(as_polars_series(1:3)$len(), 3)
})

test_that("n_chunks", {
s = as_polars_series(1:3)
s2 = as_polars_series(4:6)
expect_identical(s$n_chunks(), 1)
expect_identical(pl$concat(s, s2, rechunk = TRUE)$n_chunks(), 1)
expect_identical(pl$concat(s, s2, rechunk = FALSE)$n_chunks(), 2)
})

test_that("floor & ceil", {
expect_identical(
as_polars_series(c(1.5, .5, -.5, NA_real_, NaN))$
Expand Down

0 comments on commit 0ac6f5d

Please sign in to comment.