将df的日期范围扩展为每天一行 [英] Expand df with range of dates to one row per day

查看:51
本文介绍了将df的日期范围扩展为每天一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个df,每个项目包含一行,并带有日期范围,我需要将其扩展为每个项目每天包含一行.

I have a df that contains one line per item with a range of dates, and I need to expand it to contain one row per day per item.

它看起来像这样:

  from       to         id
1 25/02/2019 27/02/2019 A
2 15/07/2019 16/07/2019 B

我想要这个:

  date       id
1 25/02/2019 A
2 26/07/2019 A
3 27/07/2019 A
4 15/07/2019 B
5 16/07/2019 B

我设法编写了一个可以运行的代码,但是运行需要一个多小时,所以我想知道是否有更有效的方法来实现它.

I managed to write a code that works but it takes over one hour to run, so I am wondering if there is a more efficient way to do it.

我的代码:

df_dates = pd.DataFrame()

for i in range(len(df)):

    start = df.loc[i]['from']
    end = df.loc[i]['to'] + np.timedelta64(1,'D') #includes last day of the range
    dates = np.arange(start, end, dtype='datetime64[D]')

    temp = pd.DataFrame()
    temp = temp.append([df.loc[i]]*len(dates), ignore_index=True)
    temp['datadate'] = dates

    df_dates = df_dates.append(temp, ignore_index=True)

这花费了很长时间,因为真实范围大约有50年,包含1700多个项目,因此新的df非常庞大,但是也许您知道可以更快地完成相同操作的窍门:)

It takes long because the real ranges are of about 50 years with over 1700 items so the new df is massive, but maybe you know a trick to do the same faster :)

推荐答案

尝试:

df['from'] = pd.to_datetime(df['from'])
df['to'] = pd.to_datetime(df['to'])
pd.concat([pd.DataFrame({'date': pd.date_range(row['from'], row['to'], freq='D'), 'id': row['id']})
           for i, row in df.iterrows()], ignore_index=True)
        date id
0 2019-02-25  A
1 2019-02-26  A
2 2019-02-27  A
3 2019-07-15  B
4 2019-07-16  B

这篇关于将df的日期范围扩展为每天一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆