pandas新手教程之基础篇 zt

python api文档见这里:

https://www.ricequant.com/api/python/chn

Jupyter Notebook社区 策略研究 我的策略 帮助 登出 tutorials_basic Last Checkpoint: Last Wednesday at 3:29 PM (autosaved)
Ipython List策略研究列表
Python 3
File
Edit
View
Insert
Cell
Kernel
Help
Cell Toolbar:
pandas新手教程之基础篇
下面是关于pandas以及其生态系统下的libraries的基础运用展示
基础篇教程分10节,从最基础的Series和DataFrame讲起,直到最后做成最基本的移动均线和标准差,达到可以基本可以使用ricequant上ipython       notebook的级别。

注:一些要调用的libraries,之后会用到
在手打运行参考代码的时候,一定要对齐,一定要对齐,一定要对齐!重要的事情说三遍!(⊙o⊙)
另由于教程的篇幅,所以设置了显示的最长行数31,保证有一个月的数据能够在教程中显示出来
In [8]:

from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import scipy as sp
import statsmodels.tsa.stattools as sts
import matplotlib.pyplot as plt
import statsmodels.api as sm
pd.options.display.max_rows = 31
1:series
Pandas最重要的数据结构有二,Series和DataFrame,Panel暂不涉及。本节主要提到pandas series的一些基本操作
In [9]:

labels = [‘a’,’b’,’c’,’d’,’e’]
s2 = Series(np.random.randn(5),index =labels)
s2
Out[9]:
a   -1.333205
b   -1.049079
c    1.431448
d    0.292383
e    0.918513
dtype: float64
In [6]:

‘b’ in s2
Out[6]:
True
In [4]:

s2[‘b’]
Out[4]:
-0.36129673980263433
to_dict查看词典
In [5]:

mapping = s2.to_dict()
mapping
Out[5]:
{‘a': -0.38272820928427753,
‘b': -0.36129673980263433,
‘c': -0.13586616742241123,
‘d': -1.1435589583111128,
‘e': -2.193229583218169}
通过词典的方式也可以构建Series
In [6]:

Series(mapping)

Out[6]:
a   -0.382728
b   -0.361297
c   -0.135866
d   -1.143559
e   -2.193230
dtype: float64
一次取数据的长度默认为一年,见下,至于为什么是如此的数据结构,结尾彩蛋告诉你~
In [7]:

ts = get_price(‘600208.XSHG’)[‘ClosingPx’][-10:]
ts
Out[7]:
MDEntryDate
2013-12-20    3.17
2013-12-23    3.18
2013-12-24    3.17
2013-12-25    3.16
2013-12-26    3.11
2013-12-27    3.16
2013-12-30    3.17
2013-12-31    3.20
2014-01-02    3.19
2014-01-03    3.13
Name: ClosingPx, dtype: float64
声明了strat_date & end_date 以后才可以进行更个性化的定制,当然了,美股别忘记加上国家’us’
In [8]:

tsspecial = get_price(‘AAPL.US’,’us’, start_date=’2001-04-01′, end_date=’2015-04-12′)[‘ClosingPx’][-90:]
tsspecial
Out[8]:
MDEntryDate
2014-12-01    115.070
2014-12-02    114.630
2014-12-03    115.930
2014-12-04    115.490
2014-12-05    115.000
2014-12-08    112.400
2014-12-09    114.120
2014-12-10    111.950
2014-12-11    111.620
2014-12-12    109.730
2014-12-15    108.225
2014-12-16    106.745
2014-12-17    109.410
2014-12-18    112.650
2014-12-19    111.780

2015-03-20    125.900
2015-03-23    127.210
2015-03-24    126.690
2015-03-25    123.380
2015-03-26    124.240
2015-03-27    123.250
2015-03-30    126.370
2015-03-31    124.430
2015-04-01    124.250
2015-04-02    125.320
2015-04-06    127.350
2015-04-07    126.010
2015-04-08    125.600
2015-04-09    126.560
2015-04-10    127.100
Name: ClosingPx, dtype: float64
从头按顺序取ts前5项
In [9]:

ts[:5]
Out[9]:
MDEntryDate
2013-12-20    3.17
2013-12-23    3.18
2013-12-24    3.17
2013-12-25    3.16
2013-12-26    3.11
Name: ClosingPx, dtype: float64
In [10]:

ts.index
Out[10]:
DatetimeIndex([‘2013-12-20′, ‘2013-12-23′, ‘2013-12-24′, ‘2013-12-25′,
‘2013-12-26′, ‘2013-12-27′, ‘2013-12-30′, ‘2013-12-31′,
‘2014-01-02′, ‘2014-01-03′],
dtype=’datetime64[ns]’, name=’MDEntryDate’, freq=None, tz=None)
确认标签,并且按照标签取值,其实和直接按位置取是等价的。
In [13]:

date = ts.index[6]
date
Out[13]:
Timestamp(‘2013-12-30 00:00:00′)
In [14]:

ts[date]
Out[14]:
3.1699999999999999
In [15]:

ts[6]
Out[15]:
3.1699999999999999
米筐技能:同时读取多只股票,更多米筐技请参考basic_demo.
In [16]:

dfcn = get_price([‘000024.XSHE’, ‘000001.XSHE’, ‘000002.XSHE’])[‘ClosingPx’][-10:]
2:DataFrame
本节介绍关于DataFrame数据结构的一些基本操作
In [17]:

dfus = get_price([‘AAPL.US’,’IBM.US’,’MSFT.US’],country = ‘us’) [‘ClosingPx’][-30:]
dfus
Out[17]:
AAPL.US IBM.US MSFT.US
MDEntryDate
2013-11-20 73.571429 185.19 37.080
2013-11-21 74.448000 184.13 37.400
2013-11-22 74.257143 181.30 37.570
2013-11-25 74.820000 178.94 37.640
2013-11-26 76.200000 177.31 37.350
2013-11-27 77.994286 178.97 37.600
2013-11-29 79.438571 179.68 38.130
2013-12-02 78.747143 177.48 38.450
2013-12-03 80.903143 176.08 38.310
2013-12-04 80.714286 175.74 38.940
2013-12-05 81.128714 176.08 38.000
2013-12-06 80.002857 177.67 38.360
2013-12-09 80.918571 177.46 38.705
2013-12-10 80.792857 177.12 38.110
2013-12-11 80.194286 175.20 37.610
2013-12-12 80.077143 173.37 37.220
2013-12-13 79.204286 172.80 36.690
2013-12-16 79.642857 177.85 36.885
2013-12-17 79.284286 175.76 36.520
2013-12-18 78.681429 178.70 36.580
2013-12-19 77.780000 180.22 36.250
2013-12-20 78.431429 180.02 36.800
2013-12-23 81.441429 182.23 36.620
2013-12-24 81.095714 183.22 37.080
2013-12-26 80.557143 185.35 37.440
2013-12-27 80.012857 185.08 37.290
2013-12-30 79.217143 186.41 37.290
2013-12-31 80.145714 187.57 37.410
2014-01-02 79.018571 185.53 37.160
2014-01-03 77.282857 186.64 36.910
考察IBM和微软的股价比率
In [18]:

dfus[‘Ratio’] =  dfus[‘IBM.US’] / dfus[‘MSFT.US’]
dfus
Out[18]:
AAPL.US IBM.US MSFT.US Ratio
MDEntryDate
2013-11-20 73.571429 185.19 37.080 4.994337
2013-11-21 74.448000 184.13 37.400 4.923262
2013-11-22 74.257143 181.30 37.570 4.825659
2013-11-25 74.820000 178.94 37.640 4.753985
2013-11-26 76.200000 177.31 37.350 4.747256
2013-11-27 77.994286 178.97 37.600 4.759840
2013-11-29 79.438571 179.68 38.130 4.712300
2013-12-02 78.747143 177.48 38.450 4.615865
2013-12-03 80.903143 176.08 38.310 4.596189
2013-12-04 80.714286 175.74 38.940 4.513097
2013-12-05 81.128714 176.08 38.000 4.633684
2013-12-06 80.002857 177.67 38.360 4.631648
2013-12-09 80.918571 177.46 38.705 4.584937
2013-12-10 80.792857 177.12 38.110 4.647599
2013-12-11 80.194286 175.20 37.610 4.658336
2013-12-12 80.077143 173.37 37.220 4.657980
2013-12-13 79.204286 172.80 36.690 4.709730
2013-12-16 79.642857 177.85 36.885 4.821743
2013-12-17 79.284286 175.76 36.520 4.812705
2013-12-18 78.681429 178.70 36.580 4.885183
2013-12-19 77.780000 180.22 36.250 4.971586
2013-12-20 78.431429 180.02 36.800 4.891848
2013-12-23 81.441429 182.23 36.620 4.976242
2013-12-24 81.095714 183.22 37.080 4.941208
2013-12-26 80.557143 185.35 37.440 4.950588
2013-12-27 80.012857 185.08 37.290 4.963261
2013-12-30 79.217143 186.41 37.290 4.998927
2013-12-31 80.145714 187.57 37.410 5.013900
2014-01-02 79.018571 185.53 37.160 4.992734
2014-01-03 77.282857 186.64 36.910 5.056624
删除多余该列,dfus留着之后用
In [19]:

del dfus[‘Ratio’]
dfus
Out[19]:
AAPL.US IBM.US MSFT.US
MDEntryDate
2013-11-20 73.571429 185.19 37.080
2013-11-21 74.448000 184.13 37.400
2013-11-22 74.257143 181.30 37.570
2013-11-25 74.820000 178.94 37.640
2013-11-26 76.200000 177.31 37.350
2013-11-27 77.994286 178.97 37.600
2013-11-29 79.438571 179.68 38.130
2013-12-02 78.747143 177.48 38.450
2013-12-03 80.903143 176.08 38.310
2013-12-04 80.714286 175.74 38.940
2013-12-05 81.128714 176.08 38.000
2013-12-06 80.002857 177.67 38.360
2013-12-09 80.918571 177.46 38.705
2013-12-10 80.792857 177.12 38.110
2013-12-11 80.194286 175.20 37.610
2013-12-12 80.077143 173.37 37.220
2013-12-13 79.204286 172.80 36.690
2013-12-16 79.642857 177.85 36.885
2013-12-17 79.284286 175.76 36.520
2013-12-18 78.681429 178.70 36.580
2013-12-19 77.780000 180.22 36.250
2013-12-20 78.431429 180.02 36.800
2013-12-23 81.441429 182.23 36.620
2013-12-24 81.095714 183.22 37.080
2013-12-26 80.557143 185.35 37.440
2013-12-27 80.012857 185.08 37.290
2013-12-30 79.217143 186.41 37.290
2013-12-31 80.145714 187.57 37.410
2014-01-02 79.018571 185.53 37.160
2014-01-03 77.282857 186.64 36.910
基本属性:标签(index)、列(columns)、值(values)
In [20]:

dfus.index
Out[20]:
DatetimeIndex([‘2013-11-20′, ‘2013-11-21′, ‘2013-11-22′, ‘2013-11-25′,
‘2013-11-26′, ‘2013-11-27′, ‘2013-11-29′, ‘2013-12-02′,
‘2013-12-03′, ‘2013-12-04′, ‘2013-12-05′, ‘2013-12-06′,
‘2013-12-09′, ‘2013-12-10′, ‘2013-12-11′, ‘2013-12-12′,
‘2013-12-13′, ‘2013-12-16′, ‘2013-12-17′, ‘2013-12-18′,
‘2013-12-19′, ‘2013-12-20′, ‘2013-12-23′, ‘2013-12-24′,
‘2013-12-26′, ‘2013-12-27′, ‘2013-12-30′, ‘2013-12-31′,
‘2014-01-02′, ‘2014-01-03′],
dtype=’datetime64[ns]’, name=’MDEntryDate’, freq=None, tz=None)
In [21]:

dfus.columns
Out[21]:
Index([‘AAPL.US’, ‘IBM.US’, ‘MSFT.US’], dtype=’object’)
In [22]:

dfus.values
Out[22]:
array([[  73.57142857,  185.19      ,   37.08      ],
[  74.448     ,  184.13      ,   37.4       ],
[  74.25714286,  181.3       ,   37.57      ],
[  74.82      ,  178.94      ,   37.64      ],
[  76.2       ,  177.31      ,   37.35      ],
[  77.99428571,  178.97      ,   37.6       ],
[  79.43857143,  179.68      ,   38.13      ],
[  78.74714286,  177.48      ,   38.45      ],
[  80.90314286,  176.08      ,   38.31      ],
[  80.71428571,  175.74      ,   38.94      ],
[  81.12871429,  176.08      ,   38.        ],
[  80.00285714,  177.67      ,   38.36      ],
[  80.91857143,  177.46      ,   38.705     ],
[  80.79285714,  177.12      ,   38.11      ],
[  80.19428571,  175.2       ,   37.61      ],
[  80.07714286,  173.37      ,   37.22      ],
[  79.20428571,  172.8       ,   36.69      ],
[  79.64285714,  177.85      ,   36.885     ],
[  79.28428571,  175.76      ,   36.52      ],
[  78.68142857,  178.7       ,   36.58      ],
[  77.78      ,  180.22      ,   36.25      ],
[  78.43142857,  180.02      ,   36.8       ],
[  81.44142857,  182.23      ,   36.62      ],
[  81.09571429,  183.22      ,   37.08      ],
[  80.55714286,  185.35      ,   37.44      ],
[  80.01285714,  185.08      ,   37.29      ],
[  79.21714286,  186.41      ,   37.29      ],
[  80.14571429,  187.57      ,   37.41      ],
[  79.01857143,  185.53      ,   37.16      ],
[  77.28285714,  186.64      ,   36.91      ]])
In [23]:

date = dfus.index[5]
dfus.ix[date]
Out[23]:
AAPL.US     77.994286
IBM.US     178.970000
MSFT.US     37.600000
Name: 2013-11-27 00:00:00, dtype: float64
其等价于如下代码,Series和DataFrame在这方面是类似的。
In [24]:

dfus.ix[5]
Out[24]:
AAPL.US     77.994286
IBM.US     178.970000
MSFT.US     37.600000
Name: 2013-11-27 00:00:00, dtype: float64
In [25]:

dfus.ix[5,’IBM.US’]
Out[25]:
178.97
最基本的筛选
In [26]:

dfus.ix[dfus[‘AAPL.US’]>80]
Out[26]:
AAPL.US IBM.US MSFT.US
MDEntryDate
2013-12-03 80.903143 176.08 38.310
2013-12-04 80.714286 175.74 38.940
2013-12-05 81.128714 176.08 38.000
2013-12-06 80.002857 177.67 38.360
2013-12-09 80.918571 177.46 38.705
2013-12-10 80.792857 177.12 38.110
2013-12-11 80.194286 175.20 37.610
2013-12-12 80.077143 173.37 37.220
2013-12-23 81.441429 182.23 36.620
2013-12-24 81.095714 183.22 37.080
2013-12-26 80.557143 185.35 37.440
2013-12-27 80.012857 185.08 37.290
2013-12-31 80.145714 187.57 37.410
复杂一点,在之前的基础上选取IBM和MSFT两列
In [27]:

dfus.ix[dfus[‘AAPL.US’]>80,[‘IBM.US’,’MSFT.US’]]
Out[27]:
IBM.US MSFT.US
MDEntryDate
2013-12-03 176.08 38.310
2013-12-04 175.74 38.940
2013-12-05 176.08 38.000
2013-12-06 177.67 38.360
2013-12-09 177.46 38.705
2013-12-10 177.12 38.110
2013-12-11 175.20 37.610
2013-12-12 173.37 37.220
2013-12-23 182.23 36.620
2013-12-24 183.22 37.080
2013-12-26 185.35 37.440
2013-12-27 185.08 37.290
2013-12-31 187.57 37.410
赋个空值
In [28]:

dfus.ix[dfus[‘AAPL.US’]>80,[‘IBM.US’,’MSFT.US’]] = np.nan
dfus
Out[28]:
AAPL.US IBM.US MSFT.US
MDEntryDate
2013-11-20 73.571429 185.19 37.080
2013-11-21 74.448000 184.13 37.400
2013-11-22 74.257143 181.30 37.570
2013-11-25 74.820000 178.94 37.640
2013-11-26 76.200000 177.31 37.350
2013-11-27 77.994286 178.97 37.600
2013-11-29 79.438571 179.68 38.130
2013-12-02 78.747143 177.48 38.450
2013-12-03 80.903143 NaN NaN
2013-12-04 80.714286 NaN NaN
2013-12-05 81.128714 NaN NaN
2013-12-06 80.002857 NaN NaN
2013-12-09 80.918571 NaN NaN
2013-12-10 80.792857 NaN NaN
2013-12-11 80.194286 NaN NaN
2013-12-12 80.077143 NaN NaN
2013-12-13 79.204286 172.80 36.690
2013-12-16 79.642857 177.85 36.885
2013-12-17 79.284286 175.76 36.520
2013-12-18 78.681429 178.70 36.580
2013-12-19 77.780000 180.22 36.250
2013-12-20 78.431429 180.02 36.800
2013-12-23 81.441429 NaN NaN
2013-12-24 81.095714 NaN NaN
2013-12-26 80.557143 NaN NaN
2013-12-27 80.012857 NaN NaN
2013-12-30 79.217143 186.41 37.290
2013-12-31 80.145714 NaN NaN
2014-01-02 79.018571 185.53 37.160
2014-01-03 77.282857 186.64 36.910
也可以用词典的方式构建DataFrame
In [29]:

data = {}
for col in [‘foo’,’bar’,’baz’]:
for row in [‘a’,’b’,’c’,’d’]:
data.setdefault(col,{})[row] =np.random.randn()
data
Out[29]:
{‘bar': {‘a': -0.8332422805433652,
‘b': 0.5994500856951476,
‘c': 0.9537581460796728,
‘d': -0.49437981524535757},
‘baz': {‘a': 0.2766639013497691,
‘b': -0.8861177531221818,
‘c': 0.16701653134374714,
‘d': -1.8695537477196287},
‘foo': {‘a': 0.10167946157142055,
‘b': 0.31454296162293,
‘c': 1.8135929483933937,
‘d': 0.9831684422423665}}
In [30]:

DataFrame(data)
Out[30]:
bar baz foo
a -0.833242 0.276664 0.101679
b 0.599450 -0.886118 0.314543
c 0.953758 0.167017 1.813593
d -0.494380 -1.869554 0.983168
数据调整
接着上面取好的dfus接着往下做~
In [31]:

s1  = dfus[‘AAPL.US’][-20:]
s2  = dfus[‘AAPL.US’][-25:-10]
(s1, s2)
Out[31]:
(MDEntryDate
2013-12-05    81.128714
2013-12-06    80.002857
2013-12-09    80.918571
2013-12-10    80.792857
2013-12-11    80.194286
2013-12-12    80.077143
2013-12-13    79.204286
2013-12-16    79.642857
2013-12-17    79.284286
2013-12-18    78.681429
2013-12-19    77.780000
2013-12-20    78.431429
2013-12-23    81.441429
2013-12-24    81.095714
2013-12-26    80.557143
2013-12-27    80.012857
2013-12-30    79.217143
2013-12-31    80.145714
2014-01-02    79.018571
2014-01-03    77.282857
Name: AAPL.US, dtype: float64, MDEntryDate
2013-11-27    77.994286
2013-11-29    79.438571
2013-12-02    78.747143
2013-12-03    80.903143
2013-12-04    80.714286
2013-12-05    81.128714
2013-12-06    80.002857
2013-12-09    80.918571
2013-12-10    80.792857
2013-12-11    80.194286
2013-12-12    80.077143
2013-12-13    79.204286
2013-12-16    79.642857
2013-12-17    79.284286
2013-12-18    78.681429
Name: AAPL.US, dtype: float64)
有空值的时候,两个DataFrame相加,值依然为NaN,新的标签则取二者标签的并集
In [32]:

s1+s2
Out[32]:
MDEntryDate
2013-11-27           NaN
2013-11-29           NaN
2013-12-02           NaN
2013-12-03           NaN
2013-12-04           NaN
2013-12-05    162.257429
2013-12-06    160.005714
2013-12-09    161.837143
2013-12-10    161.585714
2013-12-11    160.388571
2013-12-12    160.154286
2013-12-13    158.408571
2013-12-16    159.285714
2013-12-17    158.568571
2013-12-18    157.362857
2013-12-19           NaN
2013-12-20           NaN
2013-12-23           NaN
2013-12-24           NaN
2013-12-26           NaN
2013-12-27           NaN
2013-12-30           NaN
2013-12-31           NaN
2014-01-02           NaN
2014-01-03           NaN
Name: AAPL.US, dtype: float64
输出取得的结果到ipython notebook的平台上,同名的文件一样会被覆盖哦,右键获得的foo.csv文件再点击 save link as即可下载到本地
In [33]:

(s1+s2).to_csv(‘foo.csv’)
取s1、s2交易,内连接
In [34]:

innerjoin= s1.align(s2,join = ‘inner’)
innerjoin
Out[34]:
(MDEntryDate
2013-12-05    81.128714
2013-12-06    80.002857
2013-12-09    80.918571
2013-12-10    80.792857
2013-12-11    80.194286
2013-12-12    80.077143
2013-12-13    79.204286
2013-12-16    79.642857
2013-12-17    79.284286
2013-12-18    78.681429
Name: AAPL.US, dtype: float64, MDEntryDate
2013-12-05    81.128714
2013-12-06    80.002857
2013-12-09    80.918571
2013-12-10    80.792857
2013-12-11    80.194286
2013-12-12    80.077143
2013-12-13    79.204286
2013-12-16    79.642857
2013-12-17    79.284286
2013-12-18    78.681429
Name: AAPL.US, dtype: float64)
取s1、s2并集,外连接
In [35]:

outerjoin = s1.align(s2,join=’outer’)
outerjoin
Out[35]:
(MDEntryDate
2013-11-27          NaN
2013-11-29          NaN
2013-12-02          NaN
2013-12-03          NaN
2013-12-04          NaN
2013-12-05    81.128714
2013-12-06    80.002857
2013-12-09    80.918571
2013-12-10    80.792857
2013-12-11    80.194286
2013-12-12    80.077143
2013-12-13    79.204286
2013-12-16    79.642857
2013-12-17    79.284286
2013-12-18    78.681429
2013-12-19    77.780000
2013-12-20    78.431429
2013-12-23    81.441429
2013-12-24    81.095714
2013-12-26    80.557143
2013-12-27    80.012857
2013-12-30    79.217143
2013-12-31    80.145714
2014-01-02    79.018571
2014-01-03    77.282857
Name: AAPL.US, dtype: float64, MDEntryDate
2013-11-27    77.994286
2013-11-29    79.438571
2013-12-02    78.747143
2013-12-03    80.903143
2013-12-04    80.714286
2013-12-05    81.128714
2013-12-06    80.002857
2013-12-09    80.918571
2013-12-10    80.792857
2013-12-11    80.194286
2013-12-12    80.077143
2013-12-13    79.204286
2013-12-16    79.642857
2013-12-17    79.284286
2013-12-18    78.681429
2013-12-19          NaN
2013-12-20          NaN
2013-12-23          NaN
2013-12-24          NaN
2013-12-26          NaN
2013-12-27          NaN
2013-12-30          NaN
2013-12-31          NaN
2014-01-02          NaN
2014-01-03          NaN
Name: AAPL.US, dtype: float64)
右连接
In [36]:

b,c =s1.align(s2,join =’right’)
b,c
Out[36]:
(MDEntryDate
2013-11-27          NaN
2013-11-29          NaN
2013-12-02          NaN
2013-12-03          NaN
2013-12-04          NaN
2013-12-05    81.128714
2013-12-06    80.002857
2013-12-09    80.918571
2013-12-10    80.792857
2013-12-11    80.194286
2013-12-12    80.077143
2013-12-13    79.204286
2013-12-16    79.642857
2013-12-17    79.284286
2013-12-18    78.681429
Name: AAPL.US, dtype: float64, MDEntryDate
2013-11-27    77.994286
2013-11-29    79.438571
2013-12-02    78.747143
2013-12-03    80.903143
2013-12-04    80.714286
2013-12-05    81.128714
2013-12-06    80.002857
2013-12-09    80.918571
2013-12-10    80.792857
2013-12-11    80.194286
2013-12-12    80.077143
2013-12-13    79.204286
2013-12-16    79.642857
2013-12-17    79.284286
2013-12-18    78.681429
Name: AAPL.US, dtype: float64)
步长为2取dfus中的IBM.US和MSFT.US两列
In [37]:

dfus2 = dfus.ix[::2,[‘IBM.US’,’MSFT.US’]]
dfus2
Out[37]:
IBM.US MSFT.US
MDEntryDate
2013-11-20 185.19 37.08
2013-11-22 181.30 37.57
2013-11-26 177.31 37.35
2013-11-29 179.68 38.13
2013-12-03 NaN NaN
2013-12-05 NaN NaN
2013-12-09 NaN NaN
2013-12-11 NaN NaN
2013-12-13 172.80 36.69
2013-12-17 175.76 36.52
2013-12-19 180.22 36.25
2013-12-23 NaN NaN
2013-12-26 NaN NaN
2013-12-30 186.41 37.29
2014-01-02 185.53 37.16
两者“相加”,在内容上取交集,在范围上取并集,相加后IBM和MSFT两列也只有九个非NaN,和dfus一致 这两列其他的NaN是由之前的“赋个空值”操作所致
In [38]:

dfus+dfus2
Out[38]:
AAPL.US IBM.US MS