利用python进行数据分析 笔记_《利用python进行数据分析》读书笔记--第十章 时间序列(一)...

这篇博客介绍了Pandas库中对时间序列数据的操作,包括通过日期索引数据、切片选择、时间戳范围查询以及DataFrame的类似操作。示例展示了如何使用日期字符串、datetime对象和Timestamp来访问和筛选时间序列数据。
摘要由CSDN通过智能技术生成

stamp = ts.index[2]print ts[stamp],'\n'

#还有更方便的用法,传入可以被解释为日期的字符串

print ts['1/10/2011']print ts['20110110'],'\n'

#对于较长的时间序列,只需传入“年”或“年月”即可轻松选取数据切片

long_ts = Series(np.random.randn(1000),

index= pd.date_range('1/1/2000',periods = 1000))#-*- coding:utf-8 -*-

importnumpy as npimportpandas as pdimportmatplotlib.pyplot as pltimportdatetime as dtfrom pandas importSeries,DataFramefrom datetime importdatetimefrom dateutil.parser importparseimporttimeprint long_ts,'\n'

print long_ts['2001'],'\n'

print long_ts['2001-05'],'\n'

#通过日期进行切片的方式只对规则Series有效:

print ts[datetime(2011,1,7):],'\n'

#由于大部分时间序列数据都是按照时间先后排序的,因此你可以用不存在于该时间序列中的时间戳对其进行切片(即范围查询)#就是说,本来1/6/2011不在index中,却可以用来当作范围

print ts['1/6/2011':'1/11/2011'],'\n' #这里可以传入字符串日期、datetime或者Timestamp

print 'This is time and localtime'

print "time.time(): %f" %time.time()printtime.localtime( time.time() )printtime.asctime( time.localtime(time.time()) )

ltime=time.localtime(int(time.time())) #time.time()不能直接运用strftime进行转换

print time.strftime("%Y-%m-%d %H:%M:%S", ltime)#time asctime() 函数接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"

print 'over','\n'

#还有一个等价方法截取两个日期之间的TimeSeries.

print ts.truncate(after = '1/9/2011'),'\n'

#上面这些对DataFrame也有效

dates = pd.date_range('1/1/2000',periods = 100,freq = 'W-WED') #这里的freq是按照星期进行增加

long_df = DataFrame(np.random.randn(100,4),index = dates,columns = ['Colorado','Texas','New York','Ohio'])print long_df.ix['2001-05']

>>>

0.0751316698811

-0.622706612554

-0.622706612554

2000-01-01 -1.646726

2000-01-02 1.531423

2000-01-03 0.251503

2000-01-04 0.938951

2000-01-05 0.647967

2000-01-06 0.696173

2000-01-07 -1.372519

2000-01-08 -1.398277

2000-01-09 -0.679975

2000-01-10 -0.801375

2000-01-11 -0.241165

2000-01-12 -0.332811

2000-01-13 -0.337774

2000-01-14 0.826756

2000-01-15 -0.279239

...

2002-09-12 -0.097634

2002-09-13 2.222456

2002-09-14 0.042517

2002-09-15 0.266974

2002-09-16 0.038329

2002-09-17 -1.524744

2002-09-18 1.476706

2002-09-19 0.108336

2002-09-20 0.016759

2002-09-21 -0.072676

2002-09-22 -0.960545

2002-09-23 0.520699

2002-09-24 -1.188202

2002-09-25 1.669166

2002-09-26 -0.043997

Freq: D, Length: 1000

2001-01-01 -0.168866

2001-01-02 -0.273377

2001-01-03 0.094258

2001-01-04 -0.979666

2001-01-05 0.947706

2001-01-06 0.666709

2001-01-07 0.451145

2001-01-08 -0.301992

2001-01-09 0.272385

2001-01-10 -0.255775

2001-01-11 -0.321916

2001-01-12 1.894119

2001-01-13 0.582272

2001-01-14 -1.102707

2001-01-15 0.019423

...

2001-12-17 -0.243563

2001-12-18 1.757564

2001-12-19 -0.145106

2001-12-20 -0.579629

2001-12-21 -0.431069

2001-12-22 0.480805

2001-12-23 -0.651905

2001-12-24 0.702051

2001-12-25 -0.384549

2001-12-26 -1.077664

2001-12-27 -0.972768

2001-12-28 1.001220

2001-12-29 0.418016

2001-12-30 0.567361

2001-12-31 -0.811610

Freq: D, Length: 365

2001-05-01 -0.071521

2001-05-02 0.402344

2001-05-03 -0.568929

2001-05-04 0.227754

2001-05-05 0.194631

2001-05-06 -0.407669

2001-05-07 -1.407606

2001-05-08 -0.804147

2001-05-09 0.050445

2001-05-10 -0.604275

2001-05-11 0.270760

2001-05-12 0.000804

2001-05-13 -0.348938

2001-05-14 -1.626158

2001-05-15 0.084629

2001-05-16 -0.376655

2001-05-17 1.913789

2001-05-18 2.497594

2001-05-19 0.818446

2001-05-20 0.067115

2001-05-21 -0.993827

2001-05-22 0.940616

2001-05-23 -0.951763

2001-05-24 -0.806228

2001-05-25 0.441872

2001-05-26 0.067010

2001-05-27 -1.903360

2001-05-28 -0.400990

2001-05-29 0.257146

2001-05-30 0.785503

2001-05-31 -1.129024

Freq: D

2011-01-07 0.075132

2011-01-08 -0.985630

2011-01-10 -0.622707

2011-01-12 -1.356095

2011-01-07 0.075132

2011-01-08 -0.985630

2011-01-10 -0.622707

This is time and localtime

time.time(): 1450362054.149000

time.struct_time(tm_year=2015, tm_mon=12, tm_mday=17, tm_hour=22, tm_min=20, tm_sec=54, tm_wday=3, tm_yday=351, tm_isdst=0)

Thu Dec 17 22:20:54 2015

2015-12-17 22:20:54

over

2011-01-02 -0.772858

2011-01-05 -0.908074

2011-01-07 0.075132

2011-01-08 -0.985630

Colorado Texas New York Ohio

2001-05-02 0.303341 0.026978 -0.036389 0.463034

2001-05-09 -1.573227 -0.283074 -0.882382 -1.207936

2001-05-16 1.520804 -0.838297 0.725690 1.240092

2001-05-23 1.297194 -0.516198 -0.022075 -0.876630

2001-05-30 -1.629426 1.022547 -0.131823 -0.621269

[Finished in 0.7s]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值