Skip to content

Commit

Permalink
DataFrame$tail() (pola-rs#103)
Browse files Browse the repository at this point in the history
* DataFrame$tail()

* DataFrame_tail PR review
  • Loading branch information
vincentarelbundock committed Apr 13, 2023
1 parent 10b44cd commit 386c0fb
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
13 changes: 13 additions & 0 deletions R/dataframe__frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,19 @@ DataFrame_limit = function(n) {
}


#' Tail a DataFrame
#' @name DataFrame_tail
#' @description Get the last n rows.
#' @param n positive numeric of integer number not larger than 2^32
#'
#' @details any number will converted to u32. Negative raises error
#' @keywords DataFrame
#' @return DataFrame
DataFrame_tail = function(n) {
self$lazy()$tail(n)$collect()
}


#' filter DataFrame
#' @aliases DataFrame_filter
#' @description DataFrame$filter(bool_expr)
Expand Down
2 changes: 2 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,8 @@ LazyFrame$select <- function(exprs) .Call(wrap__LazyFrame__select, self, exprs)

LazyFrame$limit <- function(n) .Call(wrap__LazyFrame__limit, self, n)

LazyFrame$tail <- function(n) .Call(wrap__LazyFrame__tail, self, n)

LazyFrame$filter <- function(expr) .Call(wrap__LazyFrame__filter, self, expr)

LazyFrame$groupby <- function(exprs, maintain_order) .Call(wrap__LazyFrame__groupby, self, exprs, maintain_order)
Expand Down
12 changes: 12 additions & 0 deletions R/lazyframe__lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ LazyFrame_limit = function(n) {
unwrap(.pr$LazyFrame$limit(self,n))
}

<<<<<<< HEAD
#' @title First
#' @description Get the first row of the DataFrame.
#' @keywords DataFrame
Expand Down Expand Up @@ -299,6 +300,17 @@ LazyFrame_slice = function(offset, length = NULL) {
unwrap(.pr$LazyFrame$slice(self, offset, length))
}

#' @title Tail
#' @description take last n rows of query
#' @keywords LazyFrame
#' @param n positive numeric or integer number not larger than 2^32
#'
#' @details any number will converted to u32. Negative raises error
#'
#' @return A new `LazyFrame` object with applied filter.
LazyFrame_tail = function(n) {
unwrap(.pr$LazyFrame$tail(self,n))
}

#' @title Lazy_groupby
#' @description apply groupby on LazyFrame, return LazyGroupBy
Expand Down
21 changes: 21 additions & 0 deletions man/DataFrame_tail.Rd

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

21 changes: 21 additions & 0 deletions man/LazyFrame_tail.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/lazy/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ impl LazyFrame {
)
}

fn tail(&self, n: Robj) -> Result<LazyFrame, String> {
Ok(LazyFrame(self.0.clone().tail(robj_to!(u32, n)?)))
}

fn filter(&self, expr: &Expr) -> LazyFrame {
let new_df = self.clone().0.filter(expr.0.clone());
LazyFrame(new_df)
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,8 @@ test_that("methods without arguments", {
a = pl$DataFrame(mtcars)$slice(2, 4)$as_data_frame()
b = mtcars[3:6,]
expect_equal(a, b, ignore_attr = TRUE)
})

a = as.data.frame(pl$DataFrame(mtcars)$tail(6))
b = tail(mtcars)
expect_equal(a, b, ignore_attr = TRUE)
})
5 changes: 4 additions & 1 deletion tests/testthat/test-lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ test_that("methods without arguments", {
a = pl$DataFrame(mtcars)$lazy()$slice(2, 4)$collect()$as_data_frame()
b = mtcars[3:6,]
expect_equal(a, b, ignore_attr = TRUE)
})

a = pl$DataFrame(mtcars)$lazy()$tail(6)$collect()$as_data_frame()
b = tail(mtcars)
expect_equal(a, b, ignore_attr = TRUE)
})


#TODO complete tests for lazy

0 comments on commit 386c0fb

Please sign in to comment.