Series:一种类似于一维数组的对象,是由一组数据(各种NumPy数据类型)以及一组与之相关的数据标签(即索引)组成。仅由一组数据也可产生简单的Series对象。注意:Series中的索引值是可以重复的。
pandas引入约定
from pandas import Series,
Series基本函数与DataFrame基本函基本一致,详细可参考DataFrame详解链接,官方文档pandas.Series链接
import numpy as np
import pandas as pd
dates = pd.date_range('20130101', periods=6)
s = pd.Series([1, 3, 5, np.nan, 6, 8], index=dates).shift(2) # Series最后两行的值为空,为空的两行放在s的前两行
s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])
print("s.str.lower():\n",s.str.lower())
rng = pd.date_range('1/1/2012', periods=5, freq='S')
print("rng:\n",rng)
ts = pd.Series(np.random.randint(0, 5, len(rng)), index=rng)
print("ts:\n",ts)
print("ts.resample('5Min').sum():\n",ts.resample('5Min').sum())
rng = pd.date_range('3/6/2012 00:00', periods=5, freq='D')
np.random.seed(0)
ts1 = pd.Series(np.random.randn(len(rng)), rng)
print("ts1:\n",ts1)
ts1_utc = ts1.tz_localize('UTC')
print("ts1_utc:\n",ts1_utc)
print("ts1_utc.tz_convert('US/Eastern'):\n",ts1_utc.tz_convert('US/Eastern'))
rng2 = pd.date_range('1/1/2012', periods=5, freq='M')
np.random.seed(0)
ts2 = pd.Series(np.random.randn(len(rng2)), index=rng2)
print("ts2:\n",ts2)
ps = ts2.to_period()
print("ps.to_timestamp():\n",ps.to_timestamp())
prng = pd.period_range('1990Q1', '2000Q4', freq='Q-NOV')
print("prig:\n",prng)
ts3 = pd.Series(np.random.randn(len(prng)), prng)
print("ts3:\n",ts3)
ts3.index = (prng.asfreq('M', 'e') + 1).asfreq('H', 's') + 9
print("ts3.index:\n",ts3.index)
print("ts3.head():\n",ts3.head())