如何在一个函数中计算天数和浮动

2024-10-03 15:26:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的函数中计算天数和浮动

所以这是我的数据帧

In[1]
df = {'Loan Nego': [2019-03-01, 2019-03-01], 'New Maturity': [2019-03-11, 2019-03-29],'Loan Amount in OCUR': [1000, 2000]}

Out[1]
Loan Nego          New Maturity          Loan Amount in OCUR   
2019-03-01         2019-03-11            1000
2019-03-01         2019-03-29            2000

In[2]
df.dtypes

Out[2]

New Maturity               datetime64[ns]
Loan Nego                  datetime64[ns]
Loan Amount in OCUR        float64

我想把这个数据框输入到我的函数中

# Equation CLOF
def clof(loan,maturity, amount):
days = (maturity-loan).days
return ((amount * days)/ 360) * (2.36/100)

我试过了,但他们是这样警觉的

AttributeError:'Series'对象没有属性'days'

df["New Interest"] = clof(df["Loan Nego"],df["New Maturity"],df["Loan Amount in OCUR"])

它不起作用

AttributeError:“Series”对象没有“days”属性

我的期望

Loan Nego          New Maturity          Loan Amount in OCUR       New Interest 
2019-03-01         2019-03-11            1000                        0.65            
2019-03-01         2019-03-29            2000                        3.671

有什么解决办法


Tags: 数据函数indfnewoutdaysamount
1条回答
网友
1楼 · 发布于 2024-10-03 15:26:29

使用^{},因为在函数中使用Series,而不是使用标量:

def clof(loan,maturity, amount):
    days = (maturity-loan).dt.days
    return ((amount * days)/ 360) * (2.36/100)

df["New Interest"] = clof(df["Loan Nego"],df["New Maturity"],df["Loan Amount in OCUR"])
print (df)
   Loan Nego New Maturity  Loan Amount in OCUR  New Interest
0 2019-03-01   2019-03-11                 1000      0.655556
1 2019-03-01   2019-03-29                 2000      3.671111

相关问题 更多 >