Skip to content

Create new Series

Source code

Description

This function is a simple way to convert R vectors to the Series class object. Internally, this function is a simple wrapper of as_polars_series().

Usage

pl$Series(
  name = NULL,
  values = NULL,
  dtype = NULL,
  ...,
  strict = TRUE,
  nan_to_null = FALSE
)

Arguments

name A character to use as the name of the Series, or NULL (default). Passed to the name argument in as_polars_series().
values Object to convert into a polars Series. Passed to the x argument in as_polars_series().
dtype One of polars data type or NULL. If not NULL, that data type is used to cast the Series created from the vector to a specific data type internally.
Ignored.
strict A logical. If TRUE (default), throw an error if any value does not exactly match the given data type by the dtype argument. If FALSE, values that do not match the data type are cast to that data type or, if casting is not possible, set to null instead. Passed to the strict argument of the $cast() method internally.
nan_to_null If TRUE, NaN values contained in the Series are replaced to null. Using the $fill_nan() method internally.

Details

Python Polars has a feature that automatically interprets something like polars.Series([1]) as polars.Series(values=[1]) if you specify Array like objects as the first argument. This feature is not available in R Polars, so something like pl$Series(1) will raise an error. You should use pl$Series(values = 1) or as_polars_series(1) instead.

Value

Series

See Also

  • as_polars_series()

Examples

library(polars)

# Constructing a Series by specifying name and values positionally:
s = pl$Series("a", 1:3)
s
#> polars Series: shape: (3,)
#> Series: 'a' [i32]
#> [
#>  1
#>  2
#>  3
#> ]
# Notice that the dtype is automatically inferred as a polars Int32:
s$dtype
#> DataType: Int32
# Constructing a Series with a specific dtype:
s2 = pl$Series(values = 1:3, name = "a", dtype = pl$Float32)
s2
#> polars Series: shape: (3,)
#> Series: 'a' [f32]
#> [
#>  1.0
#>  2.0
#>  3.0
#> ]