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>The algorithm would be:</p>

<ol>
<li>Get a part of the data frame which contains only rows filtered by date with a boolean mask</li>
<li>Perform required replacements on it</li>
<li>Append the rest of the initial data frame to the end of the resulting data frame.</li>
</ol>

<p>Dummy data:</p>

<pre><code>df = pd.DataFrame(np.zeros((5, 2)),columns=['A', 'B'],index=pd.date_range('2000',periods=5,freq='M'))

            A   B
2000-01-31  0.0 0.0
2000-02-29  0.0 0.0
2000-03-31  0.0 0.0
2000-04-30  0.0 0.0
2000-05-31  0.0 0.0
</code></pre>

<p>The code</p>

<pre><code>vars_rs = ['A', 'B']
mask = df.index &lt; '2000-03-31'
early = df[mask]
early = early.groupby([early.index.month, early.index.day])[vars_rs].transform(lambda y: y.replace(0.0, 1))  # replace with your code
result = early.append(df[~mask])
</code></pre>

<p>So the result is</p>

<pre><code>            A   B
2000-01-31  1.0 1.0
2000-02-29  1.0 1.0
2000-03-31  0.0 0.0
2000-04-30  0.0 0.0
2000-05-31  0.0 0.0
</code></pre>

python pandas