title: Python split a list of datetimes by year + month
body: <p>I have the following csv files:</p>
  
  <pre><code># simulate a csv file
  from StringIO import StringIO
  data = StringIO("""
  2012-04-01,00:10, A, 10
  2012-04-01,00:20, B, 11
  2012-04-01,00:30, B, 12
  2012-04-02,00:10, A, 18
  2012-05-02,00:20, A, 14
  2012-05-02,00:30, B, 11
  2012-05-03,00:10, A, 10
  2012-06-03,00:20, B, 13
  2012-06-03,00:30, C, 12
  """.strip())
  </code></pre>
  
  <p>which I would like to gropu by year+month plus category (ie. A, B, C).</p>
  
  <p>I would like the final data to have grouping by month and then by category
  as a view of the original data</p>
  
  <pre><code>2012-04, A
  
  &gt;&gt;  array[0,] =&gt; 2012-04-01,00:10, A, 10
  
  &gt;&gt;  array[3,] =&gt; 2012-04-02,00:10, A, 18
  
  2012-04, B
  
  &gt;&gt;  array[1,] =&gt; 2012-04-01,00:20, B, 11
  
  &gt;&gt;  array[2,] =&gt; 2012-04-01,00:30, B, 12
  
  2012-05, A
  
  &gt;&gt;  array[4,] =&gt; 2012-05-02,00:20, A, 14
  
  ...
  </code></pre>
  
  <p>And then for each group, I would like iterate to plot them using the same function.</p>
  
  <p>I have seen a similar question on splitting by dates by days
  <a href="https://stackoverflow.com/questions/8983150/split-list-of-datetimes-into-days">Split list of datetimes into days</a>
  and I am able to to so in my case a).  But having some issues turning that into a year+month split in case b).</p>
  
  <p>Here is the snippet that I have so far with the issue that I am running into:</p>
  
  <pre><code>#! /usr/bin/python
  
  import numpy as np
  import csv
  import os
  from  datetime import datetime
  
  def strToDate(string):
      d = datetime.strptime(string, '%Y-%m-%d')
      return d;
  
  def strToMonthDate(string):
      d = datetime.strptime(string, '%Y-%m-%d')
      d_by_month = datetime(d.year,d.month,1)
      return d_by_month;
  
  # simulate a csv file
  from StringIO import StringIO
  data = StringIO("""
  2012-04-01,00:10, A, 10
  2012-04-01,00:20, B, 11
  2012-04-01,00:30, B, 12
  2012-04-02,00:10, A, 18
  2012-05-02,00:20, A, 14
  2012-05-02,00:30, B, 11
  2012-05-03,00:10, A, 10
  2012-06-03,00:20, B, 13
  2012-06-03,00:30, C, 12
  """.strip())
  
  arr = np.genfromtxt(data, delimiter=',', dtype=object)
  
  
  # a) If we were to just group by dates
  # Get unique dates
  #keys = np.unique(arr[:,0])
  #keys1 = np.unique(arr[:,2])
  # Group by unique dates
  #for key in keys:
  #   print key   
  #   for key1 in keys1:      
  #       group = arr[ (arr[:,0]==key) &amp; (arr[:,2]==key1) ]                       
  #       if group.size:
  #           print "\t" + key1
  #           print group
  #   print "\n"      
  
  # b) But if we want to group by year+month in the dates 
  dates_by_month = np.array(map(strToMonthDate, arr[:,0]))
  keys2 = np.unique(dates_by_month)
  print dates_by_month
  # &gt;&gt; [datetime.datetime(2012, 4, 1, 0, 0), datetime.datetime(2012, 4, 1, 0, 0), ...
  print "\n"  
  print keys2
  # &gt;&gt; [2012-04-01 00:00:00 2012-05-01 00:00:00 2012-06-01 00:00:00]
  
  for key in keys2:
      print key       
       print type(key)
      group = arr[dates_by_month==key]
          print group
      print "\n"  
  </code></pre>
  
  <p><strong>Question:</strong> I get the monthly key but for the group, all I get is [2012-04-01 00:10  A  10] for each group.  key in keys2 is of type datetime.datetime.  Any idea what could be wrong?  Any alternative implementations suggestions are welcome.  I would prefer not to use a itertools.groupby solution, as it returns an iterator rather than an array, which is less suitable for plotting.</p>
  
  <p><strong>Edit1:</strong> Problem solved.  The issue was that the dates_by_month that I used in advance indexing in case b) should be initialized as an np.array instead of a list which map returns     dates_by_month = np.array(map(strToMonthDate, arr[:,0])).  I have fixed it in the snippet above, and the example now works.</p>