Index a Python DataFrame with two Conditions
Question: I'm trying to get a subset of a DataFrame based on two conditions.

Here my simplified example:

    
    
    import pandas as pd
    test = pd.DataFrame(np.ones(48),index = pd.date_range('2015-01-01',periods = 48, freq = '1800S'))
    

I'd now like to get all values that are in the timerange t> 08:00 and t<22:00,
thus I tried:

    
    
    result = test[test.index.hour>8 & test.index.hour<22]
    

I then get the ValueError that the truth value of an array with more than one
element is ambiguous, use a.any() or a.all() - and here I'm out of luck... .


Answer: There are 2 simple solutions :

  * First: enclose your conditions into braces like `(test.index.hour > 8) & (test.index.hour<22)` due to operator & precedence
  * Second : use [the query function](http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.query.html)

