第1关:字符串操作方法
import pandas as pd
def demo():
#********** Begin **********#
data=pd.read_csv('./step1/bournemouth_venues.csv')
data1=data['Venue Name']
data2= data1.str.split().str.get(-1)
data3 = data2.str.replace("P.*","")
data3.drop(data3[data3.values==""].index,inplace = True)
data4 = data3.str.contains("[a-zA-Z]+")
data3.drop(data4[data4==False].index,inplace=True)
return data3
# ********** End **********#
第2关:Pandas的日期与时间工具
import pandas as pd
date_number = input()
# ********** Begin ********** #
d1=pd.date_range(date_number, periods=10)
print(d1)
d2=pd.period_range(date_number, periods=10, freq='D')
print(d2)
d3=pd.timedelta_range('1 hours', periods=10, freq='H')
print(d3)
# ********** End ********** #
第3关:Pandas时间序列的高级应用
import matplotlib.pyplot as plt
import pandas as pd
def demo():
yahoo = pd.read_csv("./step3/yahoo_data.csv")
yahoo.set_index(pd.to_datetime(yahoo["Date"]),inplace=True)
# 取雅虎股票的收盘价
yh = yahoo["Close"]
fig, ax = plt.subplots(2, sharex=True)
# 画出收盘价的图
yh.plot(ax=ax[0], style="-")
# 求上个季度(仅含工作日)的平均值
# ********** Begin ********** #
data1=yh.resample('BQ').mean()
# ********** End ********** #
data1.plot(ax=ax[0], style=":")
# 求每个月末(仅含工作日)的收盘价
# ********** Begin ********** #
data2=yh.asfreq('BM')
# ********** End ********** #
data2.plot(ax=ax[0], style="--", color="red")
ax[0].legend(['input', 'resample', 'asfreq'], loc='upper right')
# 迁移数据365天
# ********** Begin ********** #
data3=yh.shift(365)
# ********** End ********** #
data3.plot(ax=ax[1])
data3.resample("BQ").mean().plot(ax=ax[1], style=":")
data3.asfreq("BM").plot(ax=ax[1], style="--", color="red")
# 设置图例与标签
local_max = pd.to_datetime('2007-11-05')
offset = pd.Timedelta(365, 'D')
ax[0].axvline(local_max, alpha=0.3, color='red')
ax[1].axvline(local_max + offset, alpha=0.3, color='red')
# 求一年期移动标准差
# ********** Begin ********** #
rolling=yh.rolling(365,center=True)
data4=rolling.std()
# ********** End ********** #
data4.plot(ax=ax[1], style="y:")
data4.plot(ax=ax[0], style="y:")
plt.savefig("./step3/result/2.png")