首页 > 解决方案 > 数据未存储在 pandas DataFrame 中

问题描述

我正在尝试在 python 中向 pandas DataFrame 添加一些数据,但由于某种原因,代码只是通过我的方法运行而没有添加数据。

打印log_return显示值,以便 DataFrame 不为空。

def __init__(self, stocks):
    self.log_return = pd.DataFrame()
    self.simple_return = pd.DataFrame()
    self.annual_return = pd.Series()
    self.covariance = pd.DataFrame()
    self.correlation = pd.DataFrame()
    self.nstocks = int
    self.portfolio_returns = []
    self.portfolio_volatilities = []
    self.log_return = self.get_log_return()
    self.simple_return = self.get_simple_return()
    self.annual_return = self.get_annual_return()
    self.covariance = self.get_covariance()
    self.correlation = self.get_correlation()
    self.nstocks = len(stocks)

def get_log_return(self):
    self.update_log_return()
    return self.log_return

def get_covariance(self):
    self.update_covariance()
    return self.covariance

def update_covariance(self):
    self.covariance = self.log_return.cov() * 250

def update_log_return(self):
    self.log_return = np.log(self.data / self.data.shift(1))

标签: pythonpandasdataframepycharm

解决方案


推荐阅读