我想建立可应用于大熊猫数据框自定义功能,将一些货币的注入量量我的当地货币(DKK
)自定义函数根据的汇率前一个月的最后一天(基地位于Pstng Date
)。 因此,举例来说,如果过帐日期是2016年3月12日,我想根据2016年2月28日的汇率将金额兑换。Forex_python在熊猫数据帧
我现在用的是forex-python库的转换功能 - 这是我的数据框:
df = pd.DataFrame({
'Crcy': ['DKK','CAD','GBP','USD'],
'Pstng Date': ['2017-01-12','2015-12-15','2016-06-06','2017-08-12'],
'Amount': [100,5000,40,50]
})
这里是我的功能:
from forex_python.converter import CurrencyRates
c = CurrencyRates()
def convert_rates2(amount,currency,PstngDate):
if currency != 'DKK':
return c.convert(base_cur=currency,dest_cur='DKK',amount=amount \
,date_obj=PstngDate - datetime.timedelta(PstngDate.strftime('%d')))
else:
return amount
的方式我想它应用到DF :
df['Amount, DKK'] = np.vectorize(convert_rates2)(
amount=df['Amount'],
currency=df['Crcy'],
PstngDate=df['Pstng Date']
)
但是,我得到以下错误:
AttributeError: 'str' object has no attribute 'strftime'
有没有人有如何解决这个问题的想法?不同的方法也非常受欢迎。
请张贴完整的错误消息。 – DyZ