Num day与Pandas的名字日

时间:2015-03-17 10:11:29

标签: python datetime pandas dayofweek weekday

如果我使用这个功能pd.DatetimeIndex(dfTrain['datetime']).weekday我得到当天的号码,但是我找不到任何给出de day名字的函数...所以我需要将0转换为星期一,1转换为Tuestday等等。

以下是我的数据框示例:

            datetime    season holiday workingday weather   temp    atemp   humidity    windspeed   count
    0   2011-01-01 00:00:00 1   0   0   1   9.84    14.395  81  0.0000  16
    1   2011-01-01 01:00:00 1   0   0   1   9.02    13.635  80  0.0000  40
    2   2011-01-01 02:00:00 1   0   0   1   9.02    13.635  80  0.0000  32
    3   2011-01-01 03:00:00 1   0   0   1   9.84    14.395  75  0.0000  13
    4   2011-01-01 04:00:00 1   0   0   1   9.84    14.395  75  0.0000  1
    5   2011-01-01 05:00:00 1   0   0   2   9.84    12.880  75  6.0032  1
    6   2011-01-01 06:00:00 1   0   0   1   9.02    13.635  80  0.0000  2
    7   2011-01-01 07:00:00 1   0   0   1   8.20    12.880  86  0.0000  3
    8   2011-01-01 08:00:00 1   0   0   1   9.84    14.395  75  0.0000  8
    9   2011-01-01 09:00:00 1   0   0   1   13.12   17.425  76  0.0000  14

另一个问题是,pandas.DatetimeIndex.dayofweekpandas.DatetimeIndex.weekday之间的区别是什么?

4 个答案:

答案 0 :(得分:12)

一种方法,只要datetime已经是datetime列,就是应用datetime.strftime来获取工作日的字符串:

In [105]:

df['weekday'] = df[['datetime']].apply(lambda x: dt.datetime.strftime(x['datetime'], '%A'), axis=1)
df
Out[105]:
             datetime  season  holiday  workingday  weather   temp   atemp  \
0 2011-01-01 00:00:00       1        0           0        1   9.84  14.395   
1 2011-01-01 01:00:00       1        0           0        1   9.02  13.635   
2 2011-01-01 02:00:00       1        0           0        1   9.02  13.635   
3 2011-01-01 03:00:00       1        0           0        1   9.84  14.395   
4 2011-01-01 04:00:00       1        0           0        1   9.84  14.395   
5 2011-01-01 05:00:00       1        0           0        2   9.84  12.880   
6 2011-01-01 06:00:00       1        0           0        1   9.02  13.635   
7 2011-01-01 07:00:00       1        0           0        1   8.20  12.880   
8 2011-01-01 08:00:00       1        0           0        1   9.84  14.395   
9 2011-01-01 09:00:00       1        0           0        1  13.12  17.425   

   humidity  windspeed  count   weekday  
0        81     0.0000     16  Saturday  
1        80     0.0000     40  Saturday  
2        80     0.0000     32  Saturday  
3        75     0.0000     13  Saturday  
4        75     0.0000      1  Saturday  
5        75     6.0032      1  Saturday  
6        80     0.0000      2  Saturday  
7        86     0.0000      3  Saturday  
8        75     0.0000      8  Saturday  
9        76     0.0000     14  Saturday  

关于您的其他问题,dayofweekweekday之间没有区别。

在工作日定义工作日地图到String等效和调用地图会更快:

dayOfWeek={0:'Monday', 1:'Tuesday', 2:'Wednesday', 3:'Thursday', 4:'Friday', 5:'Saturday', 6:'Sunday'}
df['weekday'] = df['datetime'].dt.dayofweek.map(dayOfWeek)

对于0.15.0之前的版本,以下内容应该有效:

import datetime as dt
df['weekday'] = df['datetime'].apply(lambda x: dt.datetime.strftime(x, '%A'))

版本0.18.1及更新

现在有了一种新的便捷方法dt.weekday_name来执行上述操作

版本0.23.0及更新

weekday_name现在已被删除,有利于dt.day_name

答案 1 :(得分:12)

在版本0.18.1中,您可以使用新方法dt.weekday_name

df['weekday'] = df['datetime'].dt.weekday_name
print df
             datetime  season  holiday  workingday  weather   temp   atemp  \
0 2011-01-01 00:00:00       1        0           0        1   9.84  14.395   
1 2011-01-01 01:00:00       1        0           0        1   9.02  13.635   
2 2011-01-01 02:00:00       1        0           0        1   9.02  13.635   
3 2011-01-01 03:00:00       1        0           0        1   9.84  14.395   
4 2011-01-01 04:00:00       1        0           0        1   9.84  14.395   
5 2011-01-01 05:00:00       1        0           0        2   9.84  12.880   
6 2011-01-01 06:00:00       1        0           0        1   9.02  13.635   
7 2011-01-01 07:00:00       1        0           0        1   8.20  12.880   
8 2011-01-01 08:00:00       1        0           0        1   9.84  14.395   
9 2011-01-01 09:00:00       1        0           0        1  13.12  17.425   

   humidity  windspeed  count   weekday  
0        81     0.0000     16  Saturday  
1        80     0.0000     40  Saturday  
2        80     0.0000     32  Saturday  
3        75     0.0000     13  Saturday  
4        75     0.0000      1  Saturday  
5        75     6.0032      1  Saturday  
6        80     0.0000      2  Saturday  
7        86     0.0000      3  Saturday  
8        75     0.0000      8  Saturday  
9        76     0.0000     14  Saturday  

答案 2 :(得分:5)

使用dt.weekday_namedeprecated since pandas 0.23.0,而是使用dt.day_name()

df.datetime.dt.day_name()

0    Saturday
1    Saturday
2    Saturday
3    Saturday
4    Saturday
5    Saturday
6    Saturday
7    Saturday
8    Saturday
9    Saturday
Name: datetime, dtype: object

答案 3 :(得分:0)

在@jezrael的上一个正确答案中,您可以使用以下方法:

import calendar
df['weekday'] = pd.Series(pd.Categorical(df['datetime'].dt.weekday_name, categories=list(calendar.day_name)))

还会根据this为您的新类别变量提供 order (在此示例中为“ Monday”,...,“ Sunday”)。这可能对下一步的分析很有帮助。