title: How to use .loc with groupby and two conditions in pandas
body: <p>I asked a similar question <a href="https://stackoverflow.com/questions/49144154/how-to-use-loc-with-multiple-conditions-and-groupby/49144209#49144209">here</a>, but I want to expand on this question because I'm asked to do something a little different where I cannot use .duplicates()</p>
  
  <p>I have a df that's grouped by 'Key'. I want to flag any row within a group where the discharge date matches the admit date AND between those rows, the row with the discharge date has a num1 value in the range of 5-12. </p>
  
  <pre><code>df =  pd.DataFrame({'Key': ['10003', '10003', '10003', '10003', '10003','10003','10034', '10034'], 
     'Num1': [12,13,13,13,12,13,15,12],
     'Num2': [121,122,122,124,125,126,127,128],
    'admit': [20120506, 20120508, 20121010,20121010,20121010,20121110,20120520,20120520],  'discharge': [20120508, 20120510, 20121012,20121016,20121023,20121111,20120520,20120520]})
  df['admit'] = pd.to_datetime(df['admit'], format='%Y%m%d')
  df['discharge'] = pd.to_datetime(df['discharge'], format='%Y%m%d')
  </code></pre>
  
  <p>initial df</p>
  
  <pre><code>    Key     Num1    Num2    admit       discharge
  0   10003   12      121     2012-05-06  2012-05-08
  1   10003   13      122     2012-05-08  2012-05-10
  2   10003   13      122     2012-10-10  2012-10-12
  3   10003   13      124     2012-10-10  2012-10-16
  4   10003   12      125     2012-10-10  2012-10-23
  5   10003   13      126     2012-11-10  2012-11-11
  6   10034   15      127     2012-05-20  2012-05-20
  7   10034   12      128     2012-05-20  2012-05-20
  </code></pre>
  
  <p>final df</p>
  
  <pre><code>    Key     Num1    Num2    admit       discharge   flag
  0   10003   12      121     2012-05-06  2012-05-08  1
  1   10003   13      122     2012-05-08  2012-05-10  1
  2   10003   13      122     2012-10-10  2012-10-12  0
  3   10003   13      124     2012-10-10  2012-10-16  0
  4   10003   12      125     2012-10-10  2012-10-23  0
  5   10003   13      126     2012-11-10  2012-11-11  0
  6   10034   15      127     2012-05-20  2012-05-20  1
  7   10034   12      128     2012-05-20  2012-05-20  1
  </code></pre>
  
  <p>I was trying to use filter() but I can't quite figure out how to apply any() to the discharge date. My logic was to pick the first admit date in a group and then check that date among each discharge date and once there is a match then check if the row that has the same discharge date has a value in Num1 in the range of 5-12. </p>
  
  <pre><code>num1_range = [5,6,7,8,9,10,11,12]
  df.loc[df.groupby(['Key']).filter(lambda x : (x['admit'] == x['discharge'].any())&amp;(x['Num1'].isin(num1_range).any())),'flag']=1
  </code></pre>
  
  <p>I'm getting an error</p>
  
  <pre><code>ValueError: cannot set a Timestamp with a non-timestamp
  </code></pre>