【问题标题】:Python split a list of datetimes by year + monthPython按年+月拆分日期时间列表
【发布时间】:2013-07-30 01:45:19
【问题描述】:

我有以下 csv 文件:

# 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())

我想按年+月加类别(即 A、B、C)来分类。

我希望最终数据先按月分组,然后按类别分组 作为原始数据的视图

2012-04, A

>>  array[0,] => 2012-04-01,00:10, A, 10

>>  array[3,] => 2012-04-02,00:10, A, 18

2012-04, B

>>  array[1,] => 2012-04-01,00:20, B, 11

>>  array[2,] => 2012-04-01,00:30, B, 12

2012-05, A

>>  array[4,] => 2012-05-02,00:20, A, 14

...

然后对于每个组,我想使用相同的函数迭代绘制它们。

我看到过一个类似的问题,关于按日期拆分 Split list of datetimes into days 在我的情况下,我可以这样做 a)。但是在 b) 的情况下,有一些问题将其转换为年+月拆分。

这是我目前遇到的问题的 sn-p:

#! /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) & (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
# >> [datetime.datetime(2012, 4, 1, 0, 0), datetime.datetime(2012, 4, 1, 0, 0), ...
print "\n"  
print keys2
# >> [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"  

问题:我得到了每月密钥,但对于组,我得到的只是每个组的 [2012-04-01 00:10 A 10]。 keys2 中的键是 datetime.datetime 类型。知道有什么问题吗?欢迎任何替代实施建议。我不想使用 itertools.groupby 解决方案,因为它返回一个迭代器而不是一个数组,这不太适合绘图。

Edit1:问题已解决。问题是我在案例 b) 中使用的提前索引的 dates_by_month 应该初始化为 np.array 而不是 map 返回 dates_by_month = np.array(map(strToMonthDate, arr[:,0])) 的列表。我已经在上面的 sn-p 中修复了它,现在这个例子可以工作了。

【问题讨论】:

    标签: python datetime numpy


    【解决方案1】:

    我在原来的解决方案中找到了问题所在。

    在情况 b) 中,

    dates_by_month = map(strToMonthDate, arr[:,0]) 
    

    返回一个列表而不是一个 numpy 数组。高级索引:

    group = arr[dates_by_month==key]
    

    因此行不通。如果相反,我有:

    dates_by_month = np.array(map(strToMonthDate, arr[:,0]))
    

    然后分组按预期工作。

    【讨论】:

    • 欢迎接受您自己的答案。因此,未来面临同样问题的用户可以使用您的知识。
    • @Hyperboreus,谢谢,我正在等待两天的限制,然后才能接受我的回答。
    • @frank 我稍微重新格式化了您的帖子,以使其更易于理解...随意调整/回滚...
    猜你喜欢
    • 2019-02-20
    • 2021-05-07
    • 1970-01-01
    • 2017-04-14
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2016-02-20
    相关资源
    最近更新 更多