From 7b093ae950e02331df607ebcedd6f1191bce5a97 Mon Sep 17 00:00:00 2001 From: eitsupi Date: Wed, 3 Jan 2024 14:03:06 +0000 Subject: [PATCH 1/2] feat: add `is_polars_*` functions --- DESCRIPTION | 1 + NAMESPACE | 3 +++ R/is_polars.R | 41 +++++++++++++++++++++++++++++++++ man/is_polars_df.Rd | 22 ++++++++++++++++++ man/is_polars_lf.Rd | 22 ++++++++++++++++++ man/is_polars_series.Rd | 22 ++++++++++++++++++ tests/testthat/test-is_polars.R | 17 ++++++++++++++ 7 files changed, 128 insertions(+) create mode 100644 R/is_polars.R create mode 100644 man/is_polars_df.Rd create mode 100644 man/is_polars_lf.Rd create mode 100644 man/is_polars_series.Rd create mode 100644 tests/testthat/test-is_polars.R diff --git a/DESCRIPTION b/DESCRIPTION index 048f9e109..65e1531c8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -93,6 +93,7 @@ Collate: 'group_by.R' 'info.R' 'ipc.R' + 'is_polars.R' 'json.R' 'lazyframe__group_by.R' 'lazyframe__lazy.R' diff --git a/NAMESPACE b/NAMESPACE index ac400b8f5..a624c3cdf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -182,6 +182,9 @@ export(.pr) export(as_polars_df) export(as_polars_lf) export(as_polars_series) +export(is_polars_df) +export(is_polars_lf) +export(is_polars_series) export(pl) importFrom(stats,median) importFrom(stats,na.omit) diff --git a/R/is_polars.R b/R/is_polars.R new file mode 100644 index 000000000..ae0507b50 --- /dev/null +++ b/R/is_polars.R @@ -0,0 +1,41 @@ +#' Test if the object is a polars DataFrame +#' +#' These functions test if the object is a polars DataFrame. +#' @param x An object +#' @return A logical value +#' @export +#' @examples +#' is_polars_df(mtcars) +#' +#' is_polars_df(as_polars_df(mtcars)) +is_polars_df = function(x) { + inherits(x, "RPolarsDataFrame") +} + + +#' Test if the object is a polars LazyFrame +#' +#' These functions test if the object is a polars LazyFrame. +#' @inherit is_polars_df params return +#' @export +#' @examples +#' is_polars_lf(mtcars) +#' +#' is_polars_lf(as_polars_lf(mtcars)) +is_polars_lf = function(x) { + inherits(x, "RPolarsLazyFrame") +} + + +#' Test if the object is a polars Series +#' +#' These functions test if the object is a polars Series. +#' @inherit is_polars_df params return +#' @export +#' @examples +#' is_polars_series(1:3) +#' +#' is_polars_series(as_polars_series(1:3)) +is_polars_series = function(x) { + inherits(x, "RPolarsSeries") +} diff --git a/man/is_polars_df.Rd b/man/is_polars_df.Rd new file mode 100644 index 000000000..33cb5e1c0 --- /dev/null +++ b/man/is_polars_df.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/is_polars.R +\name{is_polars_df} +\alias{is_polars_df} +\title{Test if the object is a polars DataFrame} +\usage{ +is_polars_df(x) +} +\arguments{ +\item{x}{An object} +} +\value{ +A logical value +} +\description{ +These functions test if the object is a polars DataFrame. +} +\examples{ +is_polars_df(mtcars) + +is_polars_df(as_polars_df(mtcars)) +} diff --git a/man/is_polars_lf.Rd b/man/is_polars_lf.Rd new file mode 100644 index 000000000..1faf6d76b --- /dev/null +++ b/man/is_polars_lf.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/is_polars.R +\name{is_polars_lf} +\alias{is_polars_lf} +\title{Test if the object is a polars LazyFrame} +\usage{ +is_polars_lf(x) +} +\arguments{ +\item{x}{An object} +} +\value{ +A logical value +} +\description{ +These functions test if the object is a polars LazyFrame. +} +\examples{ +is_polars_lf(mtcars) + +is_polars_lf(as_polars_lf(mtcars)) +} diff --git a/man/is_polars_series.Rd b/man/is_polars_series.Rd new file mode 100644 index 000000000..ea97266b3 --- /dev/null +++ b/man/is_polars_series.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/is_polars.R +\name{is_polars_series} +\alias{is_polars_series} +\title{Test if the object is a polars Series} +\usage{ +is_polars_series(x) +} +\arguments{ +\item{x}{An object} +} +\value{ +A logical value +} +\description{ +These functions test if the object is a polars Series. +} +\examples{ +is_polars_series(1:3) + +is_polars_series(as_polars_series(1:3)) +} diff --git a/tests/testthat/test-is_polars.R b/tests/testthat/test-is_polars.R new file mode 100644 index 000000000..489136210 --- /dev/null +++ b/tests/testthat/test-is_polars.R @@ -0,0 +1,17 @@ +make_is_polars_cases = function() { + tibble::tribble( + ~.test_name, ~is_func, ~as_func, ~x, + "is_polars_df", is_polars_df, as_polars_df, mtcars, + "is_polars_lf", is_polars_lf, as_polars_lf, mtcars, + "is_polars_series", is_polars_series, as_polars_series, 1:4, + ) +} + +patrick::with_parameters_test_that("is_polars_* functions", + { + polars_obj = as_func(x) + expect_true(is_func(polars_obj)) + expect_false(is_func(x)) + }, + .cases = make_is_polars_cases() +) From 5098119301ac33e52291410b7c3c6e549dd621e2 Mon Sep 17 00:00:00 2001 From: eitsupi Date: Wed, 3 Jan 2024 14:06:35 +0000 Subject: [PATCH 2/2] docs: update news --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 7dde54323..d7157507d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,7 @@ - New methods `$str$reverse()`, `$str$contains_any()`, and `$str$replace_many()` (#641). - New methods `$rle()` and `$rle_id()` (#648). +- New functions `is_polars_df()`, `is_polars_lf()`, `is_polars_series()` (#658). ### Miscellaneous