【发布时间】:2020-01-28 08:09:21
【问题描述】:
income tax calculation python 询问如何在给定边际税率表的情况下计算税款,its answer 提供了一个有效的函数(如下)。
但是,它仅适用于单一收入值。我将如何调整它以适用于列表/numpy 数组/pandas 系列收入值?也就是如何对这段代码进行矢量化处理?
from bisect import bisect
rates = [0, 10, 20, 30] # 10% 20% 30%
brackets = [10000, # first 10,000
30000, # next 20,000
70000] # next 40,000
base_tax = [0, # 10,000 * 0%
2000, # 20,000 * 10%
10000] # 40,000 * 20% + 2,000
def tax(income):
i = bisect(brackets, income)
if not i:
return 0
rate = rates[i]
bracket = brackets[i-1]
income_in_bracket = income - bracket
tax_in_bracket = income_in_bracket * rate / 100
total_tax = base_tax[i-1] + tax_in_bracket
return total_tax
【问题讨论】: