wesm/pandas — Issue #743: Empty DataFrame returned when grouping on datetime column
Author: ruidc
Created: 2012-02-04 09:25:29+00:00
State: open

import datetime
import numpy
import pandas

def simple_test(use_date=True):
data = [
[1, '2012-01-01', 1.0],
[2, '2012-01-02', 2.0],
[3, None, 3.0]
]
if use_date:
data = [[row[0], datetime.datetime.strptime(row[1], '%Y-%m-
%d').date() if row[1] else None, row[2]] for row in data]
df = pandas.DataFrame({'key': [x[0] for x in data], 'date': [x[1]
for x in data], 'value': [x[2] for x in data]})
df['weights'] = df['value']/df['value'].sum()
gb = df.groupby('date').aggregate(numpy.sum)

print 'II) Using date objects' if use_date else 'I) Using date
strings'
print df
print ''
print gb

if __name__ == '__main__':
simple_test(use_date=False)
print '*' * 40
simple_test(use_date=True)