摘要
1. 模块同一个名字
可以放在不同文件夹下
import 包名(文件夹名).模块名(py名).函数名
import a.test
import b.test
a.test.aaa(3)
b.test.aaa(6)
2.math
进行数学运算
#1.pi
math.pi
#2.sin
math.sin()
#3.exp
math.exp(x)
3 时间
时间有string, datetime, time obj, timestramp
timestramp继承与datetime
from datetime import datetime
#因为datetime库中有datetime函数
a = pd.to_datetime('20100203')
b = datetime.strptime('20100203','%Y%m%d')
#strftime 由datetime转成字符串,strptime转成datetime
c = pd.to_datetime(a) - pd.to_datetime(b)
c.days
#可得到具体数值
4. 截取时间段数据
将index转化成datetime后
直接用字符串可以索引
也可模糊精确到年
date = ['20160102','20160203','20170304','20180304']
ts = pd.Series([1,2,3,4],index = pd.to_datetime(date))
ts['2016']
#2016-01-02 1
#2016-02-03 2
#dtype: int64
ts['20160102']
# 1
5.高低频时间频率转化(重采样)
6.dataframe 运算
series 与 series 之间进行index匹配,匹配不到的用nan表示
series 与 dataframe 之间是series的index与dataframe的columns匹配
dataframe 与 dataframe 是index与columns匹配
7.dataframe 的简便调用函数
df = pd.DataFrame(np.arange(1,13).reshape(4,3))
f = lambda x: x/x.shift(1) - 1
df2 = df.apply(f,axis=0)
8. 数据规整化
isnull() 和 notnull()来判断是否为缺失值
df.B[df.B.notnull()]
df.fillna(0)
df.fillna(method='ffill')
ffill/ pad 是前置填充
bfill/ backfill 是后置填充
9. 画图
plt.figure(1)
plt.plot(close['2014'],'--rD',label = '收盘价')
plt.plot(open['2014'],'--b>', label = '开盘价')
plt.ylim(-1,1)
plt.legend()
plt.xlabel('日期')
plt.ylabel('价格')
plt.title('*******')
plt.grid(True,axis='y')
plt.show()
上下图
fig = plt.figure(1)
ax1 = plt.subplot(211)
ax1.plot(x,y1)
ax1.st_ylabel('*****')
ax2 = plt.subplot(212)
xlim(-2.5, 2.5)
ylim(-1, 1)
ax2.plot(x, y1)
ax2.bar(***)
plt.show()
多图
双坐标
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x, y1)
ax1.set_ylabel('*****')
ax2 = ax1.twinx()
ax2.plot(x, y2, 'r')
ax2.set_xlim([0, np.e])
plt.show()
网友评论