Skip to content

Commit

Permalink
refactor!: rewrite <DataFrame>$get_columns() (#991)
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi authored Mar 30, 2024
1 parent 054be14 commit acc29eb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 29 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
- The usage of `pl$date_range()` to create a range of `Datetime` data type is deprecated.
`pl$date_range()` will always create a range of `Date` data type in the future.
Please use `pl$datetime_range()` if you want to create a range of `Datetime` instead.
- `<DataFrame>$get_columns()` now returns an unnamed list instead of a named list (#991).

- The argument `columns` in `$drop()` is removed. `$drop()` now accepts several
character scalars, such as `$drop("a", "b", "c")` (#912).
Expand Down
25 changes: 16 additions & 9 deletions R/dataframe__frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -598,16 +598,21 @@ DataFrame_clone = function() {
.pr$DataFrame$clone_in_rust(self)
}

#' Get columns (as Series)
#' @name DataFrame_get_columns
#' @description Extract all DataFrame columns as a list of Polars series.
#' Get the DataFrame as a List of Series
#'
#' @return A list of series
#' @keywords DataFrame
#' @docType NULL
#' @format NULL
#' @return A list of [Series][Series_class]
#' @seealso
#' - [`<DataFrame>$to_list()`][DataFrame_to_list]:
#' Similar to this method but returns a list of vectors instead of [Series][Series_class].
#' @examples
#' df = pl$DataFrame(iris[1:2, ])
#' df = pl$DataFrame(foo = 1L:3L, bar = 4L:6L)
#' df$get_columns()
#'
#' df = pl$DataFrame(
#' a = 1:4,
#' b = c(0.5, 4, 10, 13),
#' c = c(TRUE, TRUE, FALSE, TRUE)
#' )
#' df$get_columns()
DataFrame_get_columns = use_extendr_wrapper

Expand Down Expand Up @@ -969,7 +974,9 @@ DataFrame_to_data_frame = function(..., int64_conversion = polars_options()$int6
#'
#' @return R list of vectors
#' @inheritSection DataFrame_class Conversion to R data types considerations
#' @keywords DataFrame
#' @seealso
#' - [`<DataFrame>$get_columns()`][DataFrame_get_columns]:
#' Similar to this method but returns a list of [Series][Series_class] instead of vectors.
#' @examples
#' pl$DataFrame(iris)$to_list()
DataFrame_to_list = function(unnest_structs = TRUE, ..., int64_conversion = polars_options()$int64_conversion) {
Expand Down
22 changes: 17 additions & 5 deletions man/DataFrame_get_columns.Rd

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

7 changes: 6 additions & 1 deletion man/DataFrame_to_list.Rd

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

11 changes: 3 additions & 8 deletions src/rust/src/rdataframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,9 @@ impl RPolarsDataFrame {
}

pub fn get_columns(&self) -> List {
let mut l = List::from_values(
self.0
.get_columns()
.iter()
.map(|x| RPolarsSeries(x.clone())),
);
l.set_names(self.0.get_column_names()).unwrap();
l
let cols = self.0.get_columns().to_vec();
let vec = unsafe { std::mem::transmute::<Vec<pl::Series>, Vec<RPolarsSeries>>(cols) };
List::from_values(vec)
}

pub fn dtypes(&self) -> List {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ test_that("get column(s)", {
expected_list_of_series = {
expected = lapply(
1:5,
function(i) as_polars_series(iris[[i]],names(iris)[i])
function(i) as_polars_series(iris[[i]], names(iris)[i])
)
names(expected) = names(iris)
expected
Expand All @@ -385,7 +385,7 @@ test_that("get column(s)", {
list_of_vectors = lapply(actual_list_of_series, function(x) x$to_vector())
expect_identical(
list_of_vectors,
as.list(iris)
unname(as.list(iris))
)
})

Expand Down
5 changes: 1 addition & 4 deletions tests/testthat/test-expr_expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,7 @@ test_that("Expr_rechunk Series_chunk_lengths", {
)$get_columns()
expect_identical(
lapply(series_list, \(x) x$chunk_lengths()),
list(
a_chunked = c(3, 3),
a_rechunked = 6
)
list(c(3, 3), 6)
)
})

Expand Down

0 comments on commit acc29eb

Please sign in to comment.