AttributeError: 'NoneType' object has no attribute 'close'解决办法,怎么解决呀
#环境&数据准备:安装包#!pip install tushare
#!pip install pyecharts
#!pip install pandas
import sys as sy
import numpy as np
import pandas as pd
import tushare as ts
import pyecharts as pye
from sklearn import datasets as ds
import matplotlib as mpl
from matplotlib import pyplot as plt
import seaborn as sns
import pyecharts as pye
#读入平安银行“000001”2017-01-01 到 2018-11-08复权后数据
df_000001 = ts.get_hist_data('000001', start='2020-01-01', end='2020-11-30')
df_szzs = ts.get_hist_data('szzs',start='2020-01-01', end='2020-11-30')
#数据准备
df_tmp1 = pd.DataFrame()
df_tmp2 = pd.DataFrame()
df_tmp1['rt_000001'] =(df_000001.close.diff(1))/df_000001.close.shift(1)
df_tmp2['rt_szzs'] =(df_szzs.close.diff(1))/df_sz.close.shift(1)
df_tmp = df_tmp1.join(df_tmp2)
df_tmp = df_tmp.dropna()
del df_tmp1
del df_tmp2
#计算Beta系数
cov_sm = np.cov(df_tmp.rt_000001, df_tmp.rt_sz)
var_m = np.var(df_tmp.rt_sz)
Beta = cov_sm/var_m
print('Bata = ' + str(Beta))
运行之后的结果是这个样子的:
AttributeError Traceback (most recent call last)
<ipython-input-4-eb0ac5645f1d> in <module>
23 df_tmp2 = pd.DataFrame()
24 df_tmp1['rt_000001'] =(df_000001.close.diff(1))/df_000001.close.shift(1)
---> 25 df_tmp2['rt_szzs'] =(df_szzs.close.diff(1))/df_sz.close.shift(1)
26 df_tmp = df_tmp1.join(df_tmp2)
27 df_tmp = df_tmp.dropna()
AttributeError: 'NoneType' object has no attribute 'close'
请教各位大神,应该怎么修改啊?网上百度的好多种方法都试过,结果不行。
df_sz 变量为NoneType, 应该是df_szzs吧 这句有错,没有返回数据(NoneType)。第一个参数应该是 股票代码。
df_szzs = ts.get_hist_data('szzs',start='2020-01-01', end='2020-11-30')#
可以自己先看看ts.get_hist_data()的用法,并检查到底有没有返回。 阿奇_o 发表于 2021-5-14 16:37
这句有错,没有返回数据(NoneType)。第一个参数应该是 股票代码。
df_szzs = ts.get_hist_data('szzs',s ...
AttributeError Traceback (most recent call last)
<ipython-input-2-ad4a93840757> in <module>
23 df_tmp2 = pd.DataFrame()
24 df_tmp1['rt_000001'] =(df_000001.close.diff(1))/df_000001.close.shift(1)
---> 25 df_tmp2['rt_szzs'] =(df_szzs.close.diff(1))/df_szzs.close.shift(1)
26 df_tmp = df_tmp1.join(df_tmp2)
27 df_tmp = df_tmp.dropna()
AttributeError: 'NoneType' object has no attribute 'close'
我现在更改了那些zs代码,可是还是有这个,问题是不是应该在close哇? Json21 发表于 2021-5-14 16:12
df_sz 变量为NoneType, 应该是df_szzs吧
哇,真的有网友回复,感动哭了!
但是那个问题是` NoneType `对象没有属性` close `,我后来改了zs,错误还是这个 玉米粥粥 发表于 2021-5-15 09:01
AttributeError Traceback (most recent call last)
in
23 df_tmp2 ...
那你print(df_szzs),有数据吗?
第一个参数用的是像 '000001'这样的股票代码吗?
页:
[1]