Python 获取 DataFrame 的日期时间列的工作日/星期几

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28009370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:36:25  来源:igfitidea点击:

Get weekday/day-of-week for Datetime column of DataFrame

pythonpandasdataframedatetimedayofweek

提问by EP1986

I have a DataFrame dflike the following (excerpt, 'Timestamp' are the index):

我有一个如下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.

I need a new column df['weekday']with the respective weekday/day-of-week of the timestamps.

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

How can I get this?

我怎样才能得到这个?

采纳答案by EdChum

Use the new dt.dayofweekproperty:

使用新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

In the situation where the Timestampis your index you need to reset the index and then call the dt.dayofweekproperty:

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

Strangely if you try to create a series from the index in order to not reset the index you get NaNvalues as does using the result of reset_indexto call the dt.dayofweekproperty without assigning the result of reset_indexback to the original df:

奇怪的是,如果您尝试从索引创建一个系列以便不重置索引,您将获得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

EDIT

编辑

As pointed out to me by user @joris you can just access the weekdayattribute of the index so the following will work and is more compact:

正如用户@joris 向我指出的那样,您可以访问weekday索引的属性,因此以下内容将起作用并且更加紧凑:

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

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

回答by QM.py

If the Timestampif a datatime, then you can just use:
df['weekday'] = df['Timestamp'].apply(lambda x: x.weekday())

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

or

或者

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

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

回答by TiTo

In case somebody else has the same issue with a multiindexed dataframe, here is what solved it for me, based on @joris solution:

如果其他人对多索引数据帧有同样的问题,以下是基于@joris 解决方案为我解决的问题:

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

for me date was the get_level_values(1)instead of get_level_values(0), which would work for the outer index.

对我来说 date 是get_level_values(1)代替get_level_values(0),它适用于外部索引。