这些函数使用 Arrow C++ CSV 读取器读取数据到 tibble
中。Arrow C++ 选项已映射到参数名称,这些名称遵循 readr::read_delim()
的命名约定,而 col_select
的灵感来自 vroom::vroom()
。
用法
read_delim_arrow(
file,
delim = ",",
quote = "\"",
escape_double = TRUE,
escape_backslash = FALSE,
schema = NULL,
col_names = TRUE,
col_types = NULL,
col_select = NULL,
na = c("", "NA"),
quoted_na = TRUE,
skip_empty_rows = TRUE,
skip = 0L,
parse_options = NULL,
convert_options = NULL,
read_options = NULL,
as_data_frame = TRUE,
timestamp_parsers = NULL,
decimal_point = "."
)
read_csv_arrow(
file,
quote = "\"",
escape_double = TRUE,
escape_backslash = FALSE,
schema = NULL,
col_names = TRUE,
col_types = NULL,
col_select = NULL,
na = c("", "NA"),
quoted_na = TRUE,
skip_empty_rows = TRUE,
skip = 0L,
parse_options = NULL,
convert_options = NULL,
read_options = NULL,
as_data_frame = TRUE,
timestamp_parsers = NULL
)
read_csv2_arrow(
file,
quote = "\"",
escape_double = TRUE,
escape_backslash = FALSE,
schema = NULL,
col_names = TRUE,
col_types = NULL,
col_select = NULL,
na = c("", "NA"),
quoted_na = TRUE,
skip_empty_rows = TRUE,
skip = 0L,
parse_options = NULL,
convert_options = NULL,
read_options = NULL,
as_data_frame = TRUE,
timestamp_parsers = NULL
)
read_tsv_arrow(
file,
quote = "\"",
escape_double = TRUE,
escape_backslash = FALSE,
schema = NULL,
col_names = TRUE,
col_types = NULL,
col_select = NULL,
na = c("", "NA"),
quoted_na = TRUE,
skip_empty_rows = TRUE,
skip = 0L,
parse_options = NULL,
convert_options = NULL,
read_options = NULL,
as_data_frame = TRUE,
timestamp_parsers = NULL
)
参数
- file
字符文件名或 URI,连接,文本数据(单个字符串或 原始 向量),Arrow 输入流或带有路径的
FileSystem
(SubTreeFileSystem
)。如果是文件名,则将打开一个内存映射的 Arrow InputStream,并在完成后关闭;将根据文件扩展名自动检测和处理压缩。如果提供了输入流,它将保持打开状态。
要被识别为文本数据,输入必须使用
I()
包装。- delim
用于分隔记录中字段的单个字符。
- quote
用于引用字符串的单个字符。
- escape_double
文件是否通过将引号加倍来转义引号?即,如果此选项为
TRUE
,则值""""
表示单个引号\"
。- escape_backslash
文件是否使用反斜杠来转义特殊字符?这比
escape_double
更通用,因为反斜杠可以用来转义分隔符、引号字符,或者添加特殊字符,如\\n
。- schema
描述表的 Schema。如果提供,它将用于满足
col_names
和col_types
。- col_names
如果为
TRUE
,则输入的第一行将用作列名,并且不会包含在数据框中。如果为FALSE
,则 Arrow 将生成列名,从“f0”、“f1”、...、“fN”开始。或者,您可以指定列名的字符向量。- col_types
列类型的紧凑字符串表示形式,Arrow Schema 或
NULL
(默认值)以从数据推断类型。- col_select
要保留的列名的字符向量,如
data.table::fread()
中的“select”参数,或 tidy 选择规范,如dplyr::select()
中所用。- na
要解释为缺失值的字符串的字符向量。
- quoted_na
引号内的缺失值是否应被视为缺失值(默认值)或字符串。(请注意,这与 Arrow C++ 中相应转换选项
strings_can_be_null
的默认值不同。)- skip_empty_rows
是否应完全忽略空行?如果为
TRUE
,则根本不会表示空行。如果为FALSE
,它们将填充缺失值。- skip
读取数据前要跳过的行数。
- parse_options
请参阅 CSV 解析选项。如果给出,这将覆盖其他参数(例如
delim
、quote
等)中提供的任何解析选项。- convert_options
请参阅 CSV 转换选项
- read_options
请参阅 CSV 读取选项
- as_data_frame
该函数应该返回
tibble
(默认值)还是 Arrow Table?- timestamp_parsers
用户定义的时间戳解析器。如果指定了多个解析器,CSV 转换逻辑将尝试从该向量的开头开始解析值。可能的值为
NULL
:默认值,使用 ISO-8601 解析器strptime 解析字符串的字符向量
TimestampParser 对象的列表
- decimal_point
用于浮点数中小数点的字符。
详情
read_csv_arrow()
和 read_tsv_arrow()
是 read_delim_arrow()
的包装器,用于指定分隔符。read_csv2_arrow()
使用 ;
作为分隔符,使用 ,
作为小数点。
请注意,并非所有 readr
选项都在此处实现。如果您遇到 arrow
应该支持的选项,请提交问题。
如果您需要控制 Arrow 特定的读取器参数,而这些参数在 readr::read_csv()
中没有等效的参数,您可以将它们提供给 parse_options
、convert_options
或 read_options
参数,或者您可以直接使用 CsvTableReader 进行更低级别的访问。
指定列类型和名称
默认情况下,CSV 读取器将从文件推断列名和数据类型,但您可以通过几种方式直接指定它们。
一种方法是在 schema
参数中提供 Arrow Schema,它是列名到类型的有序映射。提供后,它同时满足 col_names
和 col_types
参数。如果您预先知道所有这些信息,这将很有用。
您还可以将 Schema
传递给 col_types
参数。如果您这样做,仍然会从文件推断列名,除非您还指定了 col_names
。在任何一种情况下,Schema
中的列名都必须与数据的列名匹配,无论它们是显式提供还是推断的。也就是说,此 Schema
不必引用所有列:省略的列将推断其类型。
或者,您可以通过将 readr
使用的紧凑字符串表示形式提供给 col_types
参数来声明列类型。这意味着您提供一个字符串,每列一个字符,其中字符映射到 Arrow 类型,类似于 readr
类型映射
“c”:
utf8()
“i”:
int32()
“n”:
float64()
“d”:
float64()
“l”:
bool()
“f”:
dictionary()
“D”:
date32()
“t”:
time32()
(unit
参数设置为默认值“ms”
)“_”:
null()
“-”:
null()
“?”:从数据推断类型
如果对 col_types
使用紧凑字符串表示形式,则还必须指定 col_names
。
无论如何指定类型,所有类型为 null()
的列都将被删除。
请注意,如果您要指定列名(无论是通过 schema
还是 col_names
),并且 CSV 文件具有标题行(否则将用于标识列名),则需要添加 skip = 1
来跳过该行。
示例
tf <- tempfile()
on.exit(unlink(tf))
write.csv(mtcars, file = tf)
df <- read_csv_arrow(tf)
dim(df)
#> [1] 32 12
# Can select columns
df <- read_csv_arrow(tf, col_select = starts_with("d"))
# Specifying column types and names
write.csv(data.frame(x = c(1, 3), y = c(2, 4)), file = tf, row.names = FALSE)
read_csv_arrow(tf, schema = schema(x = int32(), y = utf8()), skip = 1)
#> # A tibble: 2 x 2
#> x y
#> <int> <chr>
#> 1 1 2
#> 2 3 4
read_csv_arrow(tf, col_types = schema(y = utf8()))
#> # A tibble: 2 x 2
#> x y
#> <int> <chr>
#> 1 1 2
#> 2 3 4
read_csv_arrow(tf, col_types = "ic", col_names = c("x", "y"), skip = 1)
#> # A tibble: 2 x 2
#> x y
#> <int> <chr>
#> 1 1 2
#> 2 3 4
# Note that if a timestamp column contains time zones,
# the string "T" `col_types` specification won't work.
# To parse timestamps with time zones, provide a [Schema] to `col_types`
# and specify the time zone in the type object:
tf <- tempfile()
write.csv(data.frame(x = "1970-01-01T12:00:00+12:00"), file = tf, row.names = FALSE)
read_csv_arrow(
tf,
col_types = schema(x = timestamp(unit = "us", timezone = "UTC"))
)
#> # A tibble: 1 x 1
#> x
#> <dttm>
#> 1 1970-01-01 00:00:00
# Read directly from strings with `I()`
read_csv_arrow(I("x,y\n1,2\n3,4"))
#> # A tibble: 2 x 2
#> x y
#> <int> <int>
#> 1 1 2
#> 2 3 4
read_delim_arrow(I(c("x y", "1 2", "3 4")), delim = " ")
#> # A tibble: 2 x 2
#> x y
#> <int> <int>
#> 1 1 2
#> 2 3 4