title: Find gaps in pandas time series dataframe sampled at 1 minute intervals and fill the gaps with new rows
body: <h1>Problem</h1>
  <p>I have a data frame containing financial data sampled at 1 minute intervals. Occasionally a row or two of data might be missing.</p>
  <ul>
  <li>I'm looking for a good (simple and efficient) way to insert new rows into the dataframe at the points in which there is missing data.</li>
  <li>The new rows can be empty except for the index, which contains the timestamp.</li>
  </ul>
  <h3>For example:</h3>
  <pre><code> #Example Input---------------------------------------------
                        open     high     low      close
   2019-02-07 16:01:00  124.624  124.627  124.647  124.617  
   2019-02-07 16:04:00  124.646  124.655  124.664  124.645  
  
   # Desired Ouput--------------------------------------------
                        open     high     low      close
   2019-02-07 16:01:00  124.624  124.627  124.647  124.617  
   2019-02-07 16:02:00  NaN      NaN      NaN      NaN
   2019-02-07 16:03:00  NaN      NaN      NaN      NaN
   2019-02-07 16:04:00  124.646  124.655  124.664  124.645 
  </code></pre>
  <p>My current method is based off this post -
  <a href="https://stackoverflow.com/questions/47831863/find-missing-minute-data-in-time-series-data-using-pandas?rq=1">Find missing minute data in time series data using pandas</a> - which is advises only how to identify the gaps. Not how to fill them.</p>
  <p>What I'm doing is creating a DateTimeIndex of 1min intervals. Then using this index, I create an entirely new dataframe, which can then be merged into my original dataframe thus filling the gaps.  Code is shown below. It seems quite a round about way of doing this. <strong>I would like to know if there is a better way. Maybe with resampling the data?</strong></p>
  <pre><code>import pandas as pd
  from datetime import datetime
  
  # Initialise prices dataframe with missing data
  prices = pd.DataFrame([[datetime(2019,2,7,16,0),  124.634,  124.624, 124.65,   124.62],[datetime(2019,2,7,16,4), 124.624,  124.627,  124.647,  124.617]])
  prices.columns = ['datetime','open','high','low','close']
  prices = prices.set_index('datetime')
  print(prices)
  
  # Create a new dataframe with complete set of time intervals
  idx_ref = pd.DatetimeIndex(start=datetime(2019,2,7,16,0), end=datetime(2019,2,7,16,4),freq='min')
  df = pd.DataFrame(index=idx_ref)
  
  # Merge the two dataframes 
  prices = pd.merge(df, prices, how='outer', left_index=True, 
  right_index=True)
  print(prices)
  </code></pre>