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 中的主要論點是

  • 移除這些函數會對某些使用者造成干擾。目前的使用者將必須將新的 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。當這種情緒與錯誤報告的歷史以及關於函數正確性的持續疑問相結合時,結論是,最乾淨的解決方案是棄用和移除。

  • 將函數從 ``numpy`` 命名空間移至 ``numpy.financial``。 這是 [5] 中的初始建議。這樣的變更並未解決維護問題,也沒有改變許多開發人員認為這些函數與 NumPy 之間的不協調性。它會對目前這些函數的使用者造成干擾,而沒有解決許多開發人員認為的根本問題。

討論#

過去的郵件列表討論以及相關的 GitHub issue 和 pull request 的連結已經給出。此 NEP 的公告已於 2019 年 9 月 3 日在 NumPy-Discussion 郵件列表上發布 [10],並於 2019 年 9 月 8 日在 PyData 郵件列表上發布 [11]。接受 NEP 的正式提案已於 2019 年 9 月 19 日提出 [12];也向 PyData 發送了通知(與 [11] 相同的討論串)。沒有實質性的反對意見。

參考文獻與註腳#