How to remove the elements in a specified period, from a Datetime indexed array

Question:

I have an array with DateTime index. For example:

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'])

I want to remove the first 5Min (just for example) of sgn. Is there shortcut for this, I mean without using exact index values, like:

sgn = df.sgn['5Min':]
Asked By: Erdem Şen

||

Answers:

You can compute the value that is 5min after the min:

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

Output:

                                    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]
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.