Pandas to_datetime()以特定的日期时间格式发布

时间:2016-07-18 10:10:00

标签: python pandas nginx

我想将此数据帧从此字符串转换为可用的日期时间格式

01/May/2016:06:38:13 +0100

我目前正在使用Python V 3.5.1 :: Anaconda 4.0.0(64-bit)

2 个答案:

答案 0 :(得分:1)

import time
from time import mktime
from datetime import datetime
df.time = df.time.apply(lambda x: datetime.fromtimestamp(mktime(time.strptime(x, '%d/%b/%Y:%I:%M:%S %z'))))

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html https://docs.python.org/3.5/library/datetime.html#strftime-and-strptime-behavior How do you convert a Python time.struct_time object into a datetime object?

答案 1 :(得分:1)

来自Joris评论,这应该使用dateutil

from dateutil.parser import parse
a='01/May/2016:06:38:13 +0100'

In[27]:parse(a.replace(':', ' ', 1))
Out[27]: datetime.datetime(2016, 5, 1, 6, 38, 13, tzinfo=tzoffset(None, 3600))