selection rows with special conditions
<p>I have this dataframe : </p>

<pre><code>TIMESTAMP equipmeent1 equipement2 class_energy
    2016-05-10 04:30:00 107 0 high
    2016-05-10 04:40:00 100 90 medium
    2016-05-10 04:50:00 106 0 low
    2016-05-10 05:00:00 107 0 high
</code></pre>

<p>I  try to select rows with special condition : </p>

<pre><code>x.loc[x['class_energy'] == 'high',  x['TIMESTAMP'] &gt; 2016-05-10 04:30:00 04:10:00,x['TIMESTAMP'] &lt; 2016-05-10 05:00:00 ]
</code></pre>

<p>But I get this problem :</p>

<blockquote>
<pre><code>IndexingError                             Traceback (most recent call last)
&lt;ipython-input-241-b47c8396bb9a&gt; in &lt;module&gt;()
----&gt; 1 x.loc[x['class_energy'] == 'high', x['PERIODE_TARIF'] =='HP']

C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\indexing.py
</code></pre>
  
  <p>in <strong>getitem</strong>(self, key)
         1292 
         1293         if type(key) is tuple:
      -> 1294             return self._getitem_tuple(key)
         1295         else:
         1296             return self._getitem_axis(key, axis=0)</p>

<pre><code>C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\indexing.py
</code></pre>
  
  <p>in _getitem_tuple(self, tup)
          802                 continue
          803 
      --> 804             retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
          805 
          806         return retval</p>

<pre><code>C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\indexing.py
</code></pre>
  
  <p>in _getitem_axis(self, key, axis)
         1437             return self._get_slice_axis(key, axis=axis)
         1438         elif is_bool_indexer(key):
      -> 1439             return self._getbool_axis(key, axis=axis)
         1440         elif is_list_like_indexer(key):
         1441 </p>

<pre><code>C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\indexing.py
</code></pre>
  
  <p>in _getbool_axis(self, key, axis)
         1301     def _getbool_axis(self, key, axis=0):
         1302         labels = self.obj._get_axis(axis)
      -> 1303         key = check_bool_indexer(labels, key)
         1304         inds, = key.nonzero()
         1305         try:</p>

<pre><code>C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\core\indexing.py
</code></pre>
  
  <p>in check_bool_indexer(ax, key)
         1799         mask = com.isnull(result._values)
         1800         if mask.any():
      -> 1801             raise IndexingError('Unalignable boolean Series key provided')
         1802 
         1803         result = result.astype(bool)._values</p>

<pre><code>IndexingError: Unalignable boolean Series key provided
</code></pre>
</blockquote>

<p>You need to and the conditions using <code>&amp;</code> and use parentheses:</p>

<pre><code>x.loc[(x['class_energy'] == 'high') &amp; (x['TIMESTAMP'] &gt; '2016-05-10 04:30:00') &amp; (x['TIMESTAMP'] &lt; '2016-05-10 05:00:00') ]
</code></pre>

<p>It's unclear what you're intending by randomly including <code>04:10:00</code> in your original code</p>

<p>you must use <code>&amp;</code> instead of <code>and</code> as we are comparing arrays of values, due to operator precedence the conditions need to be enclosed in parentheses also</p>

<p>What you did was just separate each condition with a <code>,</code> which is meaningless here and caused an evaluation error as it treated your args as a tuple</p>

<p>Also your error <code>x.loc[x['class_energy'] == 'high', x['PERIODE_TARIF'] =='HP']</code> doesn't match your posted code, if you wanted to use these 2 conditions:</p>

<pre><code>x.loc[(x['class_energy'] == 'high') &amp; (x['PERIODE_TARIF'] =='HP')]
</code></pre>

<p>should work</p>

python pandas