[英]Group a list of dates by month, year
raw_data = ["2015-12-31", "2015-12-1" , "2015-1-1",
"2014-12-31", "2014-12-1" , "2014-1-1",
"2013-12-31", "2013-12-1" , "2013-1-1",]
expected_grouped_bymonth = [("2015-12", #dates_in_the_list_occured_in_december_2015)
, ...
("2013-1", #january2013dates)]
或作为命令
expected_grouped_bymonth = {
"2015-12": #dates_in_the_list_occured_in_december_2015) , ...
"2013-1", #january2013dates)}
我有一个代表日期的字符串列表。 我想要的是一个元组列表或一本字典,该列表每年或每月计数出现的次数。 我尝试做的是与groupby
相关的事情。 我无法理解如何根据groupby
函数使用TimeGrouper
。
引发的异常是:
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex,
but got an instance of 'RangeIndex'
from itertools import groupby
for el in data:
if 'Real rates - Real volatilities' in el['scenario']:
counter += 1
real_records_dates.append(pd.to_datetime(el['refDate']))
print("Thera are {} real records.".format(counter))
BY_YEAR = 'Y'
BY_MONTH = 'M'
BY_DAY = 'D'
real_records_df = pd.DataFrame(pd.Series(real_records_dates))
real_records_df.groupby(pd.TimeGrouper(freq=BY_MONTH))
(如果更容易{date1:1, date2:2, ...}
您还可以假设以字典og {date1:1, date2:2, ...}
开头。我的问题仅与groupby
。)
如果您想了解每月和每年某日期发生的频率,可以使用defaulftdict :
raw_data = ["2015-12-31", "2015-12-1", "2015-1-1",
"2014-12-31", "2014-12-1", "2014-1-1",
"2013-12-31", "2013-12-1", "2013-1-1",
]
from collections import defaultdict
dates = defaultdict(lambda:defaultdict(int))
for s in raw_data:
k, v = s.rsplit("-", 1)
dates[k][v] += 1
print(dates)
或者,如果您只想按月,年对日期列表进行分组 :
dates = defaultdict(list)
for s in raw_data:
k, v = s.rsplit("-", 1)
dates[k].append(v)
print(dates)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.