Removing incomplete years from pandas dataframe
<p>I want to remove incomplete years from a dataframe to analyse the complete years further. I've looked <a href="https://stackoverflow.com/questions/17256294/pandas-dataframe-remove-values-from-a-group-with-less-than-x-rows">here</a> but the question is old and unresolved. This post follows on from a <a href="https://stackoverflow.com/questions/36090917/calculating-the-number-of-years-in-a-pandas-dataframe">previous one</a> where some of this code came from.</p>

<p>Dataframe:</p>

<pre><code>dates = pd.date_range('20070101',periods=3200)
df = pd.DataFrame(np.random.randn(3200), columns =list ('A'))
df['date'] = dates
df = df[['date','A']]
df.head()

        date         A
0 2007-01-01 -0.961392
1 2007-01-02  0.832136
2 2007-01-03  1.007776
3 2007-01-04 -1.144121
4 2007-01-05 -0.850436
</code></pre>

<p>Index by year, count the days</p>

<pre><code>count = df.set_index([df['date'].dt.year, df['date'].dt.day]).count(level=0)
count

      date    A
date           
2007   365  365
2008   366  366
2009   365  365
2010   365  365
2011   365  365
2012   366  366
2013   365  365
2014   365  365
2015   278  278
</code></pre>

<p>Drop all the years less than 365 days:</p>

<pre><code>count = count.drop(count[count.date &lt; 365].index)
</code></pre>

<p>I now want to 'uncount' the data so it returns to looking like this:</p>

<pre><code>        date         A
0 2007-01-01 -0.961392
1 2007-01-02  0.832136
2 2007-01-03  1.007776
3 2007-01-04 -1.144121
4 2007-01-05 -0.850436
</code></pre>

<p>but doesn't include 2015 as the year is too short. </p>

<h1>ANSWER</h1>

<p>Create a years variable</p>

<pre><code>years = count[count['date'] &gt;= 365].index
</code></pre>

<p>reformat the dataframe</p>

<pre><code>df = df[df['date'].dt.year.isin(years)]
df

Out[11]: 
           date         A
0    2007-01-01 -0.095152
1    2007-01-02 -0.560478
2    2007-01-03  0.793423
...    ....    ....   ...
2919 2014-12-29 -1.616931
2920 2014-12-30 -3.499278
2921 2014-12-31  0.913521
</code></pre>