Skip to content

Commit

Permalink
Add $is_*() methods for datatypes (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher authored Apr 15, 2024
1 parent 6fe4983 commit b3ca0d3
Show file tree
Hide file tree
Showing 24 changed files with 575 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@
- `$str$extract_groups()` (#979).
- `$str$find()` (#985).
- `<DataFrame>$write_ipc()` (#1032).
- `RPolarsDataType` gains several methods to check the datatype, such as
`$is_integer()`, `$is_null()` or `$is_list()` (#1036).

- New arguments or argument values:

Expand Down
142 changes: 142 additions & 0 deletions R/datatype.R
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,145 @@ DataType_List = function(datatype = "unknown") {
DataType_Categorical = function(ordering = "physical") {
.pr$DataType$new_categorical(ordering) |> unwrap()
}

#' Check whether the data type is a temporal type
#'
#' @return A logical value
#'
#' @examples
#' pl$Date$is_temporal()
#' pl$Float32$is_temporal()
DataType_is_temporal = use_extendr_wrapper

#' Check whether the data type is a logical type
#'
#' @return A logical value
DataType_is_logical = use_extendr_wrapper

#' Check whether the data type is a float type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Float32$is_float()
#' pl$Int32$is_float()
DataType_is_float = use_extendr_wrapper

#' Check whether the data type is a numeric type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Float32$is_numeric()
#' pl$Int32$is_numeric()
#' pl$String$is_numeric()
DataType_is_numeric = use_extendr_wrapper

#' Check whether the data type is an integer type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Int32$is_integer()
#' pl$Float32$is_integer()
DataType_is_integer = use_extendr_wrapper

#' Check whether the data type is a signed integer type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Int32$is_signed_integer()
#' pl$UInt32$is_signed_integer()
DataType_is_signed_integer = use_extendr_wrapper

#' Check whether the data type is an unsigned integer type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$UInt32$is_unsigned_integer()
#' pl$Int32$is_unsigned_integer()
DataType_is_unsigned_integer = use_extendr_wrapper

#' Check whether the data type is a null type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Null$is_null()
#' pl$Float32$is_null()
DataType_is_null = use_extendr_wrapper

#' Check whether the data type is a binary type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Binary$is_binary()
#' pl$Float32$is_binary()
DataType_is_binary = use_extendr_wrapper

#' Check whether the data type is a primitive type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Float32$is_primitive()
#' pl$List()$is_primitive()
DataType_is_primitive = use_extendr_wrapper

#' Check whether the data type is a boolean type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Boolean$is_bool()
#' pl$Float32$is_bool()
DataType_is_bool = use_extendr_wrapper

#' Check whether the data type is an array type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Array(width = 2)$is_array()
#' pl$Float32$is_array()
DataType_is_array = use_extendr_wrapper

#' Check whether the data type is a list type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$List()$is_list()
#' pl$Float32$is_list()
DataType_is_list = use_extendr_wrapper

#' Check whether the data type is a nested type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$List()$is_nested()
#' pl$Array(width = 2)$is_nested()
#' pl$Float32$is_nested()
DataType_is_nested = use_extendr_wrapper

#' Check whether the data type is a temporal type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$Struct()$is_struct()
#' pl$Float32$is_struct()
DataType_is_struct = use_extendr_wrapper

#' Check whether the data type is an ordinal type
#'
#' @inherit DataType_is_temporal return
#'
#' @examples
#' pl$String$is_ord()
#' pl$Categorical()$is_ord()
DataType_is_ord = use_extendr_wrapper
30 changes: 30 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,36 @@ RPolarsDataType$get_insides <- function() .Call(wrap__RPolarsDataType__get_insid

RPolarsDataType$is_temporal <- function() .Call(wrap__RPolarsDataType__is_temporal, self)

RPolarsDataType$is_logical <- function() .Call(wrap__RPolarsDataType__is_logical, self)

RPolarsDataType$is_float <- function() .Call(wrap__RPolarsDataType__is_float, self)

RPolarsDataType$is_numeric <- function() .Call(wrap__RPolarsDataType__is_numeric, self)

RPolarsDataType$is_integer <- function() .Call(wrap__RPolarsDataType__is_integer, self)

RPolarsDataType$is_signed_integer <- function() .Call(wrap__RPolarsDataType__is_signed_integer, self)

RPolarsDataType$is_unsigned_integer <- function() .Call(wrap__RPolarsDataType__is_unsigned_integer, self)

RPolarsDataType$is_null <- function() .Call(wrap__RPolarsDataType__is_null, self)

RPolarsDataType$is_binary <- function() .Call(wrap__RPolarsDataType__is_binary, self)

RPolarsDataType$is_primitive <- function() .Call(wrap__RPolarsDataType__is_primitive, self)

RPolarsDataType$is_bool <- function() .Call(wrap__RPolarsDataType__is_bool, self)

RPolarsDataType$is_array <- function() .Call(wrap__RPolarsDataType__is_array, self)

RPolarsDataType$is_list <- function() .Call(wrap__RPolarsDataType__is_list, self)

RPolarsDataType$is_nested <- function() .Call(wrap__RPolarsDataType__is_nested, self)

RPolarsDataType$is_struct <- function() .Call(wrap__RPolarsDataType__is_struct, self)

RPolarsDataType$is_ord <- function() .Call(wrap__RPolarsDataType__is_ord, self)

#' @export
`$.RPolarsDataType` <- function (self, name) { func <- RPolarsDataType[[name]]; environment(func) <- environment(); func }

Expand Down
2 changes: 0 additions & 2 deletions R/series__series.R
Original file line number Diff line number Diff line change
Expand Up @@ -1108,8 +1108,6 @@ Series_item = function(index = NULL) {
#'
#' s$clear(n = 5)
Series_clear = function(n = 0) {
# TODO: check whether n < 0 should be removed when resolved upstream
# https://github.com/pola-rs/polars/issues/15421
if (length(n) > 1 || !is.numeric(n) || n < 0) {
Err_plain("`n` must be an integer greater or equal to 0.") |>
unwrap("in $clear():")
Expand Down
18 changes: 18 additions & 0 deletions man/DataType_is_array.Rd

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

18 changes: 18 additions & 0 deletions man/DataType_is_binary.Rd

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

18 changes: 18 additions & 0 deletions man/DataType_is_bool.Rd

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

18 changes: 18 additions & 0 deletions man/DataType_is_float.Rd

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

18 changes: 18 additions & 0 deletions man/DataType_is_integer.Rd

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

18 changes: 18 additions & 0 deletions man/DataType_is_list.Rd

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

14 changes: 14 additions & 0 deletions man/DataType_is_logical.Rd

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

19 changes: 19 additions & 0 deletions man/DataType_is_nested.Rd

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

18 changes: 18 additions & 0 deletions man/DataType_is_null.Rd

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

19 changes: 19 additions & 0 deletions man/DataType_is_numeric.Rd

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

Loading

0 comments on commit b3ca0d3

Please sign in to comment.