在python中将对象转换为datetime

时间:2017-12-09 15:32:59

标签: python pandas datetime

我有一个类似下面的数据框。如何将reviewTime列转换为日期格式?目前,reviewTime是一个对象。

 overall    reviewTime
 6          10 19,2013
 5          06 18, 2014
 4          05 5, 2013

整体1682非空对象 reviewTime 1682非空对象

4 个答案:

答案 0 :(得分:1)

将所有,和空格替换为'',然后使用指定格式的pd.to_datetime,即

df['reviewTimeclean'] = pd.to_datetime(df['reviewTime'].str.replace('[,\s]',''),format='%m%d%Y')

# Also pd.to_datetime(df['reviewTime'].str.replace('[^\d]',''),format='%m%d%Y')

    overall   reviewTime reviewTimeclean
0        6   10 19,2013      2013-10-19
1        5  06 18, 2014      2014-06-18
2        4   05 5, 2013      2013-05-05

答案 1 :(得分:1)

import pandas as pd
import datetime

data = {'overall': {0: 6, 1: 5, 2: 4},
        'reviewTime': {0: '10 19,2013', 1: '06 18, 2014', 2: '05 5, 2013'}}

df = pd.DataFrame(data)

# Clean
df.reviewTime = df.reviewTime.apply(lambda x: ' '.join(i.strip() for i in x.split(",")))

# To_datetime and apply (commented out)
df.reviewTime = pd.to_datetime(df.reviewTime)
#df.reviewTime = df.reviewTime.apply(lambda x: datetime.datetime.strptime(x,"%m %d %Y"))


print(df)

返回:

   overall reviewTime
0        6 2013-10-19
1        5 2014-06-18
2        4 2013-05-05

答案 2 :(得分:0)

您可以使用以下代码:

import datetime

a='12 19,2013' #sample input

a=a.split()
a[1]=a[1].split(',')
b=[]
b.append(int(a[0]))
b.append(int(a[1][0]))
b.append(int(a[1][1]))

t=datetime.date(b[2],b[0],b[1])

print(t)

结果:

2013-12-19

答案 3 :(得分:0)

让我们尝试新的东西..

import re


df['reviewTime']=list(map(lambda x : re.sub('[^0-9]','', x),df['reviewTime'].values))


df['reviewTime'] = pd.to_datetime(df['reviewTime'].astype(str),format='%m%d%Y')
df
Out[352]: 
  reviewTime
0 2013-10-19
1 2014-06-18
2 2013-05-05

更多信息

list(map(lambda x : re.sub('[^0-9]','', x),df['reviewTime'].values))

Out[344]: ['10192013', '06182014', '0552013']