Fill in missing values using climatology data except for current year
<pre><code>df.groupby([df.index.month, df.index.day])[vars_rs].transform(lambda y: y.fillna(y.median()))
</code></pre>

<p>I am filling missing values in a dataframe with median values from climatology. The days range from Jan 1 2010 to Dec 31st 2016. However, I only want to fill in missing values for days before current date (say Oct 1st 2016). How do I modify the statement?</p>

<p>Use <code>np.where</code>, example:</p>

<pre><code>import pandas as pd
import numpy as np
df = pd.DataFrame({'A':['a','a','b','b','c','c'],'B':[1,2,3,4,5,6],'C':[1,np.nan,np.nan,np.nan,np.nan,np.nan]})
df.ix[:,'C'] = np.where((df.A != 'c')&amp;(df.B &lt; 4)&amp;(pd.isnull(df.C)),-99,df.ix[:,'C'])
</code></pre>

<p>Like this you can directly modify the desired column using boolean expressions and all columns.</p>

<p>Original dataframe:</p>

<pre><code>   A  B    C
0  a  1  1.0
1  a  2  NaN
2  b  3  NaN
3  b  4  NaN
4  c  5  NaN
5  c  6  NaN
</code></pre>

<p>Modified dataframe:</p>

<pre><code>   A  B     C
0  a  1   1.0
1  a  2 -99.0
2  b  3 -99.0
3  b  4   NaN
4  c  5   NaN
5  c  6   NaN
</code></pre>

python pandas