我有以下课程:
import pandas as pd
class CashFlowSchedule:
flows = {}
annual_growth_rate = None
df = None
daterange = None
months = None
amount = None
growth = None
growth_month = None
name = None
def copy(self):
return CashFlowSchedule(daterange=self.daterange, months=self.months, amount=self.amount, growth=self.growth,
growth_month=self.growth_month)
def render_named_df(self):
temp = self.df.copy()
temp.columns = [self.name]
return temp
def re_init_df(self):
self.df = pd.DataFrame([self.flows]).T
def __init__(self, daterange=pd.date_range('19900101', '20010101'), months=range(1, 13), amount=0, growth=0,
growth_month=1, name=None, years=None):
self.daterange = daterange
self.months = months
self.amount = amount
self.growth = growth
self.growth_month = growth_month
self.name = name
self.annual_growth_rate = growth
if years is None:
years = list({x.year: 0 for x in daterange}.keys())
for dt in daterange:
if dt.month == growth_month:
amount *= (1. + self.annual_growth_rate)
if dt.month in months and dt.year in years:
self.flows[dt] = amount
else:
self.flows[dt] = 0.
self.df = pd.DataFrame([self.flows]).T
和另一个模块中的另一个类:
class SinglePropertyValuation:
# descriptive
desc_zipcode = None
desc_property_type = None
desc_units = None
desc_beds = None
desc_smooth_annual_expenses = None
# dates
date_purchase = None
date_sale = None
date_distance_months = None
date_daterange_ownership = None
# revenue
rev_rent = None
# recurring expenses
exp_taxes = None
exp_insurance = None
exp_maintenance = None
exp_management = None
exp_hoa = None
cash_flows = {}
monthly_raw_rents = []
desc_nearby_zips = None
def establish_rent_cashflows(self, monthly_per_unit_rent, growth_rate=None,
growth_month=None, default_growth_rate=.02):
self.rev_rent =copy.copy(cfs.CashFlowSchedule(daterange=copy.copy(self.date_daterange_ownership),
amount=monthly_per_unit_rent * self.desc_units, growth=growth_rate,
growth_month=growth_month, name='rev_rent'))
def establish_tax_cashflows(self, annual_tax, growth):
if self.desc_smooth_annual_expenses:
self.exp_taxes = copy.copy(cfs.CashFlowSchedule(daterange=self.date_daterange_ownership, amount=annual_tax / 12.,
growth=growth, growth_month=5, name='exp_taxes'))
else:
self.exp_taxes = copy.copy(cfs.CashFlowSchedule(daterange=self.date_daterange_ownership, amount=annual_tax,
months=[4],
growth=growth, growth_month=5, name='exp_taxes'))
def establish_vacancy_data(self, override_vacancy_rate=None):
self.exp_vacancy_data = self.rev_rent.copy()
self.exp_vacancy_data.flows = {dt: self.exp_vacancy_data.flows[dt] * override_vacancy_rate for dt in
self.exp_vacancy_data.flows.keys()}
self.exp_vacancy_data.re_init_df()
self.exp_vacancy_data.name = 'exp_vacancy'
当我打电话时:
spv = SinglePropertyValuation()
spv.establish_rent_cashflows(monthly_per_unit_rent=1063, growth_rate=.01)
spv.establish_vacancy_data(override_vacancy_rate=1. / 24.)
spv.establish_tax_cashflows(annual_tax=8000., growth=.01)
我的spv.rev_rent.flows
字典在第一次调用时正确实例化,并且在我的调用中没有改变spv.establish_vacancy_data
,但是当spv.establish_tax_cashflows
被触发时,spv.rev_rent.flows
CHANGES。
我该如何纠正这种行为?通过谷歌搜索和反复试验,我意识到这是一个常见错误,但我不知道如何修复我的错误,因为大多数答案都建议复制(我正在这样做)。
现在,正如我正在查看的那样,spv.rev_rent
应该spv.exp_maintenance
通过 更加相关copy()
,但是导致意外行为的是税收电话。
让我发疯-非常感谢任何帮助!