改善大型数据集在pd.date_range迭代上的性能时间

时间:2018-07-09 13:36:25

标签: python python-3.x pandas date-range

我有一个非常大的日期数据集,我正在尝试计算每个月花费的天数。以下是数据集中的示例:

    <div class="product__machine-info__mobile__inner--color select-style">
      <select name="machineColorWay" v-model="color">
        <option v-for="currentColor in machine.content[0].machine_colors" :key="currentColor.color_slug" v-if="currentColor.inStock"
          v-bind:class="{ active: (currentColor.color_slug === color.color_slug)}" v-bind:value="currentColor">
          {{ currentColor.color_slug }}
        </option>
      </select>
    </div>

使用以下代码,我能够找到天数,但是输出被四舍五入,因此,例如,不显示;在18年3月花费了10.86天,它将改为输出11天。

                LoadDate            DischDate
87364    01/07/2017 00:22:16  16/07/2017 08:10:55
745999   08/06/2017 19:38:06  12/06/2017 15:20:59
1870836  17/02/2018 19:51:15  10/04/2018 03:53:57
436131   17/11/2015 00:00:17  06/01/2016 11:00:01
984437   26/11/2016 14:48:47  15/12/2016 18:39:54

我将上述pd.date_range公式中的频率从freq ='D'更改为freq ='T',以计算分钟数,然后除以1440,以准确地获得天数。但是,即使仅对少量数据集进行此过程也非常耗时,并且根本无法扩展。

例如更改后的代码:

dates.join(dates.apply(lambda x: pd.Series(pd.date_range(x.LoadDate, 
x.DischDate, freq='D')).dt.strftime('%m-%y'),1)
     .apply(lambda x: x.value_counts(),1)
     .rename(columns=lambda x: pd.to_datetime(x, format='%m-%y').strftime('%b-%y')))

我应该如何改善此代码的性能,并能够针对1.1gb大小的数据集进行扩展。

非常感谢您的帮助。

- black.mamba

0 个答案:

没有答案