使用python从对象映射到日期类型

时间:2019-01-28 11:05:26

标签: python datetime

我在数据框'close_date'中有一列'opp_sacc',其格式为:

opp_sacc['close_date']
0          2012-07-31 00:00:00
1          2011-07-31 00:00:00
2          2013-01-31 00:00:00
3          2012-01-31 00:00:00
4          2014-04-30 00:00:00
...
1999821    2011-07-12 00:00:00
Name: close_date, Length: 1999822, dtype: object

我尝试使用此函数将其类型从对象映射到日期时间:

#function that transforms to date format another input
def todate(x):
    try:
        x=pd.to_datetime(re.sub('\..*', '', x), format='%d/%m/%y  %H:%M:%S')
    except:
        pass
    return x

但是当我调用此函数时:

opp_sacc['close_date']=opp_sacc['close_date'].map(todate)

它没有将其转换为日期类型。

能帮我解决这个问题吗?

thakns

0 个答案:

没有答案