使用月份和年份填写词典列表中的缺失值

时间:2014-03-31 12:33:10

标签: python datetime

我为可视化准备数据。数据结构如下:

data = [{u'count': 1, u'_id': {u'year': 2010, u'month': 4}}, {u'count': 1, u'_id': {u'year': 2010, u'month': 5}}, {u'count': 2, u'_id': {u'year': 2010, u'month': 7}}, {u'count': 1, u'_id': {u'year': 2010, u'month': 9}}, {u'count': 1, u'_id': {u'year': 2010, u'month': 10}}, {u'count': 4, u'_id': {u'year': 2010, u'month': 12}}]

我用这种方式将它们转换为带有时间戳和计数变量的列表:

chart = []
for month in data:
      d = datetime.datetime.strptime(str(month['_id']['year'])+"-"+str(month['_id']['month']),'%Y-%m')
      dat = time.mktime(d.timetuple())
      chart.append([dat*1000,month['count']])

结果有点像这样(这个例子不适合输入数据的例子)

chart: [[1220216400000.0, 1], [1222808400000.0, 8], [1225490400000.0, 1], [1228082400000.0, 6], [1230760800000.0, 4], [1233439200000.0, 1], [1235858400000.0, 1], [1238533200000.0, 1], [1241125200000.0, 2], [1243803600000.0, 1], [1246395600000.0, 1], [1249074000000.0, 1]]

我要做的是更改代码,以包括count = 0的第一个和最后一个日期之间缺少的月份。例如,在数据中,从2010年5月开始,下一个字段是10月7日。缺少第6个月,我希望将其包含在count = 0中。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

这是一种方法。

我们的想法是拥有一本字典dat - > count。如果您不知道数据中的年份,您需要在每次迭代时初始化每月数据:

import datetime
from pprint import pprint
import time

data = [{u'count': 1, u'_id': {u'year': 2010, u'month': 4}}, {u'count': 1, u'_id': {u'year': 2010, u'month': 5}},
        {u'count': 2, u'_id': {u'year': 2010, u'month': 7}}, {u'count': 1, u'_id': {u'year': 2010, u'month': 9}},
        {u'count': 1, u'_id': {u'year': 2010, u'month': 10}}, {u'count': 4, u'_id': {u'year': 2010, u'month': 12}}]

chart = {}
for month in data:
    year = month['_id']['year']
    for m in xrange(1, 12):
        d = datetime.datetime.strptime(str(year) + "-" + str(m), '%Y-%m')
        dat = time.mktime(d.timetuple()) * 1000
        if dat not in chart:
            chart[dat] = 0

    d = datetime.datetime.strptime(str(year) + "-" + str(month['_id']['month']), '%Y-%m')
    dat = time.mktime(d.timetuple()) * 1000
    chart[dat] = month['count']

pprint(sorted(chart.items()))

如果您知道数据中的年份 - 在data循环之前初始化月份计数。

打印:

[(1262322000000.0, 0),
 (1265000400000.0, 0),
 (1267419600000.0, 0),
 (1270094400000.0, 1),
 (1272686400000.0, 1),
 (1275364800000.0, 0),
 (1277956800000.0, 2),
 (1280635200000.0, 0),
 (1283313600000.0, 1),
 (1285905600000.0, 1),
 (1288584000000.0, 0),
 (1291179600000.0, 4)]

请参阅 - 缺少月数,0计数。

希望有所帮助。

答案 1 :(得分:1)

这是一个使用dateutil库每月迭代日期范围的解决方案。

我们的想法是将OrderedDict初始化为datetime作为关键字,count作为值。然后,对于有序字典中的每个项目,每月迭代当前和之前添加的项目之间的日期范围,并添加0计数:

from collections import OrderedDict
import datetime
from pprint import pprint
import time
from dateutil.rrule import rrule, MONTHLY


data = [{u'count': 1, u'_id': {u'year': 2010, u'month': 4}}, {u'count': 1, u'_id': {u'year': 2010, u'month': 5}},
        {u'count': 2, u'_id': {u'year': 2010, u'month': 7}}, {u'count': 1, u'_id': {u'year': 2010, u'month': 9}},
        {u'count': 1, u'_id': {u'year': 2010, u'month': 10}}, {u'count': 4, u'_id': {u'year': 2010, u'month': 12}}]

new_data = OrderedDict()
for item in data:
    year, month = item['_id']['year'], item['_id']['month']
    d = datetime.datetime.strptime(str(year) + "-" + str(month), '%Y-%m')
    new_data[d] = item['count']

chart = {}
last_added = None
for d, count in new_data.iteritems():
    date_start = last_added if last_added else d
    for dt in rrule(MONTHLY, dtstart=date_start, until=d):
        key = time.mktime(dt.timetuple()) * 1000
        if key not in chart:
            chart[key] = count if dt == d else 0
    last_added = d

pprint(sorted(chart.items()))

打印:

[(1270094400000.0, 1),
 (1272686400000.0, 1),
 (1275364800000.0, 0),
 (1277956800000.0, 2),
 (1280635200000.0, 0),
 (1283313600000.0, 1),
 (1285905600000.0, 1),
 (1288584000000.0, 0),
 (1291179600000.0, 4)]

希望它适合你。

答案 2 :(得分:0)

我看到您的列表已排序,因此您只需记住上一个日期(最初设置为1)并填充列表中缺少的元素(如果有)(即month['_id']['month']之间的差异和上一个日期大于1)。