在特定时间范围内选择对datetime64 [ns]类型的观察 [英] selecting observation of datetime64[ns] type in particular time range

查看:331
本文介绍了在特定时间范围内选择对datetime64 [ns]类型的观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据帧(dfnew),其中一列(时间戳)是datetime64[ns]类型.现在,我想看看在特定时间范围内有多少个观测值,可以说10:00:00到12:00:00.

I have a pandas dataframe(dfnew) in which one column(timestamp) is of datetime64[ns] type. Now I want to see how many observations are in particular time range lets say 10:00:00 to 12:00:00.

    dfnew['timestamp'] = dfnew['timestamp'].astype('datetime64[ns]')
    dfnew['timestamp]
0    2013-12-19 09:03:21.223000
1    2013-12-19 11:34:23.037000
2    2013-12-19 11:34:23.050000
3    2013-12-19 11:34:23.067000
4    2013-12-19 11:34:23.067000
5    2013-12-19 11:34:23.067000
6    2013-12-19 11:34:23.067000
7    2013-12-19 11:34:23.067000
8    2013-12-19 11:34:23.067000
9    2013-12-19 11:34:23.080000
10   2013-12-19 11:34:23.080000
11   2013-12-19 11:34:23.080000
12   2013-12-19 11:34:23.080000
13   2013-12-19 11:34:23.080000
14   2013-12-19 11:34:23.080000
15   2013-12-19 11:34:23.097000
16   2013-12-19 11:34:23.097000
17   2013-12-19 11:34:23.097000
18   2013-12-19 11:34:23.097000
19   2013-12-19 11:34:23.097000
Name: timestamp

    dfnew['Time']=dfnew['timestamp'].map(Timestamp.time)
    t1 = datetime.time(10, 0, 0)
    t2 = datetime.time(12, 0, 0)
    print len(dfnew[t1<dfnew["Time"]<t2])

这会产生错误 TypeError:无法将datetime.time与Series进行比较. 我是熊猫数据框的新手.我想我在这里犯了一个非常愚蠢的错误.感谢任何帮助.

This produce an error TypeError: can't compare datetime.time to Series. I am new to pandas dataframe. I guess I am making a very silly mistake here.Any help appreciated.

推荐答案

您可以使用DatetimeIndex

You can use the DatetimeIndex indexer_between_time method, so a trick here to make use of it is to pass the Series / column to the DatetimeIndex constructor:

from datetime import time

# s is your datetime64 column

In [11]: pd.DatetimeIndex(s).indexer_between_time(time(10), time(12))
Out[11]: 
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])

这将获得10到12(含*)之间的时间位置,因此请使用iloc进行过滤:

This gets the position of the times between 10 and 12 (inclusive*), so use iloc to filter:

In [12]: s.iloc[pd.DatetimeIndex(s).indexer_between_time(time(10), time(12))]
Out[12]: 
1    2013-12-19 11:34:23.037000
2    2013-12-19 11:34:23.050000
3    2013-12-19 11:34:23.067000
4    2013-12-19 11:34:23.067000
5    2013-12-19 11:34:23.067000
6    2013-12-19 11:34:23.067000
7    2013-12-19 11:34:23.067000
8    2013-12-19 11:34:23.067000
9    2013-12-19 11:34:23.080000
10   2013-12-19 11:34:23.080000
11   2013-12-19 11:34:23.080000
12   2013-12-19 11:34:23.080000
13   2013-12-19 11:34:23.080000
14   2013-12-19 11:34:23.080000
15   2013-12-19 11:34:23.097000
16   2013-12-19 11:34:23.097000
17   2013-12-19 11:34:23.097000
18   2013-12-19 11:34:23.097000
19   2013-12-19 11:34:23.097000
Name: timestamp, dtype: datetime64[ns]

* include_startinclude_endindexer_between_time的可选布尔参数.

* include_start and include_end are optional boolean arguments of indexer_between_time.

这篇关于在特定时间范围内选择对datetime64 [ns]类型的观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆