Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export is_polars_dtype() #927

Merged
merged 12 commits into from
Mar 17, 2024
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export(as_polars_df)
export(as_polars_lf)
export(as_polars_series)
export(is_polars_df)
export(is_polars_dtype)
export(is_polars_lf)
export(is_polars_series)
export(pl)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- New functions `pl$datetime()`, `pl$date()`, and `pl$time()` to easily create
Expr of class datetime, date, and time via columns and literals (#918).
- New function `pl$arg_where()` to get the indices that match a condition (#922).
- New function `is_polars_dtype()` (#927).

## Polars R Package 0.15.1

Expand Down
6 changes: 3 additions & 3 deletions R/dataframe__frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ pl_DataFrame = function(..., make_names_unique = TRUE, schema = NULL) {
# no args create empty DataFrame
if (length(largs) == 0L) {
if (!is.null(schema)) {
largs = lapply(seq_along(schema), \(x) {
out = lapply(seq_along(schema), \(x) {
pl$lit(numeric(0))$cast(schema[[x]])$alias(names(schema)[x])
})
out = pl$select(largs)
}) |>
pl$select()
} else {
out = .pr$DataFrame$default()
}
Expand Down
20 changes: 14 additions & 6 deletions R/datatype.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,20 @@ print.RPolarsDataType = function(x, ...) {
"!=.RPolarsDataType" = function(e1, e2) e1$ne(e2)


#' check if x is a valid RPolarsDataType
#' @name is_polars_dtype
#' @noRd
#' @param x a candidate
#' @return a list DataType with an inner DataType
#' @examples .pr$env$is_polars_dtype(pl$Int64)
#' Check if input is a valid DataType
#'
#' @param x An object to be tested.
#' @param include_unknown If `FALSE` (default), `pl$Unknown` is considered as
#' an invalid datatype.
#'
#' @export
#' @return A boolean scalar.
eitsupi marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @examples
#' is_polars_dtype(pl$Int64)
#' is_polars_dtype(mtcars)
#' is_polars_dtype(pl$Unknown)
#' is_polars_dtype(pl$Unknown, include_unknown = TRUE)
is_polars_dtype = function(x, include_unknown = FALSE) {
inherits(x, "RPolarsDataType") && (x != pl$Unknown || include_unknown)
}
Expand Down
26 changes: 26 additions & 0 deletions man/is_polars_dtype.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test-datatype.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,12 @@ test_that("allow '*' for time_zone", {

expect_identical(df$select(pl$col(pl$Datetime("ms", "*")))$width, 1)
})

test_that("is_polars_dtype works", {
expect_true(is_polars_dtype(pl$Int64))
expect_false(is_polars_dtype("numeric"))
expect_false(is_polars_dtype(mtcars))

expect_false(is_polars_dtype(pl$Unknown))
expect_true(is_polars_dtype(pl$Unknown, include_unknown = TRUE))
})
Loading