如何将日期间隔与给定的数据帧匹配

时间:2019-07-15 05:57:45

标签: python pandas dataframe date-range

我建立了一个数据框,其中包含来自多个相关django模型的数据。这些模型中的许多模型都在系统中添加了自己的开始日期/结束日期。我想将添加的日期与我从系统用户那里获得的日期进行匹配,并为这些日期建立字典。

我试图将数据范围添加到包含与该资源有关的所有信息的数据框。

我的数据框包含与资源有关的所有信息

full_data = pd.DataFrame((resource).values('signum', 'name', 'start',
    'end', 'company__name', 'resourcerole__role__name',
    'resourcerole__start', 'resourcerole__end',
    'resourcecontracttype__contracttype__name',
    'resourcecontracttype__start', 'resourcecontracttype__end',
    'resourcelocation__site__country', 'resourcelocation__site__city',
    'resourcelocation__start', 'resourcelocation__end',
    'teamresource__start', 'teamresource__end',
    'teamresource__team__name', 'teamresource__team__start',
    'teamresource__team__end',  'teamresource__team__productteam__start',
    'teamresource__team__productteam__end',
    'teamresource__team__productteam__product__name',
    'teamresource__team__productteam__product__start',
    'teamresource__team__productteam__product__end' ),

    columns=['signum', 'name', 'start',
    'end', 'company__name', 'resourcerole__role__name',
    'resourcerole__start', 'resourcerole__end',
    'resourcecontracttype__contracttype__name',
    'resourcecontracttype__start', 'resourcecontracttype__end',
    'resourcelocation__site__country', 'resourcelocation__site__city',
    'resourcelocation__start', 'resourcelocation__end',
    'teamresource__start', 'teamresource__end',
    'teamresource__team__name', 'teamresource__team__start',
    'teamresource__team__end',  'teamresource__team__productteam__start',
    'teamresource__team__productteam__end',
    'teamresource__team__productteam__product__name',
    'teamresource__team__productteam__product__start',
    'teamresource__team__productteam__product__end'
    ])



#Using intersect to match dates
full_data['start'].values[loop_counter] = full_data['start'].values[loop_counter] + relativedelta(months=-1)
            if full_data['end'].values[loop_counter] == None:
                date_resource = pd.date_range(full_data['start'].values[loop_counter], self.end, freq='MS')
            else:
                date_resource = pd.date_range(full_data['start'].values[loop_counter], full_data['end'].values[loop_counter], freq='MS')
            matched_dates = date_filter.intersection(date_resource)

最终结果应该是一个数据框,其中包含有关资源的所有信息以及我与所有相关模型日期相交后匹配的日期的信息。

0 个答案:

没有答案