我创建了一个类对象,从数据库中检索信息并将其存储在pandas中,这样我就可以使用一些数据科学库进行操作。
class IntDailyGoals (object):
def __init__(self, begin_date, end_date, store=None):
self.begin_date = begin_date
self.end_date = end_date
self.store = store
self.int_mnth_goal = pd.DataFrame(list(StoreGoalsInput.objects.values('store_number',
'interest',
'date')))
self.int_mnth_goal['interest'] = pd.to_numeric(self.int_mnth_goal['interest'])
self.int_mnth_goal['date'] = pd.to_datetime(self.int_mnth_goal['date'])
self.mnth_goal_int =self.int_mnth_goal[(self.int_mnth_goal['date'] >= self.begin_date) &
(self.int_mnth_goal['date'] <= self.end_date) &
(self.int_mnth_goal['store_number'] == self.store.store_number)]
self.mnth_goal_int= self.mnth_goal_int['interest']
self.tot_workingdays = np.busday_count(np.datetime64(self.begin_date),
np.datetime64(self.end_date),
weekmask='Mon Tue Wed Thu Fri Sat')
self.div_intmnthgoal_workingdays = round(np.divide(self.mnth_goal_int, self.tot_workingdays),2)
def get_div_goalsint_wdays(self):
div_goalsint_wdays = self.div_intmnthgoal_workingdays
return div_goalsint_wdays
def __str__(self):
return self.get_div_goalsint_wdays()
这将返回:
2 6558.4
Name: interest, dtype: float64
6558.4
这将显示在Django模板中。
def get_div_goalsint_wdays(self):
div_goalsint_wdays = self.div_intmnthgoal_workingdays['interest']
return div_goalsint_wdays
但我有一个
KeyError: 'interest'