用python实现星期的转换_使用Python将每周数据转换为每日数据

I will try to be as clear as possible in this question. Let's say I have a dataframe formed as:

Date Quantity

05/05/2017 34

12/05/2017 24

19/05/2017 45

26/05/2017 23

2/06/2017 56

9/06/2017 32

I would like to convert this dataframe having weekly data (as you see) into one having daily data. However, there will be some "holes" in my dataframe (ex. week-end days). I already have stored the daily dates into another variable.

I would like to obtain something like this:

Date Quantity

05/05/2017 34

08/05/2017 34

09/05/2017 34

... ...

2/06/2017 56

5/06/2017 56

6/06/2017 56

... ...

My idea is to have a loop that says "whenever the date is prior to the date in the weekly dataframe (ex. 19/05/2017) but higher than the previous date (so 12/05/2017), I want to append to the column "Quantity" in the daily dataframe the correct number (in this case 45).

However, I do not know how to do this in Python. Should I convert the dates into numbers and then back to dates? Does anyone have any faster way to do this? Thank you very much

解决方案

Here's an option using resample with business day frequency (B) and forward fill:

df['Date'] = pd.to_datetime(df.Date, format='%d/%m/%Y')

df.set_index('Date').resample('B').ffill().reset_index()

# Date Quantity

#0 2017-05-05 34

#1 2017-05-08 34

#2 2017-05-09 34

#...

#20 2017-06-02 56

#21 2017-06-05 56

#22 2017-06-06 56

#...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值