pandas subset using sliced boolean index
<p>code to make test data:</p>

<pre><code>import pandas as pd
import numpy as np

testdf = {'date': range(10),
      'event': ['A', 'A', np.nan, 'B', 'B', 'A', 'B', np.nan, 'A', 'B'],
      'id': [1] * 7 + [2] * 3}
testdf = pd.DataFrame(testdf)

print(testdf)
</code></pre>

<p>gives </p>

<pre><code>    date event  id
0     0     A   1
1     1     A   1
2     2   NaN   1
3     3     B   1
4     4     B   1
5     5     A   1
6     6     B   1
7     7   NaN   2
8     8     A   2
9     9     B   2
</code></pre>

<p>subset testdf</p>

<pre><code>df_sub = testdf.loc[testdf.event == 'A',:]
print(df_sub)
    date event  id
0     0     A   1
1     1     A   1
5     5     A   1
8     8     A   2
</code></pre>

<p><strong>(Note: not re-indexed)</strong></p>

<p>create conditional boolean index</p>

<pre><code>bool_sliced_idx1 = df_sub.date &lt; 4
bool_sliced_idx2 = (df_sub.date &gt; 4) &amp; (df_sub.date &lt; 6)
</code></pre>

<p>I want to insert conditional values using this new index in original df, like</p>

<pre><code>dftest[ 'new_column'] = np.nan
dftest.loc[bool_sliced_idx1, 'new_column'] = 'new_conditional_value'
</code></pre>

<p>which obviously (now) gives error:</p>

<pre><code>pandas.core.indexing.IndexingError: Unalignable boolean Series key provided
</code></pre>

<p><code>bool_sliced_idx1</code> looks like</p>

<pre><code>&gt;&gt;&gt; print(bool_sliced_idx1)
0     True
1     True
5    False
8    False
Name: date, dtype: bool
</code></pre>

<p>I tried <code>testdf.ix[(bool_sliced_idx1==True).index,:]</code>, but that doesn't work because</p>

<pre><code>&gt;&gt;&gt; (bool_sliced_idx1==True).index
Int64Index([0, 1, 5, 8], dtype='int64')
</code></pre>

<p>IIUC, you can just combine all of your conditions at once, instead of trying to chain them.  For example, <code>df_sub.date &lt; 4</code> is really just <code>(testdf.event == 'A') &amp; (testdf.date &lt; 4)</code>.  So, you could do something like:</p>

<pre><code># Create the conditions.
cond1 = (testdf.event == 'A') &amp; (testdf.date &lt; 4)
cond2 = (testdf.event == 'A') &amp; (testdf.date.between(4, 6, inclusive=False))

# Make the assignments.
testdf.loc[cond1, 'new_col'] = 'foo'
testdf.loc[cond2, 'new_col'] = 'bar'
</code></pre>

<p>Which would give you:</p>

<pre><code>   date event  id new_col
0     0     A   1     foo
1     1     A   1     foo
2     2   NaN   1     NaN
3     3     B   1     NaN
4     4     B   1     NaN
5     5     A   1     bar
6     6     B   1     NaN
7     7   NaN   2     NaN
8     8     A   2     NaN
9     9     B   2     NaN
</code></pre>

python pandas indexing dataframe slice