# 首先 准备生成一个关于基金数据的Series
def get_fund_series():
import requests
from dateutil import parser
import datetime
headers = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
'Sec-Fetch-User': '?1',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-Mode': 'navigate',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
}
response = requests.get('https://glink.genius.com.cn/base/V_JRJ_FUND_NET_HISTORY/full=2&sort=TRADEDATE%20desc&filter-FUND_CODE-str=002001&limit=200', headers=headers)
res = response.json()
data = []
index= []
for item in res['rows']:
temp = parser.parse(item['MTIME'])+ datetime.timedelta(hours=8)
temp = temp.strftime('%Y-%m-%d')
if temp not in index:
index.append(temp)
data.append(item['UNIT_NET'])
return index, data
index, data = get_fund_series()
fund = pd.Series(data, index)
# 查看 Series 头部和尾部数据
print(fund.head())
print(fund.tail(3))
2019-12-18 1.512
2019-12-17 1.503
2019-12-14 1.514
2019-12-13 1.496
2019-12-12 1.498
dtype: float64
2019-03-02 1.370
2019-03-01 1.350
2019-02-28 1.343
dtype: float64
# 查看index
print(fund.index)
Index(['2019-12-18', '2019-12-17', '2019-12-14', '2019-12-13', '2019-12-12',
'2019-12-11', '2019-12-10', '2019-12-07', '2019-12-06', '2019-12-05',
...
'2019-03-13', '2019-03-12', '2019-03-09', '2019-03-08', '2019-03-07',
'2019-03-06', '2019-03-05', '2019-03-02', '2019-03-01', '2019-02-28'],
dtype='object', length=198)
# 无论是头部尾部,还是index 我们得到的都是Series类型的数据,也可以转为list/numpy.array/
print(fund.head().values, fund.head().to_numpy(), np.asarray(fund.head()), list(fund.head()))
print('****************')
print(fund.head().array)
[1.512 1.503 1.514 1.496 1.498] [1.512 1.503 1.514 1.496 1.498] [1.512 1.503 1.514 1.496 1.498] [1.512, 1.503, 1.514, 1.496, 1.498]
****************
[1.512, 1.503, 1.514, 1.496, 1.498]
Length: 5, dtype: float64