python如何下载pandas、时间延长_pandas,python - 如何选择时间序列中的特定时间...

bd96500e110b49cbb3cd949968f18be7.png

I worked now for quite some time using python and pandas for analysing a set of hourly data and find it quite nice (Coming from Matlab.)

Now I am kind of stuck. I created my DataFrame like that:

SamplingRateMinutes=60

index = DateRange(initialTime,finalTime, offset=datetools.Minute(SamplingRateMinutes))

ts=DataFrame(data, index=index)

What I want to do now is to select the Data for all days at the hours 10 to 13 and 20-23 to use the data for further calculations.

So far I sliced the data using

selectedData=ts[begin:end]

And I am sure to get some kind of dirty looping to select the data needed. But there must be a more elegant way to index exacly what I want. I am sure this is a common problem and the solution in pseudocode should look somewhat like that:

myIndex=ts.index[10<=ts.index.hour<=13 or 20<=ts.index.hour<=23]

selectedData=ts[myIndex]

To mention I am an engineer and no programer :) ... yet

解决方案

Here's an example that does what you want:

In [32]: from datetime import datetime as dt

In [33]: dr = p.DateRange(dt(2009,1,1),dt(2010,12,31), offset=p.datetools.Hour())

In [34]: hr = dr.map(lambda x: x.hour)

In [35]: dt = p.DataFrame(rand(len(dr),2), dr)

In [36]: dt

Out[36]:

DateRange: 17497 entries, 2009-01-01 00:00:00 to 2010-12-31 00:00:00

offset: <1 Hour>

Data columns:

0 17497 non-null values

1 17497 non-null values

dtypes: float64(2)

In [37]: dt[(hr >= 10) & (hr <=16)]

Out[37]:

Index: 5103 entries, 2009-01-01 10:00:00 to 2010-12-30 16:00:00

Data columns:

0 5103 non-null values

1 5103 non-null values

dtypes: float64(2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值