将整周的月值设置为周中日月值

时间:2017-12-04 04:56:31

标签: python pandas datetime

我有以下pandas.Dataframe

Date-Time           Week Month
2017-07-29 16:05:00 30  Jul-17
2017-07-29 23:58:00 30  Jul-17
2017-07-30 04:46:00 31  Jul-17
2017-07-30 10:57:00 31  Jul-17
2017-07-31 09:36:00 31  Jul-17
2017-07-31 10:46:00 31  Jul-17
2017-08-01 09:06:00 31  Aug-17
2017-08-01 09:13:00 31  Aug-17
2017-08-02 16:02:00 31  Aug-17
2017-08-02 16:06:00 31  Aug-17
2017-08-02 16:55:00 31  Aug-17
2017-08-02 18:57:00 31  Aug-17
2017-08-02 21:56:00 31  Aug-17
2017-08-03 08:49:00 31  Aug-17
2017-08-03 14:16:00 31  Aug-17
2017-08-04 09:09:00 31  Aug-17
2017-08-04 10:08:00 31  Aug-17
2017-08-05 11:24:00 31  Aug-17
2017-08-05 12:42:00 31  Aug-17
2017-08-06 03:47:00 32  Aug-17
2017-08-06 03:55:00 32  Aug-17
2017-08-07 08:57:00 32  Aug-17
2017-08-07 11:35:00 32  Aug-17

有没有办法可以将每周的月份值标准化为中周日(星期三)所属月份的值?

在上述数据框架中,第31周的星期三位于8月,因此第31周的月份列应如下所示:

Date-Time           Week Month
2017-07-29 16:05:00 30  Jul-17
2017-07-29 23:58:00 30  Jul-17
2017-07-30 04:46:00 31  Aug-17
2017-07-30 10:57:00 31  Aug-17
2017-07-31 09:36:00 31  Aug-17
2017-07-31 10:46:00 31  Aug-17
2017-08-01 09:06:00 31  Aug-17
2017-08-01 09:13:00 31  Aug-17
2017-08-02 16:02:00 31  Aug-17
2017-08-02 16:06:00 31  Aug-17
2017-08-02 16:55:00 31  Aug-17
2017-08-02 18:57:00 31  Aug-17
2017-08-02 21:56:00 31  Aug-17
2017-08-03 08:49:00 31  Aug-17
2017-08-03 14:16:00 31  Aug-17
2017-08-04 09:09:00 31  Aug-17
2017-08-04 10:08:00 31  Aug-17
2017-08-05 11:24:00 31  Aug-17
2017-08-05 12:42:00 31  Aug-17
2017-08-06 03:47:00 32  Aug-17
2017-08-06 03:55:00 32  Aug-17
2017-08-07 08:57:00 32  Aug-17
2017-08-07 11:35:00 32  Aug-17

请注意,数据框的Week和Month列都是从同一数据帧的Date-Time列派生的。

2 个答案:

答案 0 :(得分:1)

可以按照以下方式完成:

df['DWY'] = df['Date-Time'].dt.strftime('%U-%Y')
df['DWY'] = '3-' + df['DWY'].astype(str)
for i in df.index:
    df['DWY'][i] = dt.strptime(df['DWY'][i],'%w-%U-%Y')

但是,如果有更有效的方法,请告诉我。

编辑: for循环可以替换为:

df['DWY'] = pd.to_datetime(df['DWY'],format='%w-%U-%Y')

答案 1 :(得分:1)

您可以使用timedelta将星期三移回本周初,然后使用pandas.Dateoffset将日期固定为星期几,例如:

代码:

df['DWY'] = df['Date-Time'] - pd.Timedelta(3, unit='d') - pd.DateOffset(weekday=2)

对于长矢量,这大约是四倍于你的答案中字符串操作的时间。

测试代码:

import pandas as pd

data = u"""
date        month
2016-12-31  1
2017-01-01  1
2017-01-28  1
2017-01-29  1
2017-02-25  1
2017-02-26  1
2017-04-01  1
2017-04-02  1
2017-04-29  1
2017-04-30  1
2017-06-03  1
2017-06-04  1
2017-07-01  1
2017-07-02  1
2017-07-29  1
2017-07-30  1
2017-09-02  1
2017-09-03  1
2017-09-30  1
2017-10-01  1
2017-10-28  1
2017-10-29  1
2017-12-02  1
2017-12-03  1
2017-12-30  1
2017-12-31  1"""

df = pd.read_fwf(StringIO(data), header=1)
df['date'] = pd.to_datetime(df['date'])

df['weds-week'] = df['date'] - pd.Timedelta(3, unit='d') - pd.DateOffset(weekday=2)
df['month'] = df['weds-week'].dt.month
print(df)

结果:

         date  month  weds-week
0  2016-12-31     12 2016-12-28
1  2017-01-01      1 2017-01-04
2  2017-01-28      1 2017-01-25
3  2017-01-29      2 2017-02-01
4  2017-02-25      2 2017-02-22
5  2017-02-26      3 2017-03-01
6  2017-04-01      3 2017-03-29
7  2017-04-02      4 2017-04-05
8  2017-04-29      4 2017-04-26
9  2017-04-30      5 2017-05-03
10 2017-06-03      5 2017-05-31
11 2017-06-04      6 2017-06-07
12 2017-07-01      6 2017-06-28
13 2017-07-02      7 2017-07-05
14 2017-07-29      7 2017-07-26
15 2017-07-30      8 2017-08-02
16 2017-09-02      8 2017-08-30
17 2017-09-03      9 2017-09-06
18 2017-09-30      9 2017-09-27
19 2017-10-01     10 2017-10-04
20 2017-10-28     10 2017-10-25
21 2017-10-29     11 2017-11-01
22 2017-12-02     11 2017-11-29
23 2017-12-03     12 2017-12-06
24 2017-12-30     12 2017-12-27
25 2017-12-31      1 2018-01-03