我有以下 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
...
然后对于每个组,我想使用相同的函数迭代绘制它们。
我见过一个关于按天分割日期的类似问题将日期时间列表拆分为天 https://stackoverflow.com/questions/8983150/split-list-of-datetimes-into-days在我的情况下我能够这样做a)。但在情况 b) 中存在一些问题,将其变成年+月分割。
这是迄今为止我遇到的问题的片段:
#! /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 中的 key 类型为 datetime.datetime。知道可能出什么问题吗?欢迎任何替代实施建议。我不想使用 itertools.groupby 解决方案,因为它返回一个迭代器而不是一个数组,这不太适合绘图。
Edit1:问题解决了。问题是我在情况 b) 中提前索引使用的dates_by_month 应该初始化为 np.array,而不是映射返回dates_by_month = np.array(map(strToMonthDate, arr[:,0])) 的列表。我已在上面的代码片段中修复了该问题,该示例现在可以运行了。