如何从以Datetime为索引的数组中删除指定时间段内的元素



我有一个具有DateTime索引的数组。例如:

import scipy.stats as st
import pandas as pd

n = 50000
dur = st.expon.rvs(loc=0, scale=1, size=n)
sgn = st.norm.rvs(0,1, size=n)
t = dur.cumsum()
df = pd.DataFrame({'Date_Time':t,'sgn':sgn})
df['Date_Time']=pd.to_datetime(df.Date_Time, unit='s')
df=df.set_index(['Date_Time'])

我想删除sgn的第一个5Min(仅举个例子)。是否有快捷方式,我的意思是不使用精确的索引值,如:

sgn = df.sgn['5Min':]

可以计算最小值后5分钟的值:

out = df.loc[df.index.min()+pd.Timedelta('5min'):]

输出:

sgn
Date_Time                              
1970-01-01 00:05:02.622590737  0.270888
1970-01-01 00:05:05.008655097 -0.095408
1970-01-01 00:05:06.421593085  1.235543
1970-01-01 00:05:06.641397682 -0.290745
1970-01-01 00:05:06.725335705  0.650911
...                                 ...
1970-01-01 13:55:42.587687617  0.141481
1970-01-01 13:55:47.584010498 -0.331172
1970-01-01 13:55:48.612765970 -0.947417
1970-01-01 13:55:48.724715688  0.832305
1970-01-01 13:55:49.979336424  1.477405
[49687 rows x 1 columns]

相关内容

  • 没有找到相关文章

最新更新