获取DataFrame的Datetime列的工作日/星期几

时间:2015-01-18 11:54:32

标签: python pandas

我有一个DataFrame df,如下所示(摘录,'时间戳'是索引):

Timestamp              Value
2012-06-01 00:00:00     100
2012-06-01 00:15:00     150
2012-06-01 00:30:00     120
2012-06-01 01:00:00     220
2012-06-01 01:15:00      80
...and so on.

我需要一个新列df['weekday'],其中包含时间戳的相应工作日/星期几。

我怎么能得到这个?

4 个答案:

答案 0 :(得分:66)

使用新的dt.dayofweek属性:

In [2]:

df['weekday'] = df['Timestamp'].dt.dayofweek
df
Out[2]:
            Timestamp  Value  weekday
0 2012-06-01 00:00:00    100        4
1 2012-06-01 00:15:00    150        4
2 2012-06-01 00:30:00    120        4
3 2012-06-01 01:00:00    220        4
4 2012-06-01 01:15:00     80        4

Timestamp是您的索引的情况下,您需要重置索引,然后调用dt.dayofweek属性:

In [14]:

df = df.reset_index()
df['weekday'] = df['Timestamp'].dt.dayofweek
df
Out[14]:
            Timestamp  Value  weekday
0 2012-06-01 00:00:00    100        4
1 2012-06-01 00:15:00    150        4
2 2012-06-01 00:30:00    120        4
3 2012-06-01 01:00:00    220        4
4 2012-06-01 01:15:00     80        4

奇怪的是,如果您尝试从索引创建一个系列以便不重置索引,那么您将获得NaN值,就像使用reset_index的结果来调用dt.dayofweek属性一样将reset_index的结果分配回原始df:

In [16]:

df['weekday'] = pd.Series(df.index).dt.dayofweek
df
Out[16]:
                     Value  weekday
Timestamp                          
2012-06-01 00:00:00    100      NaN
2012-06-01 00:15:00    150      NaN
2012-06-01 00:30:00    120      NaN
2012-06-01 01:00:00    220      NaN
2012-06-01 01:15:00     80      NaN
In [17]:

df['weekday'] = df.reset_index()['Timestamp'].dt.dayofweek
df
Out[17]:
                     Value  weekday
Timestamp                          
2012-06-01 00:00:00    100      NaN
2012-06-01 00:15:00    150      NaN
2012-06-01 00:30:00    120      NaN
2012-06-01 01:00:00    220      NaN
2012-06-01 01:15:00     80      NaN

修改

正如用户@joris所指出的那样,您只需访问索引的weekday属性,以便以下内容可以正常工作并且更加紧凑:

df['Weekday'] = df.index.weekday

答案 1 :(得分:4)

如果Timestamp datatime,那么您可以使用:
df['weekday'] = df['Timestamp'].apply(lambda x: x.weekday())

df['weekday'] = pd.to_datetime(df['Timestamp']).apply(lambda x: x.weekday())

答案 2 :(得分:1)

万一其他人对多索引数据框有相同的问题,以下是基于@joris解决方案为我解决的问题:

df['Weekday'] = df.index.get_level_values(1).weekday

对我而言,日期是get_level_values(1)而不是get_level_values(0),它将适用于外部索引。

答案 3 :(得分:1)

你可以通过这种方式获得:

import datetime
df['weekday'] = pd.Series(df.index).dt.day_name()