pandas 从日期类型列获取星期几 [英] Pandas Get Day of Week from date type column

查看:280
本文介绍了 pandas 从日期类型列获取星期几的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.6和Pandas 0.20.3。

I'm using Python 3.6 and Pandas 0.20.3.

我有一列已从datetime转换为date类型。我只需要约会。为了方便使用,我将其作为派生列。但我希望通过一周中的某一天进行一些进一步的操作。我可以从日期时间类型中获取星期几,但不能从日期中获取。在我看来,这应该是可能的,但是我尝试了多种变体却没有成功。

I have a column that I've converted to date type from datetime. All I need is the date. I have it as a derived column for ease of use. But I'm looking to do some further operations via a day of the week calculation. I can get the day of week from a datetime type but not from the date. It seems to me that this should be possible but I've tried multiple variations and not found success.

这里有一个例子:

import numpy as np
import pandas as pd
df = pd.DataFrame({'date':['2017-5-16','2017-5-17']})
df['trade_date']=pd.to_datetime(df['date'])

我可以从datetime列 trade_date中获取星期几。

I can get the day of the week from the datetime column 'trade_date'.

df['dow']=df['trade_date'].dt.dayofweek
df
    date    trade_date  dow
0   2017-5-16   2017-05-16  1
1   2017-5-17   2017-05-17  2

但是如果我有个约会,宁可比日期时间更短,没有骰子:
例如:

But if I have a date, rather than a datetime, no dice: For instance:

df['trade_date_2']=pd.to_datetime(df['date']).dt.date

然后:

df['dow_2']=df['trade_date_2'].dt.dayofweek

我得到了(最后):

AttributeError: Can only use .dt accessor with datetimelike values

我已经尝试过dayofweek(),工作日的各种组合,weekday(),我意识到,我对熊猫的工作方式一无所知。所以...除了添加另一列(trade_date列的日期时间版本)之外,还有什么建议吗?
我也欢迎解释为什么它不起作用。

I've tried various combinations of dayofweek(), weekday, weekday() which, I realize, highlight my ignorance of exactly how Pandas works. So ... any suggestions besides adding another column which is the datetime version of column trade_date? I'll also welcome explanations of why this is not working.

推荐答案

有一个问题是 pandas datetime (时间戳),其中实现了 .dt 方法和 python date 否。

There is problem it is difference between pandas datetime (timestamps) where are implemented .dt methods and python date where not.

#return python date
df['trade_date_2']= pd.to_datetime(df['date']).dt.date

print (df['trade_date_2'].apply(type))
0    <class 'datetime.date'>
1    <class 'datetime.date'>
Name: trade_date_2, dtype: object

#cannot work with python date
df['dow_2']=df['trade_date_2'].dt.dayofweek

需要转换为 pandas datetime

df['dow_2']= pd.to_datetime(df['trade_date_2']).dt.dayofweek

print (df)
        date trade_date_2  dow_2
0  2017-5-16   2017-05-16      1
1  2017-5-17   2017-05-17      2

所以最好使用:

df['date'] = pd.to_datetime(df['date'])
print (df['date'].apply(type))
0    <class 'pandas._libs.tslib.Timestamp'>
1    <class 'pandas._libs.tslib.Timestamp'>
Name: date, dtype: object

df['trade_date_2']= df['date'].dt.date
df['dow_2']=df['date'].dt.dayofweek
print (df)
        date trade_date_2  dow_2
0 2017-05-16   2017-05-16      1
1 2017-05-17   2017-05-17      2

编辑:

谢谢您 Bharath shetty 解决方案使用 python date -失败,并使用 NaT

Thank you Bharath shetty for solution working with python date - failed with NaT:

df = pd.DataFrame({'date':['2017-5-16',np.nan]})

df['trade_date_2']= pd.to_datetime(df['date']).dt.date
df['dow_2'] = df['trade_date_2'].apply(lambda x: x.weekday()) 




AttributeError: float对象没有属性 weekday

AttributeError: 'float' object has no attribute 'weekday'

比较解决方案:

df = pd.DataFrame({'date':['2017-5-16','2017-5-17']})
df = pd.concat([df]*10000).reset_index(drop=True)

def a(df):
    df['trade_date_2']= pd.to_datetime(df['date']).dt.date
    df['dow_2'] = df['trade_date_2'].apply(lambda x: x.weekday()) 
    return df

def b(df):
    df['date1'] = pd.to_datetime(df['date'])
    df['trade_date_21']= df['date1'].dt.date
    df['dow_21']=df['date1'].dt.dayofweek
    return (df)

def c(df):
    #dont write to column, but to helper series 
    dates = pd.to_datetime(df['date'])
    df['trade_date_22']= dates.dt.date
    df['dow_22']=        dates.dt.dayofweek
    return (df)

In [186]: %timeit (a(df))
10 loops, best of 3: 101 ms per loop

In [187]: %timeit (b(df))
10 loops, best of 3: 90.8 ms per loop

In [188]: %timeit (c(df))
10 loops, best of 3: 91.9 ms per loop

这篇关于 pandas 从日期类型列获取星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆