在python中将对象转换为日期时间格式 [英] Converting object to datetime format in python

查看:1635
本文介绍了在python中将对象转换为日期时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的csv DateTime列的第一行:

Below is the first row of my csv DateTime column:

2015年11月2日星期一20:37:10 GMT + 00:00

Mon Nov 02 20:37:10 GMT+00:00 2015

DateTime列当前是一个对象,我想将其转换为datetime格式,以便获取显示为2015-11-02的日期,并为该时间创建一个单独的列.

The DateTime column is currently an object and I want to convert it to datetime format so that I can get the date to appear as 2015-11-02 and I will create a separate column for the time.

我用于将列转换为日期时间格式的代码是:

The code I am using to convert the column to date time format is:

for item, frame in df['DateTime'].iteritems():
     datetime.datetime.strptime(df['DateTime'], "%a-%b-%d-%H-%M-%S-%Z-%Y")

我收到此错误: TypeError:必须为str,而不是Series

I am getting this error: TypeError: must be str, not Series

任何帮助将不胜感激!

推荐答案

使用pd.to_datetime():

df['DateTime'] = pd.to_datetime(df['DateTime'])

例如,

pd.to_datetime('Mon Nov 02 20:37:10 GMT+00:00 2015')

产生Timestamp('2015-11-02 20:37:10').

这篇关于在python中将对象转换为日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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