首页 > 解决方案 > 为什么我不能在变量上调用 load()?

问题描述

我可以调用load()REPL。

>>> load("trades.csv")
 symbol                  timestamp    price size
   AAPL 2019-05-01 09:30:00.578802 210.5200  780
   AAPL 2019-05-01 09:30:00.580485 210.8100  390
    BAC 2019-05-01 09:30:00.629205  30.2500  510
    CVX 2019-05-01 09:30:00.944122 117.8000 5860
   AAPL 2019-05-01 09:30:01.002405 211.1300  320
   AAPL 2019-05-01 09:30:01.066917 211.1186  310
   AAPL 2019-05-01 09:30:01.118968 211.0000  730
    BAC 2019-05-01 09:30:01.186416  30.2450  380
    ...                        ...      ...  ...

但我无法在脚本中加载命令行参数:

let trades = load(argv[1])

我收到一个错误:

Error: macro parameter filename requires a comptime literal
Error: unable to determine type for trades

标签: empirical-lang

解决方案


经验是静态类型的。load()如果可以在编译时确定文件路径,则将推断 Dataframe 的类型。允许使用变量和任意表达式。例如:

let filename = "trades.csv"
let trades = load("./" + filename)

上面是一个常量(不可变) ,filename因为它是用 . 声明的let。Butargv是可变的(不是常量),因为它是用 . 隐式声明的var

Empirical 必须知道 Dataframe 的类型,在这种情况下我们必须明确传递。幸运的是,我们可以在 REPL 中得到提示。

>>> columns(load("trades.csv"))
symbol: String
timestamp: Timestamp
price: Float64
size: Int64

我们还必须调用csv_load(),因为它需要一个模板。这是应该的脚本:

data Trade:
  symbol: String,
  timestamp: Timestamp,
  price: Float64,
  size: Int64
end

let trades = csv_load{Trade}(argv[1])

This compiles as expected. Calling print(trades) in the script will display the Dataframe.


推荐阅读