Pandas DatetimeIndex strange behaviour
<p>I handle a DataFrame, which index is string, year-month, for example:</p>

<pre><code>index = ['2007-01', '2007-03', ...]
</code></pre>

<p>however, the index is not full. e.g. <code>2007-02</code> is missing.
What I want is to reindex the DataFrame with full index.</p>

<p>What I have tried:</p>

<pre><code>In [60]: pd.DatetimeIndex(start='2007-01', end='2007-12', freq='M')
Out[60]: 
DatetimeIndex(['2007-01-31', '2007-02-28', '2007-03-31', '2007-04-30',
           '2007-05-31', '2007-06-30', '2007-07-31', '2007-08-31',
           '2007-09-30', '2007-10-31', '2007-11-30'],
          dtype='datetime64[ns]', freq='M')
</code></pre>

<p>The index is every month's ends.</p>

<pre><code>In [64]: pd.DatetimeIndex(['2007-01', '2007-03', '2007-04', '2007-05'])
Out[64]: DatetimeIndex(['2007-01-01', '2007-03-01', '2007-04-01', '2007-05-01'],    dtype='datetime64[ns]', freq=None)
</code></pre>

<p>The index is every month's start.</p>

<p>How to handle this problem?</p>

<p>I think you need add parameter <code>freq='MS'</code> if need frequency first day of months:</p>

<pre><code>print (pd.DatetimeIndex(start='2007-01', end='2007-12', freq='MS'))
DatetimeIndex(['2007-01-01', '2007-02-01', '2007-03-01', '2007-04-01',
               '2007-05-01', '2007-06-01', '2007-07-01', '2007-08-01',
               '2007-09-01', '2007-10-01', '2007-11-01', '2007-12-01'],
              dtype='datetime64[ns]', freq='MS')
</code></pre>

<p>Link to <a href="http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases" rel="nofollow">Offset Aliases in pandas documentation</a>, thank you <a href="http://stackoverflow.com/questions/39720641/pandas-datetimeindex-strange-behaviour/39720677#comment66738837_39720641">EdChum</a>.</p>

<p>Another solution is use <a href="http://pandas.pydata.org/pandas-docs/stable/timeseries.html#periodindex-and-period-range" rel="nofollow"><code>PeriodIndex</code></a> for generating months period:</p>

<pre><code>print (pd.PeriodIndex(start='2007-01', end='2007-12', freq='M'))
PeriodIndex(['2007-01', '2007-02', '2007-03', '2007-04', '2007-05', '2007-06',
             '2007-07', '2007-08', '2007-09', '2007-10', '2007-11', '2007-12'],
            dtype='int64', freq='M')
</code></pre>

python pandas date-range period datetimeindex