Skip to content

Clone a Series

Source code

Description

This makes a very cheap deep copy/clone of an existing Series. Rarely useful as Series are nearly 100% immutable. Any modification of a Series should lead to a clone anyways, but this can be useful when dealing with attributes (see examples).

Usage

<Series>$clone()

Value

Series

Examples

library(polars)

df1 = as_polars_series(1:10)

# Make a function to take a Series, add an attribute, and return a Series
give_attr = function(data) {
  attr(data, "created_on") = "2024-01-29"
  data
}
df2 = give_attr(df1)

# Problem: the original Series also gets the attribute while it shouldn't!
attributes(df1)
#> $class
#> [1] "RPolarsSeries"
#> 
#> $created_on
#> [1] "2024-01-29"
# Use $clone() inside the function to avoid that
give_attr = function(data) {
  data = data$clone()
  attr(data, "created_on") = "2024-01-29"
  data
}
df1 = as_polars_series(1:10)
df2 = give_attr(df1)

# now, the original Series doesn't get this attribute
attributes(df1)
#> $class
#> [1] "RPolarsSeries"