所得税计算 python询问如何在给定边际税率表的情况下计算税款,其答案提供了一个有效的函数(如下)。
但是,它仅适用于单一的收入价值。我将如何调整它以适用于列表/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