Python 将日期字符串转换为星期几
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16766643/
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
Convert Date String to Day of Week
提问by zbinsd
I have date strings like this:
我有这样的日期字符串:
'January 11, 2010'
and I need a function that returns the day of the week, like
我需要一个返回星期几的函数,比如
'mon', or 'monday'
etc. I can't find this anywhere in the Python help. Anyone? Thanks.
等等。我无法在 Python 帮助中的任何地方找到它。任何人?谢谢。
采纳答案by Andrey Sobolev
You might want to use strptime and strftimemethods from datetime:
您可能想要使用以下strptime and strftime方法datetime:
>>> import datetime
>>> datetime.datetime.strptime('January 11, 2010', '%B %d, %Y').strftime('%A')
'Monday'
or for 'Mon':
或为'Mon':
>>> datetime.datetime.strptime('January 11, 2010', '%B %d, %Y').strftime('%a')
'Mon'
回答by zbinsd
>>> import time
>>> dateStr = 'January 11, 2010'
>>> timestamp = time.strptime(dateStr, '%B %d, %Y')
>>> timestamp
time.struct_time(tm_year=2010, tm_mon=1, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=11, tm_isdst=-1)
回答by Felix Yan
回答by Lahiruzz
use date.weekday()Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
使用date.weekday()以整数形式返回星期几,其中星期一为 0,星期日为 6。
http://docs.python.org/2/library/datetime.html#datetime.date.weekday
http://docs.python.org/2/library/datetime.html#datetime.date.weekday
回答by Prasanna
import time
time.strftime('%A')
回答by Zed Fang
I think the fastest way to do it like below:
我认为最快的方法如下:
df[Date_Column].dt.weekday_name

