Python时间序列:将字典中的每日数据合并为每周数据

Python时间序列:将字典中的每日数据合并为每周数据,python,datetime,dictionary,timestamp,time-series,Python,Datetime,Dictionary,Timestamp,Time Series,我有一本字典如下 my_dict.keys() = dict_keys([20160101, 20160102, 20160103, 20160104, 20160105, 20160106, 20160107, 20160108, 20160109, 20160110, 20160111, 20160112, 20160113, 20160114, 20160115, 20160116, 20160117, 20160118, 20160119, 2

我有一本字典如下

my_dict.keys() = 
dict_keys([20160101, 20160102, 20160103, 20160104, 20160105, 20160106,
       20160107, 20160108, 20160109, 20160110, 20160111, 20160112,
       20160113, 20160114, 20160115, 20160116, 20160117, 20160118,
       20160119, 20160120, 20160121, 20160122, 20160123, 20160124,
       ......    
       20171203, 20171204, 20171213, 20171215, 20171216, 20171217,
       20171218, 20171219, 20171220, 20171221, 20171222, 20171223,
       20171224, 20171225, 20171226, 20171227, 20171228, 20171229,
       20171230, 20171231])

my_dict[20160101] = 
array([[ 0.,  0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  2.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.,  0.,  2.],
       [ 0.,  0.,  4.,  0.,  0.,  0.]])
所以,正如您已经注意到的,我的键指示日期,每个日期都有6乘6的浮点数组。在我字典中的每个键中,所有索引都是相同的

**需要注意的重要一点是,我的字典并不是每天都有。例如,在20171204之后,其20171213和20171215。所以可以跳过日期

现在,我的任务是将每日数据(不是每一天)添加到每周数据中,并在一周内添加所有值。换句话说,从2016年第一周到2017年最后一周,在一周内添加每个值并提供每周数据。此外,由于2016年第一周从20160103(太阳)开始,我可以忽略我的简历中的20160101和20160102数据以及2017年的周末数据。你们能帮我解决这个问题吗?提前谢谢

-------编辑--------- 我的问题似乎不够清楚。因此,我将提供一个快速示例。因为我想遵循一周一次的标准,所以每周从周日开始。因此,2016年的第一周将是201601032016010420160105201601062016010720160108201601‌​09.


因此,我的新词典《每周词典》[201601]这可能是熊猫的工作:

import pandas as pd

# First, get a list of keys
date_ints = list(my_dict)
# Turn them into a pandas Series object
date_int_series = pd.Series(date_ints)
# Cast them to a string, then format them into a full datetime-type with the proper
# format specification
datetime_series = pd.to_datetime(date_int_series.astype('str'), format='%Y%m%d')
# Create a dictionary mapping each date integer -> week of the year
date_int_to_week = dict(zip(date_int_series, datetime_series.dt.week))
这本词典将my_dict的每一个键作为一个键,其相应的一年中的一周作为其值

编辑:

如果您要查找的是基于周对原始词典的每个条目求和,您可以执行以下操作:

week_to_date_list = {}
for date_int, week in date_int_to_week.items():
    if week not in week_to_date_list:
        week_to_date_list[week] = []
    week_to_date_list[week].append(date_int)

my_dict_weekly = {}
for week in week_to_date_list:
    arrays_in_week = [my_dict[day_int] for day_int in week_to_date_list[week]]
    my_dict_weekly[week] = reduce(sum, arrays_in_week)

my_dict_weekly
现在应该是一个以一年中的周为关键字的字典,然后是与该周对应的所有数组的
和。如果您使用的是python 3,则需要从
functools
导入
reduce
,这可能是熊猫的工作:

import pandas as pd

# First, get a list of keys
date_ints = list(my_dict)
# Turn them into a pandas Series object
date_int_series = pd.Series(date_ints)
# Cast them to a string, then format them into a full datetime-type with the proper
# format specification
datetime_series = pd.to_datetime(date_int_series.astype('str'), format='%Y%m%d')
# Create a dictionary mapping each date integer -> week of the year
date_int_to_week = dict(zip(date_int_series, datetime_series.dt.week))
这本词典将my_dict的每一个键作为一个键,其相应的一年中的一周作为其值

编辑:

如果您要查找的是基于周对原始词典的每个条目求和,您可以执行以下操作:

week_to_date_list = {}
for date_int, week in date_int_to_week.items():
    if week not in week_to_date_list:
        week_to_date_list[week] = []
    week_to_date_list[week].append(date_int)

my_dict_weekly = {}
for week in week_to_date_list:
    arrays_in_week = [my_dict[day_int] for day_int in week_to_date_list[week]]
    my_dict_weekly[week] = reduce(sum, arrays_in_week)

my_dict_weekly
现在应该是一个以一年中的周为关键字的字典,然后是与该周对应的所有数组的
和。如果您使用的是python 3,则需要从
functools

导入
reduce
如果我确实理解了您的问题,我认为您可以使用
datetime
timedelta
datetime
模块解决此问题,例如:

from datetime import datetime, timedelta

def get_days_of_week(year, week=1):
    # number of the days
    days = {'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 
            'Thursday': 4, 'Friday': 5, 'Saturday': 6, 'Sunday': 7}
    # construct the datetime object with the year and the desired week
    a = datetime.strptime('{0}'.format(year), '%Y') + timedelta(days=7*(week-1))
    # Every week should start by Sunday .. So escaping days untill the first Sunday
    a += timedelta(days=7-days.get(a.strftime('%A'), 0))
    for k in range(0, 7):
        yield (a + timedelta(days=k)).strftime('%Y%m%d')

days = list(get_days_of_week(2016, week=1))
print('2016 / week = 1:', days)

days = list(get_days_of_week(2016, week=22))
print('2016 / week = 22:', days)
输出:

2016 / week = 1: 
 ['20160103',
 '20160104',
 '20160105',
 '20160106',
 '20160107',
 '20160108',
 '20160109']

2016 / week = 22: 
 ['20160529',
 '20160530',
 '20160531',
 '20160601',
 '20160602',
 '20160603',
 '20160604']
[20160103, 20160104, 20160105, 20160106, 20160107, 20160108, 20160109]
[20160626, 20160627, 20160628, 20160629, 20160630, 20160701, 20160702]
{ 201601: [ 20160103,
            20160104,
            20160105,
            20160106,
            20160107,
            20160108,
            20160109],
  201606: [ 20160626,
            20160627,
            20160628,
            20160629,
            20160630,
            20160701,
            20160702]}
编辑:

根据您上次的编辑,此代码可能满足您的需要:

from datetime import datetime, timedelta

def get_days_of_week(data):
    # number of the days
    days = {'Monday': 1, 'Tuesday': 2, 'Wednesday': 3,
            'Thursday': 4, 'Friday': 5, 'Saturday': 6, 'Sunday': 7}
    date = datetime.strptime('{}'.format(data), '%Y%m%d')
    # get week number
    week = int(date.strftime('%U'))
    # get year
    year = date.strftime('%Y')
    # construct the datetime object with the year and the desired week
    a = datetime.strptime(year, '%Y') + timedelta(days=7*week)
    # Every week should start by Synday .. So escaping days untill the first Sunday
    a += timedelta(days=7-days.get(a.strftime('%A'), 0))

    return {int(str(data)[:-2]): [int((a + timedelta(days=k)).strftime('%Y%m%d')) for k in range(0, 7)]}

week_dict = {}
week_dict.update(get_days_of_week(20160101))
week_dict.update(get_days_of_week(20160623))
print(week_dict[201601])
print(week_dict[201606])

print(week_dict)
输出:

2016 / week = 1: 
 ['20160103',
 '20160104',
 '20160105',
 '20160106',
 '20160107',
 '20160108',
 '20160109']

2016 / week = 22: 
 ['20160529',
 '20160530',
 '20160531',
 '20160601',
 '20160602',
 '20160603',
 '20160604']
[20160103, 20160104, 20160105, 20160106, 20160107, 20160108, 20160109]
[20160626, 20160627, 20160628, 20160629, 20160630, 20160701, 20160702]
{ 201601: [ 20160103,
            20160104,
            20160105,
            20160106,
            20160107,
            20160108,
            20160109],
  201606: [ 20160626,
            20160627,
            20160628,
            20160629,
            20160630,
            20160701,
            20160702]}

如果我确实理解您的问题,我认为您可以使用
datetime
模块中的
datetime
timedelta
来解决此问题,如下示例:

from datetime import datetime, timedelta

def get_days_of_week(year, week=1):
    # number of the days
    days = {'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 
            'Thursday': 4, 'Friday': 5, 'Saturday': 6, 'Sunday': 7}
    # construct the datetime object with the year and the desired week
    a = datetime.strptime('{0}'.format(year), '%Y') + timedelta(days=7*(week-1))
    # Every week should start by Sunday .. So escaping days untill the first Sunday
    a += timedelta(days=7-days.get(a.strftime('%A'), 0))
    for k in range(0, 7):
        yield (a + timedelta(days=k)).strftime('%Y%m%d')

days = list(get_days_of_week(2016, week=1))
print('2016 / week = 1:', days)

days = list(get_days_of_week(2016, week=22))
print('2016 / week = 22:', days)
输出:

2016 / week = 1: 
 ['20160103',
 '20160104',
 '20160105',
 '20160106',
 '20160107',
 '20160108',
 '20160109']

2016 / week = 22: 
 ['20160529',
 '20160530',
 '20160531',
 '20160601',
 '20160602',
 '20160603',
 '20160604']
[20160103, 20160104, 20160105, 20160106, 20160107, 20160108, 20160109]
[20160626, 20160627, 20160628, 20160629, 20160630, 20160701, 20160702]
{ 201601: [ 20160103,
            20160104,
            20160105,
            20160106,
            20160107,
            20160108,
            20160109],
  201606: [ 20160626,
            20160627,
            20160628,
            20160629,
            20160630,
            20160701,
            20160702]}
编辑:

根据您上次的编辑,此代码可能满足您的需要:

from datetime import datetime, timedelta

def get_days_of_week(data):
    # number of the days
    days = {'Monday': 1, 'Tuesday': 2, 'Wednesday': 3,
            'Thursday': 4, 'Friday': 5, 'Saturday': 6, 'Sunday': 7}
    date = datetime.strptime('{}'.format(data), '%Y%m%d')
    # get week number
    week = int(date.strftime('%U'))
    # get year
    year = date.strftime('%Y')
    # construct the datetime object with the year and the desired week
    a = datetime.strptime(year, '%Y') + timedelta(days=7*week)
    # Every week should start by Synday .. So escaping days untill the first Sunday
    a += timedelta(days=7-days.get(a.strftime('%A'), 0))

    return {int(str(data)[:-2]): [int((a + timedelta(days=k)).strftime('%Y%m%d')) for k in range(0, 7)]}

week_dict = {}
week_dict.update(get_days_of_week(20160101))
week_dict.update(get_days_of_week(20160623))
print(week_dict[201601])
print(week_dict[201606])

print(week_dict)
输出:

2016 / week = 1: 
 ['20160103',
 '20160104',
 '20160105',
 '20160106',
 '20160107',
 '20160108',
 '20160109']

2016 / week = 22: 
 ['20160529',
 '20160530',
 '20160531',
 '20160601',
 '20160602',
 '20160603',
 '20160604']
[20160103, 20160104, 20160105, 20160106, 20160107, 20160108, 20160109]
[20160626, 20160627, 20160628, 20160629, 20160630, 20160701, 20160702]
{ 201601: [ 20160103,
            20160104,
            20160105,
            20160106,
            20160107,
            20160108,
            20160109],
  201606: [ 20160626,
            20160627,
            20160628,
            20160629,
            20160630,
            20160701,
            20160702]}

你所说的每周数据是什么意思?您的意思是,例如
20171213
将转换为
201750
,因为50是2017年12月13日的第50周?考虑到周日是一周的第一天,我想将反映2016年第一周的所有值(即20160103201601042016010520160106201601072016010820160109)添加到201601中,并使用每周数据制作一个新字典。这有用吗?你说的每周数据是什么意思?您的意思是,例如
20171213
将转换为
201750
,因为50是2017年12月13日的第50周?考虑到周日是一周的第一天,我想将反映2016年第一周的所有值(即20160103201601042016010520160106201601072016010820160109)添加到201601中,并使用每周数据制作一个新字典。这有用吗?我不明白。这如何将每个字典的值添加到每周值中?你介意举个例子吗?我已经编辑了我的问题。希望它有意义。谢谢我添加了一个解释,说明如何使用
date\u int\u to\u week
字典对原始
my\u dict
进行求和。还有一件事要问。当您将日期设置为周时,似乎周一是一周的开始。你怎么能把它改成星期天是一周的开始?-->21个数组\u in_week=[my_dict[day\u int]for day\u int in_week\u to_date\u list[week]]索引器:列表索引超出范围我不明白。这如何将每个字典的值添加到每周值中?你介意举个例子吗?我已经编辑了我的问题。希望它有意义。谢谢我添加了一个解释,说明如何使用
date\u int\u to\u week
字典对原始
my\u dict
进行求和。还有一件事要问。当您将日期设置为周时,似乎周一是一周的开始。如何将其更改为星期天是一周的开始?-->21个数组\u in_week=[my_dict[day\u int]for day\u int in_week]to \u date\u list[week]]索引器:列表索引超出范围我编辑了我的问题。希望这对你有意义!。谢谢我编辑了我的问题。希望这对你有意义!。谢谢