Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can just use reindex on a time series using your date range. Also it looks like you would be better off using a TimeSeries instead of a DataFrame (see <a href="http://pandas.pydata.org/pandas-docs/stable/timeseries.html" rel="noreferrer">documentation</a>), although reindexing is also the correct method for adding missing index values to DataFrames as well.</p> <p>For example, starting with:</p> <pre><code>date_index = pd.DatetimeIndex([pd.datetime(2003,6,24), pd.datetime(2003,8,13), pd.datetime(2003,8,19), pd.datetime(2003,8,22), pd.datetime(2003,8,24)]) ts = pd.Series([2,1,2,1,5], index=date_index) </code></pre> <p>Gives you a time series like your example dataframe's head:</p> <pre><code>2003-06-24 2 2003-08-13 1 2003-08-19 2 2003-08-22 1 2003-08-24 5 </code></pre> <p>Simply doing </p> <pre><code>ts.reindex(pd.date_range(min(date_index), max(date_index))) </code></pre> <p>then gives you a complete index, with NaNs for your missing values (you can use <code>fillna</code> if you want to fill the missing values with some other values - see <a href="http://pandas.pydata.org/pandas-docs/stable/missing_data.html#missing-data-fillna" rel="noreferrer">here</a>):</p> <pre><code>2003-06-24 2 2003-06-25 NaN 2003-06-26 NaN 2003-06-27 NaN 2003-06-28 NaN 2003-06-29 NaN 2003-06-30 NaN 2003-07-01 NaN 2003-07-02 NaN 2003-07-03 NaN 2003-07-04 NaN 2003-07-05 NaN 2003-07-06 NaN 2003-07-07 NaN 2003-07-08 NaN 2003-07-09 NaN 2003-07-10 NaN 2003-07-11 NaN 2003-07-12 NaN 2003-07-13 NaN 2003-07-14 NaN 2003-07-15 NaN 2003-07-16 NaN 2003-07-17 NaN 2003-07-18 NaN 2003-07-19 NaN 2003-07-20 NaN 2003-07-21 NaN 2003-07-22 NaN 2003-07-23 NaN 2003-07-24 NaN 2003-07-25 NaN 2003-07-26 NaN 2003-07-27 NaN 2003-07-28 NaN 2003-07-29 NaN 2003-07-30 NaN 2003-07-31 NaN 2003-08-01 NaN 2003-08-02 NaN 2003-08-03 NaN 2003-08-04 NaN 2003-08-05 NaN 2003-08-06 NaN 2003-08-07 NaN 2003-08-08 NaN 2003-08-09 NaN 2003-08-10 NaN 2003-08-11 NaN 2003-08-12 NaN 2003-08-13 1 2003-08-14 NaN 2003-08-15 NaN 2003-08-16 NaN 2003-08-17 NaN 2003-08-18 NaN 2003-08-19 2 2003-08-20 NaN 2003-08-21 NaN 2003-08-22 1 2003-08-23 NaN 2003-08-24 5 Freq: D, Length: 62 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload