Skip to content

Commit

Permalink
refactor: swap pl$Series and as_polars_series.default
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed Apr 6, 2024
1 parent 3c0d0ec commit 6f43923
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
20 changes: 12 additions & 8 deletions R/as_polars.R
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,12 @@ as_polars_lf.RPolarsLazyGroupBy = function(x, ...) {

#' To polars Series
#'
#' [as_polars_series()] is a generic function that converts an R object to a
#' polars Series. It is basically a wrapper for [pl$Series()][pl_Series].
#' @param x Object to convert into a polars Series
#' @param name A string to use as the name of the Series.
#' If `NULL` (default), the name of `x` is used or an unnamed Series is created.
#' [as_polars_series()] is a generic function that converts an R object to
#' [a polars Series][Series_class].
#' @param x Object to convert into [a polars Series][Series_class].
#' @param name A character to use as the name of the [Series][Series_class].
#' If `NULL` (default), the name of `x` is used or an empty character `""`
#' will be used if `x` has no name.
#' @inheritParams as_polars_df
#' @return a [Series][Series_class]
#' @export
Expand All @@ -329,7 +330,8 @@ as_polars_series = function(x, name = NULL, ...) {
#' @rdname as_polars_series
#' @export
as_polars_series.default = function(x, name = NULL, ...) {
pl$Series(values = x, name = name)
.pr$Series$new(name %||% "", x) |>
unwrap("in as_polars_series():")
}


Expand Down Expand Up @@ -367,13 +369,15 @@ as_polars_series.POSIXlt = function(x, name = NULL, ...) {
#' @rdname as_polars_series
#' @export
as_polars_series.data.frame = function(x, name = NULL, ...) {
pl$DataFrame(unclass(x))$to_struct(name = name)
as_polars_df(x)$to_struct(name = name)
}


#' @rdname as_polars_series
#' @export
as_polars_series.vctrs_rcrd = as_polars_series.data.frame
as_polars_series.vctrs_rcrd = function(x, name = NULL, ...) {
pl$select(unclass(x))$to_struct(name = name)
}


#' @rdname as_polars_series
Expand Down
13 changes: 7 additions & 6 deletions R/series__series.R
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,16 @@ Series_struct = method_as_active_binding(
# TODO: change the arguments in 0.17.0
#' Create new Series
#'
#' This function is a simple way to convert basic types of vectors provided by base R to
#' This function is a simple way to convert R vectors to
#' [the Series class object][Series_class].
#' For converting more types properly, use the generic function [as_polars_series()].
#' Internally, this function is a simple wrapper of [as_polars_series()].
#' @param ... Treated as `values`, `name`, and `dtype` in order.
#' In future versions, the order of the arguments will be changed to
#' `pl$Series(name, values, dtype, ..., nan_to_null)` and `...` will be ignored.
#' @param values Vector of base R types, or `NULL` (default).
#' If `NULL`, empty Series is created.
#' @param name Name of the Series. If `NULL` (default), an empty string is used.
#' @param values Object to convert into a polars Series.
#' Passed to the `x` argument in [as_polars_series()][as_polars_series].
#' @param name A character to use as the name of the Series, or `NULL` (default).
#' Passed to the `name` argument in [as_polars_series()][as_polars_series].
#' @param dtype One of [polars data type][pl_dtypes] or `NULL`.
#' If not `NULL`, that data type is used to [cast][Expr_cast] the Series created from the vector
#' to a specific data type internally.
Expand Down Expand Up @@ -334,7 +335,7 @@ pl_Series = function(
uw()
}

out = .pr$Series$new(name %||% "", values) |>
out = result(as_polars_series(values, name)) |>
uw()

if (!is.null(dtype)) {
Expand Down
11 changes: 6 additions & 5 deletions man/as_polars_series.Rd

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

11 changes: 6 additions & 5 deletions man/pl_Series.Rd

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

0 comments on commit 6f43923

Please sign in to comment.