🔴
入学要求
💯
能力测试
🛣️
课程安排
🕹️
研究资源

练习

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

练习 1

a. Series

给定一组数据,请创建一个带有日期时间索引的 Pandas Series s,索引从 2016-01-01 开始,频率为每日,且长度与数据相同。

python

l = np.random.randint(1, 100, size=1000)
s = pd.Series(l)
## 你的代码写在这里

b. 访问 Series 元素

python

## 你的代码写在这里
## 你的代码写在这里

c. 布尔索引

在 Series s 中,打印所有介于 1 和 3 之间的值。

python

## 你的代码写在这里

练习 2:索引和时间序列

a. 显示

打印 Series s 的前 5 个和后 5 个元素。

python

## 你的代码写在这里
## 你的代码写在这里

b. 重采样

python

symbol = "CMG"
start = "2012-01-01"
end = "2016-01-01"
prices = get_pricing(symbol, start_date=start, end_date=end, fields="price")
## 你的代码写在这里
## 你的代码写在这里

练习 3:缺失数据

python

## 你的代码写在这里
## 你的代码写在这里

练习 4:使用 Pandas 进行时间序列分析

a. 常规信息

打印 Series s 的计数、均值、标准差、最小值、第 25、50 和 75 百分位数以及最大值。

python

print "Summary Statistics"
## 你的代码写在这里

b. Series 操作

python

data = get_pricing('GE', fields='open_price', start_date='2016-01-01', end_date='2017-01-01')
## 你的代码写在这里
## 你的代码写在这里
# 滚动均值
## 你的代码写在这里
## 你的代码写在这里
# 滚动标准差
## 你的代码写在这里
## 你的代码写在这里

练习 5:DataFrames

a. 索引

使用 dict_data 创建一个 DataFrame,并以 l 作为其索引。

python

l = {'fifth', 'fourth', 'third', 'second', 'first'}
dict_data = {'a': [1, 2, 3, 4, 5], 'b': ['L', 'K', 'J', 'M', 'Z'], 'c': np.random.normal(0, 1, 5)}
## 你的代码写在这里

b. DataFrame 操作

python

s1 = pd.Series([2, 3, 5, 7, 11, 13], name='prime')
s2 = pd.Series([1, 4, 6, 8, 9, 10], name='other')
## 你的代码写在这里
## 你的代码写在这里
## 你的代码写在这里

练习 6:访问 DataFrame 元素

a. 列

python

symbol = ["XOM", "BP", "COP", "TOT"]
start = "2012-01-01"
end = "2016-01-01"
prices = get_pricing(symbol, start_date=start, end_date=end, fields="price")
if isinstance(symbol, list):
    prices.columns = map(lambda x: x.symbol, prices.columns)
else:
    prices.name = symbol
# 检查这两者的数据类型
prices.XOM.head()
prices.loc[:, 'XOM'].head()
## 你的代码写在这里
## 你的代码写在这里
## 你的代码写在这里

练习 7:布尔索引

a. 过滤

从上题的 prices 中过滤数据,仅打印满足以下条件的值:

python

# 过滤 prices 数据,仅打印满足以下条件的值
# BP > 30
# XOM < 100
# BP > 30 且 XOM < 100
# (BP > 30 且 XOM < 100) 与 TOT 无 NaN 的并集
## 你的代码写在这里
# 添加 TSLA 列并删除 XOM 列
## 你的代码写在这里

b. DataFrame 操作(再次)

python

df_1 = get_pricing(['SPY', 'VXX'], start_date=start, end_date=end, fields='price')
df_2 = get_pricing(['MSFT', 'AAPL', 'GOOG'], start_date=start, end_date=end, fields='price')
## 你的代码写在这里
# 将 GOOG 的缺失数据填充为 0
## 你的代码写在这里

练习 8:时间序列分析

a. 汇总

python

# 打印 'prices' 时间序列的摘要
## 你的代码写在这里
# 打印前 10 个值的自然对数回报
## 你的代码写在这里
# 打印乘法回报
## 你的代码写在这里
# 标准化回报并绘图
## 你的代码写在这里
# 滚动均值
## 你的代码写在这里
# 滚动标准差
## 你的代码写在这里
# 绘图
## 你的代码写在这里