Skip to content

Commit

Permalink
refactor: pl$DataFrame(<data.frame>) uses as_polars_df() (#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi authored Apr 13, 2024
1 parent d47e055 commit 1ea820b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
31 changes: 19 additions & 12 deletions R/dataframe__frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ DataFrame_width = method_as_active_binding(\() .pr$DataFrame$shape(self)[2L])



#' Create new DataFrame
#' @name pl_DataFrame
#' Create a new polars DataFrame
#'
#' @param ... One of the following:
#' - a data.frame or something that inherits data.frame or DataFrame
#' - a list of mixed vectors and Series of equal length
#' - mixed vectors and/or Series of equal length
#' - a positional argument of a [data.frame] or a [DataFrame][DataFrame_class]
#' (not recommended use). In this case, the object will be passed to [as_polars_df()].
#'
#' Columns will be named as of named arguments or alternatively by names of
#' Series or given a placeholder name.
Expand All @@ -239,9 +239,9 @@ DataFrame_width = method_as_active_binding(\() .pr$DataFrame$shape(self)[2L])
#' prefixed a running number.
#' @param schema A named list that will be used to convert a variable to a
#' specific DataType. See Examples.
#'
#' @return DataFrame
#' @keywords DataFrame_new
#' @seealso
#' - [as_polars_df()]
#' @return [DataFrame][DataFrame_class]
#'
#' @examples
#' pl$DataFrame(
Expand All @@ -267,10 +267,22 @@ DataFrame_width = method_as_active_binding(\() .pr$DataFrame$shape(self)[2L])
pl_DataFrame = function(..., make_names_unique = TRUE, schema = NULL) {
uw = \(res) unwrap(res, "in $DataFrame():")

largs = unpack_list(...) |>
skip_classes = c("data.frame", "RPolarsDataFrame")
largs = unpack_list(..., skip_classes = skip_classes) |>
result() |>
uw()

# pass to `as_polars_df()`
if (length(largs) == 1L && is.null(names(largs)) &&
(inherits(largs[[1]], skip_classes))) {
# TODO: schema v.s. schema_overrides <https://github.com/pola-rs/r-polars/issues/897>
out = as_polars_df(largs[[1]], make_names_unique = make_names_unique, schema_overrides = schema) |>
result() |>
uw()

return(out)
}

if (length(largs) > 0 && !is.null(schema) && !all(names(schema) %in% names(largs))) {
Err_plain("Some columns in `schema` are not in the DataFrame.") |>
uw()
Expand All @@ -289,11 +301,6 @@ pl_DataFrame = function(..., make_names_unique = TRUE, schema = NULL) {
return(out)
}

# pass through if already a DataFrame
if (inherits(largs[[1L]], "RPolarsDataFrame")) {
return(largs[[1L]])
}

# keys are tentative new column names
keys = names(largs)
if (length(keys) == 0) keys = rep(NA_character_, length(largs))
Expand Down
15 changes: 10 additions & 5 deletions man/pl_DataFrame.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-as_polars.R
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,16 @@ test_that("as_polars_series for nested type", {
)
)
})


test_that("as_polars_df and pl$DataFrame for data.frame has list column", {
data = data.frame(a = I(list(data.frame(b = 1L))))
expect_true(
as_polars_df(data)$equals(
pl$DataFrame(data)
)
)
expect_true(
as_polars_df(data)$dtypes[[1]] == pl$List(pl$Struct(b = pl$Int32))
)
})

0 comments on commit 1ea820b

Please sign in to comment.