如何将dateinterval与给定的datafram匹配

2024-10-01 13:40:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经构建了一个数据框架,它由来自几个相关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)

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


Tags: 数据nameloopdataproductstartteamfull