gpt4 book ai didi

python - CustomClass复制错误: AttributeError: 'CustomClass' object has no attribute 'copy'

转载 作者:行者123 更新时间:2023-12-01 09:09:03 25 4
gpt4 key购买 nike

我在自己的 .py 文件中有以下类:

import pandas as pd

class CashFlowSchedule(object):
flows = {}
annual_growth_rate = None
df = None

def __init__(self, daterange, months=range(1, 13), amount=0, growth=0, growth_month=1):

self.annual_growth_rate = growth

for dt in daterange:

if dt.month == growth_month:
amount *= (1. + self.annual_growth_rate)


if dt.month in months:
self.flows[dt] = amount
else:
self.flows[dt] = 0.

self.df = pd.DataFrame([self.flows]).T

当我打电话时:

import cf_schedule as cfs
x=cfs.CashFlowSchedule(pd.date_range('20180101','20190101'))
x.copy()

我得到:

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-22-c96b6d8d0ab0> in <module>()
----> 1 x.copy()

AttributeError: 'CashFlowSchedule' object has no attribute 'copy'

出了什么问题,我在这里错过了什么?

这个类非常原始,我认为 __copy__ 应该存在于 object 方法中。

谢谢

最佳答案

问题是 CashFlowSchedule 类没有 copy() 方法。

假设您要创建 CashFlowSchedule 的副本,您应该使用 Python 的 copy library .

创建浅拷贝:

import copy
import cf_schedule as cfs
x=cfs.CashFlowSchedule(pd.date_range('20180101','20190101'))
x_copy = copy.copy(x)

要创建深拷贝,只需将最后一行替换为:

x_copy = copy.deepcopy(x)

关于python - CustomClass复制错误: AttributeError: 'CustomClass' object has no attribute 'copy' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51813906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com