title: How do I use a datetime.date value in apache beam GroupBy?
body: <p>I am trying to use apache beam's <a href="https://beam.apache.org/documentation/transforms/python/aggregation/groupby/" rel="nofollow noreferrer"><code>beam.GroupBy(...)</code></a> construct, and running into a problem with it not knowing how to encode a standard python <code>datetime.date</code>.</p>
  <p>Here's a simplified code chunk demonstrating the issue. The code:</p>
  <pre class="lang-py prettyprint-override"><code>import logging
  import random
  import apache_beam as beam
  from apache_beam.io import WriteToText
  from datetime import date
  
  
  def random_record():
      return {
          'account_id'   : random.randint(1004, 1009),
          'activity_date': date(2021, 10, random.randint(1, 4)),
          'region'       : random.randint(30, 40),
          'largest_sale' : random.randint(10000, 40000),
          'total_sales'  : random.randint(100000, 900000)
      }
  
  def main(argv=None):
      random.seed(2349090823434)
  
      records = [random_record() for i in range(100)]
  
      with beam.Pipeline() as p:
          output = (
              p 
              | &quot;Source&quot; &gt;&gt; beam.Create(records)
              | &quot;Typed&quot;  &gt;&gt; beam.Map(lambda d: beam.Row(**d))
              | &quot;Rollup&quot; &gt;&gt; (beam.GroupBy('account_id', 'activity_date')
                                  .aggregate_field('total_sales', sum, 'total_sales')
                                  .aggregate_field('largest_sale', max, 'largest_sale')
                          )
              | &quot;Output&quot; &gt;&gt; WriteToText('learn-output', file_name_suffix='.csv')
          )
  
  if __name__ == '__main__':
      logging.getLogger().setLevel(logging.INFO)
      main()
  </code></pre>
  <p>When I run this:</p>
  <pre class="lang-sh prettyprint-override"><code>% python learn.py
  </code></pre>
  <p>Results in the error:</p>
  <pre><code>WARNING:apache_beam.coders.coder_impl:Using fallback deterministic coder for type '&lt;class 'apache_beam.transforms.core.Key'&gt;' in 'Rollup/CombinePerKey(TupleCombineFn)/GroupByKey'.
  Traceback (most recent call last):
    ...
  TypeError: Unable to deterministically encode '2021-10-01' of type '&lt;class 'datetime.date'&gt;', please provide a type hint for the input of 'Rollup/CombinePerKey(TupleCombineFn)/GroupByKey'
  
  ...
  
  TypeError: Unable to deterministically encode 'Key(account_id=1007, activity_date=datetime.date(2021, 10, 1))' of type '&lt;class 'apache_beam.transforms.core.Key'&gt;', 
  please provide a type hint for the input of 'Rollup/CombinePerKey(TupleCombineFn)/GroupByKey'
  </code></pre>
  <p>I am wondering how to actually &quot;provide a type hint for the input of 'Rollup/CombinePerKey(TupleCombineFn)/GroupByKey'&quot;.</p>
  <p>It seems like the <code>GroupByKey</code> is being called within <code>GroupBy</code>. So I either need to tell GroupBy how to handle my 2-tuple key, or perhaps register some coder to support <code>datetime.date</code>...</p>