if语句未捕获记录

-1 投票
1 回答
71 浏览
提问于 2025-04-13 12:42

我下面创建了一个示例数据框。

import pandas as pd
import numpy_financial as npf

df = pd.DataFrame({
    'loannum': [111, 222],
    'datadt': [dt.datetime(2024, 2, 29), dt.datetime(2024, 2, 29)],
    'balloondt_i': [dt.datetime(2024, 8, 1), dt.datetime(2024, 8, 1)],
    'balloondt': [dt.datetime(2024, 8, 1), dt.datetime(2024, 8, 1)],
    'currbal': [21662536.64, 32424669.41],
    'rate': [7.349, 7.349],
    'pmtfreq': [1, 1],
    'int_only': [None, None],
    'dpd_mult': [1, 1],
    'prinbal': [0, 0],
    'prinamt': [0, 0],
    'pmtamt': [34669, 51893],
    'intamt': [0, 0],
    'intbal': [0, 0]
})

当我运行以下代码时,prinbalprinamt 字段的结果却出现了意外的负数。对于贷款111,我得到了 -97995.98,而对于贷款222,我得到了 -146681.08,这两个字段都是如此。我本来期待贷款111的结果是 3038254.50,贷款222的结果是 4547685.23

for idx in df.index:
    dpd_mult = df.loc[idx, 'dpd_mult']
    datadt = df.loc[idx, 'datadt']
    balloondt_i = df.loc[idx, 'balloondt_i']
    currbal = df.loc[idx, 'currbal']
    rate = df.loc[idx, 'rate']
    pmtfreq = df.loc[idx, 'pmtfreq']
    pmtamt = df.loc[idx, 'pmtamt'] if not pd.isna(df.loc[idx, 'pmtamt']) else None
    int_only = df.loc[idx, 'int_only']
    intbal = df.loc[idx, 'intbal']

    if balloondt_i <= datadt + relativedelta(months=+1):  # Approximates 'intnx'
        df.loc[idx, 'prinamt'] = df.loc[idx, 'currbal']
    else:
        for i in range(dpd_mult):
            if currbal <= 0:  # Break if current balance is zero or negative
                break

            df.loc[idx, 'intbal'] = df.loc[idx, 'currbal'] * rate / 1200 * pmtfreq

            if int_only == 'IO':
                df.loc[idx, 'prinbal'] = 0

            elif intbal >= pmtamt:
                nperiods = max(1, 12 * (balloondt_i.year - datadt.year) + (balloondt_i.month - datadt.month) - (i - 1) * pmtfreq) / pmtfreq
                df.loc[idx, 'prinbal'] = npf.pmt(rate * pmtfreq / 1200, nperiods, currbal) - intbal

            else:
                df.loc[idx, 'prinbal'] = df.loc[idx, 'pmtamt'] - df.loc[idx, 'intbal']

            df.loc[idx, 'currbal'] -= df.loc[idx, 'prinbal']
            df.loc[idx, 'prinamt'] += df.loc[idx, 'prinbal']
            df.loc[idx, 'intamt'] += df.loc[idx, 'intbal']

我不明白为什么这些贷款没有在 elif 语句中被正确处理。我可以通过 df.loc[df['intbal'] > df['pmtamt']] 来筛选数据框,得到了预期的结果。intbal 的计算是正确的。我尝试把 elif 移到第一个判断,但结果还是一样。我还尝试明确调用这两个字段,使用 elif df.loc[idx, 'intbal'] >= df.loc[idx, 'pmtamt'],结果依然没有变化。

我期待的输出是

loannum prinbal prinamt intamt intbal
111 3038254.50 3038254.50 132664.98 132664.98
222 4547685.23 4547685.23 198574.08 198574.08

1 个回答

1
            elif intbal >= pmtamt:

这里使用的是之前赋值给 intbal 的原始值。如果你想要获取数据框中这一行的 intbal 的最新值,你需要用 df.loc[idx, 'intbal']

            elif df.loc[idx, 'intbal'] >= pmtamt:

当我做这个修改时,它进入了 elif 这个分支,尽管最后的结果仍然不是你期待的那样。

   loannum       prinbal       prinamt         intamt         intbal
0      111 -3.170919e+06 -3.170919e+06  132664.984806  132664.984806
1      222 -4.746259e+06 -4.746259e+06  198574.079578  198574.079578

我没有检查你的计算,我不知道它们应该做什么,或者为什么你仍然得到不同的结果。这只是解决了 elif 的问题。

撰写回答