python - 为什么 Quantopian 的 pf.create_full_tear_sheet() 函数给我一个 DateTimeArray 错误?

标签 python pandas quantopian

我正在尝试对我自己的一组返回数据 df_returns(pandas 数据框)运行 Pyfolio 的 pf.create_full_tear_sheet(df_returns) 函数,如下所示:

enter image description here

但是我得到了错误:

TypeError: Addition/subtraction of integers and integer-arrays with DatetimeArray is no longer supported.  Instead of adding/subtracting `n`, use `n * obj.freq`

我怀疑日期格式可能有问题,因此我检查了数据类型:

In: df_returns['Date'].dtype
Out: dtype('<M8[ns]')

In: df_returns['% Returns'].dtype
Out: dtype('float64')

会不会是我没有在 pf.create_full_tear_sheet(df_returns) 中指定基准数据也是导致错误的原因?

最佳答案

我真的无法重现你的错误。这可能与您正在传递完整数据框的事实有关:根据 Pyfolio's API reference returns 参数必须作为 pd.Series 传递。

如果我只传递 Returns % 列,它会提供正确的输出。尝试:

df_returns = df_returns.set_index('Date')
pf.create_full_tear_sheet(df_returns['% Returns'])

值得一提的是,我发现依赖包已经过时了:

  • 我必须手动安装 zipline,它会将 pandas 降级回 0.22.0
  • matplotlib3.2.x 以来抛出了很多已弃用的警告,因此我将其降级为 3.1.x
  • 不推荐使用 .argmin() 并引发警告。 This issue has been known since 2019-05-24 .

这让我相信 pyfolio 可能对您的环境非常敏感。您是否使用 virtual environment instructions 安装它?在文档中?

关于python - 为什么 Quantopian 的 pf.create_full_tear_sheet() 函数给我一个 DateTimeArray 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60935336/

相关文章:

python - python 中的稀疏向量?

java - Malt 解析器抛出类未找到异常

Python 模拟 : Mocking a function inside the function I'm testing

python - Pandas 应用(轴=1): produce more than one row

python - 从第二列开始循环

python - 如何编写每周 EMA 的 CustomFactor - Python、Quantopian

python - 如何迭代 python 列表?

python - 以 3 为一组分配列值 - Python

python - 使用 pandas 读取 csv 时获取坏行/错误的数量 ( error_bad_lines)

python - 在 zipline 上运行回测时如何解决 AssertionError