pandas 在时间范围之外放行 [英] Pandas Drop Rows Outside of Time Range
问题描述
我正在尝试遍历DataFrame索引中的每一行,并删除所有不在特定时间之间的行.
I am trying to go through every row in a DataFrame index and remove all rows that are not between a certain time.
我一直在寻找解决方案,但是没有一个将日期和时间分开,我要做的就是删除时间范围之外的行.
I have been looking for solutions but none of them separate the Date from the Time, and all I want to do is drop the rows that are outside of a Time range.
推荐答案
You can use the between_time
function directly:
ts.between_time(datetime.time(18), datetime.time(9), include_start=False, include_end=False)
原始答案:
您可以使用indexer_between_time
Index
方法.
例如,要在上午9点至下午6点之间(包括以下时间)包括:
For example, to include those times between 9am and 6pm (inclusive):
ts.ix[ts.index.indexer_between_time(datetime.time(9), datetime.time(18))]
做相反的事情,排除在下午6点至上午9点之间(排他性):
to do the opposite and exclude those times between 6pm and 9am (exclusive):
ts.ix[ts.index.indexer_between_time(datetime.time(18), datetime.time(9),
include_start=False, include_end=False)]
注意:indexer_between_time
的参数include_start
和include_end
在默认情况下为True
,将include_start
设置为False
表示日期时间的时间段正好是start_time
(第一个参数),在这种情况下为6pm.
Note: indexer_between_time
's arguments include_start
and include_end
are by default True
, setting include_start
to False
means that datetimes whose time-part is precisely start_time
(the first argument), in this case 6pm, will not be included.
示例:
In [1]: rng = pd.date_range('1/1/2000', periods=24, freq='H')
In [2]: ts = pd.Series(pd.np.random.randn(len(rng)), index=rng)
In [3]: ts.ix[ts.index.indexer_between_time(datetime.time(10), datetime.time(14))]
Out[3]:
2000-01-01 10:00:00 1.312561
2000-01-01 11:00:00 -1.308502
2000-01-01 12:00:00 -0.515339
2000-01-01 13:00:00 1.536540
2000-01-01 14:00:00 0.108617
注意:相同的语法(使用 ix
)适用于DataFrame:
Note: the same syntax (using ix
) works for a DataFrame:
In [4]: df = pd.DataFrame(ts)
In [5]: df.ix[df.index.indexer_between_time(datetime.time(10), datetime.time(14))]
Out[5]:
0
2000-01-03 10:00:00 1.312561
2000-01-03 11:00:00 -1.308502
2000-01-03 12:00:00 -0.515339
2000-01-03 13:00:00 1.536540
2000-01-03 14:00:00 0.108617
这篇关于 pandas 在时间范围之外放行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!