title: Pandas fill-in-zero for by missing dates *defined by* group
body: <p>Question: Using pandas -- how to efficiently fill-in missing dates with zero values, with monthly (e.g. last day indexed) frequency, relative to the min/max date values <em>per</em> group?</p>
  
  <p><em>Edit</em> do not assume input dates to correspond to the last day in its month.
  To deal with this issue, add the following line to the proposed answers below:</p>
  
  <pre><code>df.date = df.date + pd.offsets.MonthEnd(0) 
  </code></pre>
  
  <p><em>Without this fix, filled in values with freq='M' can results in NA's...!</em></p>
  
  <p>Note:
  pandas version 0.24.2</p>
  
  <p>Example input:</p>
  
  <pre><code>data = [{'name': 'A', 'date': '2019-01-01', 'val': 10},
  {'name': 'A', 'date': '2019-04-30', 'val': 2},
  {'name': 'B', 'date': '2019-02-15', 'val': 6},
  {'name': 'B', 'date': '2019-05-01', 'val': 5}]
  
  df = pd.DataFrame(data)
  
           date name  val
  0  2019-01-01    A   10
  1  2019-04-30    A    2
  2  2019-02-15    B    6
  3  2019-05-01    B    5
  </code></pre>
  
  <p>Note that the dates in the input are not necessarily the first or last day of their corresponding month.</p>
  
  <p>Example desired output</p>
  
  <pre><code>         date name  val
  0  2019-01-31    A   10
  1  2019-02-28    A    0
  2  2019-03-31    A    0
  3  2019-04-30    A    2
  4  2019-02-28    B    6
  5  2019-03-31    B    0
  6  2019-04-30    B    0
  7  2019-05-31    B    5
  </code></pre>
  
  <p>Attempts:</p>
  
  <p>The following works at the index level but fills everything with NA:</p>
  
  <pre><code>df['date'] = pd.to_datetime(df['date'])
  
  dg = df.groupby('name').apply(lambda x: x.reindex(pd.date_range(min(x.date), max(x.date), freq='M')))
  </code></pre>
  
  <p>Also:</p>
  
  <p><a href="https://stackoverflow.com/questions/44978196/pandas-filling-missing-dates-and-values-within-group/44979696#44979696">Pandas filling missing dates and values within group</a></p>
  
  <p>The above link's answer appear not be not relative to for each group, but rather the entire dataset's min/max date values. </p>