Mat*_*Son 1 r apache-arrow pyarrow
.csv
我尝试读取多个文件arrow::open_dataset()
,但由于列类型不一致而引发错误。
我发现这个问题主要与我的问题有关,但我正在尝试一种稍微不同的方法。
我想使用arrow
一个示例 CSV 文件来利用类型的自动检测。弄清楚所有类型的列非常耗时。
然后,我采用架构并更正一些导致问题的列。
然后我使用更新后的架构来读取所有文件。
以下是我的方法:
data = read_csv_arrow('data.csv.gz', as_data_frame = F) # has more than 30 columns
sch = data$schema
print(sch)
Run Code Online (Sandbox Code Playgroud)
Schema
trade_id: int64
secid: int64
side: int64
...
nonstd: int64
flags: string
Run Code Online (Sandbox Code Playgroud)
我想将'trade_id'
列类型从int64
更改为string
并将其他列保持不变。
如何更新架构?
我正在使用 R arrow
,但我想相关的答案pyarrow
可能适用。
有几种不同的方法可以做到这一点;您可以提取架构的代码并自行手动更新,也可以将架构另存为变量并以编程方式更新。
library(arrow)
# set up an arrow table
cars_table <- arrow_table(mtcars)
# view the schema
sch <- cars_table$schema
# print the code that makes up the schema - you could now copy this and edit it
sch$code()
#> schema(mpg = float64(), cyl = float64(), disp = float64(), hp = float64(),
#> drat = float64(), wt = float64(), qsec = float64(), vs = float64(),
#> am = float64(), gear = float64(), carb = float64())
# look at an individual element in the schema
sch[[2]]
#> Field
#> cyl: double
# update this element
sch[[2]] <- Field$create("cylinders", int32())
sch[[2]]
#> Field
#> cylinders: int32
sch$code()
#> schema(mpg = float64(), cylinders = int32(), disp = float64(), hp = float64(),
#> drat = float64(), wt = float64(), qsec = float64(), vs = float64(),
#> am = float64(), gear = float64(), carb = float64())
Run Code Online (Sandbox Code Playgroud)