NEP 32 — 将 NumPy 的金融函数移除#

作者:

Warren Weckesser <warren.weckesser@gmail.com>

状态:

最终

类型:

标准轨道

创建时间:

2019-08-30

解决时间:

https://mail.python.org/pipermail/numpy-discussion/2019-September/080074.html

摘要#

我们提议弃用并最终移除 NumPy 中的金融函数[1]。这些函数将被移动到一个独立的存储库,并作为一个名为 numpy_financial 的单独包提供给社区。

动机与范围#

NumPy 的金融函数[1]是 10 个函数:fvipmtirrmirrnpernpvpmtppmtpvrate。这些函数提供了基本的金融计算,如未来值、净现值等。这些函数于 2008 年添加到 NumPy[2]

2009 年 5 月,Joe Harrington 请求将一个名为 xirr 的函数添加到金融函数中,这引发了一场关于这些函数的长篇讨论[3]。讨论中提出的一个重要观点是,“真正的”金融库必须能够处理实际日期。NumPy 的金融函数不能处理实际日期或日历。在该讨论中,多次表达了对一个更强大的、独立于 NumPy 的库的偏好。

2009 年 6 月,D. L. Goldsmith 对其中一些金融函数的实现正确性表示担忧[4]。当时有人建议将金融函数从 NumPy 移到一个独立的包中。

在 2013 年的 GitHub issue 中[5],Nathaniel Smith 建议将金融函数从顶级命名空间移到 numpy.financial。他还建议给这些函数更好的命名。当时的回复包括了弃用它们并将它们从 NumPy 移到一个单独的包的建议。该 issue 仍然开放。

2013 年晚些时候[6],邮件列表中有人建议将这些函数从 NumPy 中移除。

从 NumPy 中移除这些函数的论据

  • 它们对于 NumPy 来说过于专业化。

  • 由于它们不能处理实际日期和日历,因此对于“真实世界”的金融计算实际上没有用。

  • 其中一些函数的“正确性”定义似乎是一个约定问题,而当前的 NumPy 开发人员没有相关的背景来判断其正确性。

  • 过去和现在的 NumPy 开发人员对维护这些函数的兴趣都很小。

保留这些函数的主要论据是

  • 移除这些函数会对一些用户造成干扰。现有用户将不得不将新的 numpy_financial 包添加到他们的依赖项中,然后修改他们的代码以使用新包。

  • 提供的函数虽然不是“工业级”的,但显然与电子表格和某些计算器提供的函数相似。在 NumPy 中提供它们可以方便一些开发人员将其软件迁移到 Python 和 NumPy。

从邮件列表讨论和 GitHub issue 中的评论可以清楚地看出,许多当前的 NumPy 开发人员认为移除这些函数的好处大于成本。例如,来自[5]

The financial functions should probably be part of a separate package
-- Charles Harris

If there's a better package we can point people to we could just deprecate
them and then remove them entirely... I'd be fine with that too...
-- Nathaniel Smith

+1 to deprecate them. If no other package exists, it can be created if
someone feels the need for that.
-- Ralf Gommers

I feel pretty strongly that we should deprecate these. If nobody on numpy’s
core team is interested in maintaining them, then it is purely a drag on
development for NumPy.
-- Stephan Hoyer

以及 2013 年关于从 NumPy 中移除这些函数的邮件列表讨论

I am +1 as well, I don't think they should have been included in the first
place.
-- David Cournapeau

但并非所有人都赞成移除

The fin routines are tiny and don't require much maintenance once
written.  If we made an effort (putting up pages with examples of common
financial calculations and collecting those under a topical web page,
then linking to that page from various places and talking it up), I
would think they could attract users looking for a free way to play with
financial scenarios.  [...]
So, I would say we keep them.  If ours are not the best, we should bring
them up to snuff.
-- Joe Harrington

要了解金融函数的维护负担,可以查看所有带有 component: numpy.lib.financial 标签的 GitHub issue[7]和 pull request[8]

衡量移除这些函数影响的一种方法是查找 GitHub 上所有使用它们的应用。可以使用 python-api-inspect 服务[9]执行此类搜索。搜索 NumPy 金融函数的全部用法,发现只有八个存储库。(请参阅[5]中的评论以获取实际的 SQL 查询。)

实现#

  • 创建一个新的 Python 包 numpy_financial,该包将在顶级的 NumPy GitHub 组织下维护。该存储库将包含金融函数的定义和单元测试。该包将添加到 PyPI,以便可以使用 pip 安装。

  • 在 NumPy 版本 1.18 中开始弃用 numpy 命名空间中的金融函数。在 NumPy 版本 1.20 中移除金融函数。

向后兼容性#

如前所述,移除这些函数会破坏向后兼容性。通过提供 numpy_financial 库可以减轻这些影响。

替代方案#

[5]中提到了以下替代方案

  • 保持函数不变(即什么都不做)。 对历史的审查清楚地表明,这并非许多 NumPy 开发者的偏好。一个反复出现的评论是,这些函数根本不属于 NumPy。当这种情绪与 bug 报告的历史以及关于函数正确性的持续疑问相结合时,结论是,最干净的解决方案是弃用和移除。

  • 将函数从 ``numpy`` 命名空间移动到 ``numpy.financial``。 这是在[5]中的最初建议。这种改变并没有解决维护问题,也没有改变许多开发人员认为这些函数与 NumPy 不匹配的问题。它给当前使用这些函数的用户带来了不便,而没有解决许多开发人员认为的基本问题。

讨论#

指向过去邮件列表讨论以及相关 GitHub issue 和 pull request 的链接已经给出。关于接受 NEP 的正式提案是在 2019 年 9 月 19 日提出的[12];同时还向 PyData 发送了通知(与[11]相同的线程)。没有实质性的反对意见。

参考文献和脚注#