Python 熊猫在时间范围外放置行
我正在尝试遍历数据帧索引中的每一行,并删除不在某个时间间隔内的所有行 我一直在寻找解决方案,但没有一个能将日期和时间分开,我只想删除超出时间范围的行。您可以直接使用该函数:Python 熊猫在时间范围外放置行,python,pandas,Python,Pandas,我正在尝试遍历数据帧索引中的每一行,并删除不在某个时间间隔内的所有行 我一直在寻找解决方案,但没有一个能将日期和时间分开,我只想删除超出时间范围的行。您可以直接使用该函数: ts.between_time(datetime.time(18), datetime.time(9), include_start=False, include_end=False) 原始答复: 您可以使用indexer\u-between\u-timeIndex方法 例如,包括上午9点至下午6点(含)之间的时间: 相
ts.between_time(datetime.time(18), datetime.time(9), include_start=False, include_end=False)
原始答复: 您可以使用
indexer\u-between\u-time
Index
方法
例如,包括上午9点至下午6点(含)之间的时间:
相反,不包括下午6点到上午9点之间的时间(不包括):
注意:include\u start
和include\u end
之间的索引器在默认情况下为True
,将include\u start
设置为False
意味着时间部分精确为start\u time
(第一个参数)的日期时间(在本例中为6pm)将不包括在内
例如:
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
注意:相同的语法(使用)适用于数据帧:
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
您也可以这样做:rng = pd.date_range('1/1/2000', periods=24, freq='H') ts = pd.Series(pd.np.random.randn(len(rng)), index=rng) ts.ix[datetime.time(10):datetime.time(14)] Out[4]: 2000-01-01 10:00:00 -0.363420 2000-01-01 11:00:00 -0.979251 2000-01-01 12:00:00 -0.896648 2000-01-01 13:00:00 -0.051159 2000-01-01 14:00:00 -0.449192 Freq: H, dtype: float64 rng=pd.日期范围('1/1/2000',周期=24,频率=H') ts=pd系列(pd.np.random.randn(len(rng)),索引=rng) ts.ix[日期时间(10):日期时间(14)] 出[4]: 2000-01-01 10:00:00 -0.363420 2000-01-01 11:00:00 -0.979251 2000-01-01 12:00:00 -0.896648 2000-01-01 13:00:00 -0.051159 2000-01-01 14:00:00 -0.449192 频率:H,数据类型:float64
数据帧的工作方式也一样。如果您不是在寻找一个内置函数,那么@AndyHayden所说的
interference\u time
是一种方法
对于那些寻找就地版本的人,我在这里留下以下评论:
如果您希望将选择置于适当位置(直接应用于pandas.DataFramedf
),可以使用:
df.drop(numpy.setdiff1d(df.index, df.between_time(start_time=dt.time(hours=8,minute=0),
end_time=dt.time(hours=18, minute=0),
include_start=True,
include_end=False).index)
,inplace=True)
其中,dt
是日期时间
这里我们找到了不在时间范围内的索引,并将它们放在适当的位置。对于一个数据帧,我必须这样做:df.reindex(df.index[df.index.indexer_-between_-time(time(9),time(18)))@Jeff——我想你必须使用
dataframe.ix
likedf.ix[df.index.indexer_-between_-time()
在数据框架上
@和yhayden——我的第一个想法也是,它的工作原理相同,但我认为DataFrame试图将其应用于列(没有ix)@root我认为将ix与Series一起使用也是一种很好的做法,这样语法就完全相同了,所以我更新了第一部分。@AndyHayden--为了使其完整,您还可以添加该方法还需要include\u start
和include\u end
参数,这不是很重要,但有时可能会变得有用:)
rng = pd.date_range('1/1/2000', periods=24, freq='H')
ts = pd.Series(pd.np.random.randn(len(rng)), index=rng)
ts.ix[datetime.time(10):datetime.time(14)]
Out[4]:
2000-01-01 10:00:00 -0.363420
2000-01-01 11:00:00 -0.979251
2000-01-01 12:00:00 -0.896648
2000-01-01 13:00:00 -0.051159
2000-01-01 14:00:00 -0.449192
Freq: H, dtype: float64
df.drop(numpy.setdiff1d(df.index, df.between_time(start_time=dt.time(hours=8,minute=0),
end_time=dt.time(hours=18, minute=0),
include_start=True,
include_end=False).index)
,inplace=True)