Skip to content

Export the polars object as a tibble data frame

Description

This S3 method is basically a shortcut of as_polars_df(x, …)$to_struct()$to_r_vector(struct = "tibble"). Additionally, you can check or repair the column names by specifying the .name_repair argument. Because polars DataFrame allows empty column name, which is not generally valid column name in R data frame.

Usage

## S3 method for class 'polars_data_frame'
as_tibble(
  x,
  ...,
  .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet",
    "universal_quiet"),
  uint8 = c("integer", "raw"),
  int64 = c("double", "character", "integer", "integer64"),
  date = c("Date", "IDate"),
  time = c("hms", "ITime"),
  decimal = c("double", "character"),
  as_clock_class = FALSE,
  ambiguous = c("raise", "earliest", "latest", "null"),
  non_existent = c("raise", "null")
)

# S3 method for class 'polars_lazy_frame'
as_tibble(
  x,
  ...,
  .name_repair = c("check_unique", "unique", "universal", "minimal", "unique_quiet",
    "universal_quiet"),
  uint8 = c("integer", "raw"),
  int64 = c("double", "character", "integer", "integer64"),
  date = c("Date", "IDate"),
  time = c("hms", "ITime"),
  decimal = c("double", "character"),
  as_clock_class = FALSE,
  ambiguous = c("raise", "earliest", "latest", "null"),
  non_existent = c("raise", "null")
)

Arguments

x A polars object
Passed to as_polars_df().
.name_repair Treatment of problematic column names:
  • “minimal”: No name repair or checks, beyond basic existence,
  • “unique”: Make sure names are unique and not empty,
  • “check_unique”: (default value), no name repair, but check they are unique,
  • “universal”: Make the names unique and syntactic
  • “unique_quiet”: Same as “unique”, but "quiet"
  • “universal_quiet”: Same as “universal”, but "quiet"
  • a function: apply custom name repair (e.g., .name_repair = make.names for names in the style of base R).
  • A purrr-style anonymous function, see rlang::as_function()
This argument is passed on as repair to vctrs::vec_as_names(). See there for more details on these terms and the strategies used to enforce them.
uint8 Determine how to convert Polars’ UInt8 type values to R type. One of the followings:
  • “integer” (default): Convert to the R’s integer type.
  • “raw”: Convert to the R’s raw type. If the value is null, export as 00.
int64 Determine how to convert Polars’ Int64, UInt32, or UInt64 type values to R type. One of the followings:
  • “double” (default): Convert to the R’s double type. Accuracy may be degraded.
  • “character”: Convert to the R’s character type.
  • “integer”: Convert to the R’s integer type. If the value is out of the range of R’s integer type, export as NA_integer\_.
  • “integer64”: Convert to the bit64::integer64 class. The bit64 package must be installed. If the value is out of the range of bit64::integer64, export as bit64::NA_integer64\_.
date Determine how to convert Polars’ Date type values to R class. One of the followings:
  • “Date” (default): Convert to the R’s Date class.
  • “IDate”: Convert to the data.table::IDate class.
time Determine how to convert Polars’ Time type values to R class. One of the followings:
  • “hms” (default): Convert to the hms::hms class. If the hms package is not installed, a warning will be shown.
  • “ITime”: Convert to the data.table::ITime class. The data.table package must be installed.
decimal Determine how to convert Polars’ Decimal type values to R type. One of the followings:
  • “double” (default): Convert to the R’s double type.
  • “character”: Convert to the R’s character type.
as_clock_class A logical value indicating whether to export datetimes and duration as the clock package’s classes.
  • FALSE (default): Duration values are exported as difftime and datetime values are exported as POSIXct. Accuracy may be degraded.
  • TRUE: Duration values are exported as clock_duration, datetime without timezone values are exported as clock_naive_time, and datetime with timezone values are exported as clock_zoned_time. For this case, the clock package must be installed. Accuracy will be maintained.
ambiguous Determine how to deal with ambiguous datetimes. Only applicable when as_clock_class is set to FALSE and datetime without timezone values are exported as POSIXct. Character vector or expression containing the followings:
  • “raise” (default): Throw an error
  • “earliest”: Use the earliest datetime
  • “latest”: Use the latest datetime
  • “null”: Return a NA value
non_existent Determine how to deal with non-existent datetimes. Only applicable when as_clock_class is set to FALSE and datetime without timezone values are exported as POSIXct. One of the followings:
  • “raise” (default): Throw an error
  • “null”: Return a NA value

Value

A tibble

See Also

  • as.data.frame(\): Export the polars object as a basic data frame.

Examples

library("polars")


# Polars DataFrame may have empty column name
df <- pl$DataFrame(x = 1:2, c("a", "b"))
df
#> shape: (2, 2)
#> ┌─────┬─────┐
#> │ x   ┆     │
#> │ --- ┆ --- │
#> │ i32 ┆ str │
#> ╞═════╪═════╡
#> │ 1   ┆ a   │
#> │ 2   ┆ b   │
#> └─────┴─────┘
# Without checking or repairing the column names
tibble::as_tibble(df, .name_repair = "minimal")
#> # A tibble: 2 × 2
#>       x ``   
#>   <int> <chr>
#> 1     1 a    
#> 2     2 b
tibble::as_tibble(df$lazy(), .name_repair = "minimal")
#> # A tibble: 2 × 2
#>       x ``   
#>   <int> <chr>
#> 1     1 a    
#> 2     2 b
# You can make that unique
tibble::as_tibble(df, .name_repair = "unique")
#> # A tibble: 2 × 2
#>       x ...2 
#>   <int> <chr>
#> 1     1 a    
#> 2     2 b
tibble::as_tibble(df$lazy(), .name_repair = "unique")
#> # A tibble: 2 × 2
#>       x ...2 
#>   <int> <chr>
#> 1     1 a    
#> 2     2 b