Exclude between time in pandas
<p>I know that you can select data from <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.html" rel="nofollow">pandas.DatetimeIndex</a> using <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.between_time.html" rel="nofollow">pandas.DataFrame.between_time</a>. Is there a convenient way to exclude between two times in <code>pandas</code>?</p>

<p>For example, to exclude data between 16:00 and 17:00, I am currently doing the following.</p>

<pre><code>In [1]: import pandas as pd
        import numpy as np

In [2]: df = pd.DataFrame(np.random.randn(24 * 60 + 1, 2), columns=list("AB"), index=pd.date_range(start="20161013 00:00:00", freq="1T", periods=24 * 60 +1))

In [3]: idx = df.index.hour == 16

In [4]: df = df[~idx]

In [5]: df.between_time("16:00", "17:00")
Out[5]: 
                            A         B
2016-10-13 17:00:00 -0.745892  1.832912
</code></pre>

<p><strong>EDIT</strong></p>

<p>I have been able to use this:</p>

<pre><code>In[41]:df2 = df.ix[np.setdiff1d(df.index, df.between_time("16:00", "17:00").index)]


In[42]:df2.between_time("15:59", "17:01")
Out[42]: 
                            A         B
2016-10-13 15:59:00  1.190678  0.783776
2016-10-13 17:01:00 -0.590931 -1.059962
</code></pre>

<p>Is there a better way?</p>

<p>You can combine <code>between_time</code> with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html" rel="nofollow"><code>drop</code></a>:</p>

<pre><code>df2 = df.drop(df.between_time("16:00", "17:00").index)
</code></pre>

python pandas numpy