序列 pd.Series
(附件为代码运行信息)
1、创建序列
序列是带标签索引的一维数组
In [88]:
import pandas as pd
创建序列
In [41]:
a = pd.Series([9.1,9.5,10.0,11]) #未指定索引,索引默认是 0,1,2,3... a
Out[41]:
0 9.1 1 9.5 2 10.0 3 11.0 dtype: float64
可以在创建序列时指定索引
In [42]:
a = pd.Series([9.1,9.5,10.0,11],index=['a','b','c','d']) a
Out[42]:
a 9.1 b 9.5 c 10.0 d 11.0 dtype: float64
可以为创建的序列指定名称
In [43]:
a = pd.Series([9.1,9.5,10.0,11],index=['a','b','c','d'],name='t') a
Out[43]:
a 9.1 b 9.5 c 10.0 d 11.0 Name: t, dtype: float64
2、时间索引
pd.Series的索引可以是数字、字符串,也可以是时间 使用pd.to_datetime()函数 讲字符串序列转换为时间戳索引。默认是时间为世界时(UTC)
UTC -> BJT: +8h BJT -> UTC: -8h
In [44]:
a = pd.Series([9.1,9.5,10.0,11],index=pd.to_datetime(['2010-01-01','2010-01-02','2010-01-03','2010-01-04']),name='t') a
Out[44]:
2010-01-01 9.1 2010-01-02 9.5 2010-01-03 10.0 2010-01-04 11.0 Name: t, dtype: float64
假设上述设置的时间的BJT,先需要转化为UTC。如下: 使用datetime.timedelta() 函数
In [45]:
import datetime as dt time_index = pd.to_datetime(['2010-01-01','2010-01-02','2010-01-03','2010-01-04']) time_index = time_index - dt.timedelta(hours = 8) a = pd.Series([9.1,9.5,10.0,11],index=time_index,name='t') a
Out[45]:
2009-12-31 16:00:00 9.1 2010-01-01 16:00:00 9.5 2010-01-02 16:00:00 10.0 2010-01-03 16:00:00 11.0 Name: t, dtype: float64
3、pd.Series对象的算术运算
In [46]:
a = pd.Series([9.1,9.5,10.0,11],index=['a','b','c','d