最常見的 R 向量類型會自動轉換為合適的 Arrow 資料類型,而無需擴展類型。對於預設情況下無法適當處理轉換的向量類型,您可以建立 vctrs_extension_array()
,它會將 vctrs::vec_data()
傳遞給 Array$create()
,並在 Array 轉換回 R 向量時呼叫 vctrs::vec_restore()
。
用法
vctrs_extension_array(x, ptype = vctrs::vec_ptype(x), storage_type = NULL)
vctrs_extension_type(x, storage_type = infer_type(vctrs::vec_data(x)))
引數
- x
一個 vctr(即,
vctrs::vec_is()
傳回TRUE
)。- ptype
一個
vctrs::vec_ptype()
,它通常是零長度的物件,並設定了適當的屬性。此值將使用serialize()
序列化,因此它不應參考任何無法儲存/重新載入的 R 物件。- storage_type
底層儲存陣列的 資料類型。
值
vctrs_extension_array()
傳回具有vctrs_extension_type()
的 ExtensionArray 實例。vctrs_extension_type()
傳回擴展名稱為 "arrow.r.vctrs" 的 ExtensionType 實例。
範例
(array <- vctrs_extension_array(as.POSIXlt("2022-01-02 03:45", tz = "UTC")))
#> ExtensionArray
#> <POSIXlt of length 0>
#> -- is_valid: all not null
#> -- child 0 type: double
#> [
#> 0
#> ]
#> -- child 1 type: int32
#> [
#> 45
#> ]
#> -- child 2 type: int32
#> [
#> 3
#> ]
#> -- child 3 type: int32
#> [
#> 2
#> ]
#> -- child 4 type: int32
#> [
#> 0
#> ]
#> -- child 5 type: int32
#> [
#> 122
#> ]
#> -- child 6 type: int32
#> [
#> 0
#> ]
#> -- child 7 type: int32
#> [
#> 1
#> ]
#> -- child 8 type: int32
#> [
#> 0
#> ]
#> -- child 9 type: string
#> [
#> "UTC"
#> ]
#> -- child 10 type: int32
#> [
#> 0
#> ]
array$type
#> VctrsExtensionType
#> POSIXlt of length 0
as.vector(array)
#> [1] "2022-01-02 03:45:00 UTC"
temp_feather <- tempfile()
write_feather(arrow_table(col = array), temp_feather)
read_feather(temp_feather)
#> # A tibble: 1 x 1
#> col
#> <dttm>
#> 1 2022-01-02 03:45:00
unlink(temp_feather)